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_EXPANDPARTIAL, TVIS_EX_FLAT,
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
44 #include "wine/port.h"
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
69 /* internal structures */
70 typedef struct tagTREEVIEW_INFO
73 HWND hwndNotify; /* Owner window to send notifications to */
78 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
79 INT cdmode; /* last custom draw setting */
80 UINT uScrollTime; /* max. time for scrolling in milliseconds */
81 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
83 UINT uItemHeight; /* item height */
86 LONG clientWidth; /* width of control window */
87 LONG clientHeight; /* height of control window */
89 LONG treeWidth; /* width of visible tree items */
90 LONG treeHeight; /* height of visible tree items */
92 UINT uIndent; /* indentation in pixels */
93 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
94 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
95 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
96 HTREEITEM editItem; /* item being edited with builtin edit box */
98 HTREEITEM firstVisible; /* handle to first visible item */
100 HTREEITEM dropItem; /* handle to item selected by drag cursor */
101 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
102 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
103 HIMAGELIST dragList; /* Bitmap of dragged item */
108 COLORREF clrInsertMark;
112 HFONT hUnderlineFont;
117 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
118 BOOL bIgnoreEditKillFocus;
121 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
122 HIMAGELIST himlNormal;
123 int normalImageHeight;
124 int normalImageWidth;
125 HIMAGELIST himlState;
126 int stateImageHeight;
130 DWORD lastKeyPressTimestamp;
132 INT nSearchParamLength;
133 WCHAR szSearchParam[ MAX_PATH ];
136 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
138 HTREEITEM parent; /* handle to parent or 0 if at root */
139 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
140 HTREEITEM firstChild; /* handle to first child or 0 if no child */
152 int iIntegral; /* item height multiplier (1 is normal) */
153 int iLevel; /* indentation level:0=root level */
155 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
161 LONG textWidth; /* horizontal text extent for pszText */
162 LONG visibleOrder; /* visible ordering, 0 is first visible item */
163 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
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_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
212 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
213 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
215 /* Random Utilities *****************************************************/
216 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
218 /* Returns the treeview private data if hwnd is a treeview.
219 * Otherwise returns an undefined value. */
220 static inline TREEVIEW_INFO *
221 TREEVIEW_GetInfoPtr(HWND hwnd)
223 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
226 /* Don't call this. Nothing wants an item index. */
228 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
230 return DPA_GetPtrIndex(infoPtr->items, handle);
233 /* Checks if item has changed and needs to be redrawn */
234 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
235 const TVITEMEXW *tvChange)
237 /* Number of children has changed */
238 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
241 /* Image has changed and it's not a callback */
242 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
243 tiNew->iImage != I_IMAGECALLBACK)
246 /* Selected image has changed and it's not a callback */
247 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
248 tiNew->iSelectedImage != I_IMAGECALLBACK)
251 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
252 tiNew->iExpandedImage != I_IMAGECALLBACK)
255 /* Text has changed and it's not a callback */
256 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
257 tiNew->pszText != LPSTR_TEXTCALLBACKW)
260 /* Indent has changed */
261 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
264 /* Item state has changed */
265 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
271 /***************************************************************************
272 * This method checks that handle is an item for this tree.
275 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
277 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
279 TRACE("invalid item %p\n", handle);
287 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
291 GetObjectW(hOrigFont, sizeof(font), &font);
292 font.lfWeight = FW_BOLD;
293 return CreateFontIndirectW(&font);
297 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
301 GetObjectW(hOrigFont, sizeof(font), &font);
302 font.lfUnderline = TRUE;
303 return CreateFontIndirectW(&font);
307 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
309 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
310 return infoPtr->hUnderlineFont;
311 if (item->state & TVIS_BOLD)
312 return infoPtr->hBoldFont;
313 return infoPtr->hFont;
316 /* for trace/debugging purposes only */
318 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
320 if (item == NULL) return "<null item>";
321 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
322 if (item->pszText == NULL) return "<null>";
323 return debugstr_w(item->pszText);
326 /* An item is not a child of itself. */
328 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
332 child = child->parent;
333 if (child == parent) return TRUE;
334 } while (child != NULL);
340 /* Tree Traversal *******************************************************/
342 /***************************************************************************
343 * This method returns the last expanded sibling or child child item
346 static TREEVIEW_ITEM *
347 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
349 if (!item) return NULL;
351 while (item->lastChild)
353 if (item->state & TVIS_EXPANDED)
354 item = item->lastChild;
359 if (item == infoPtr->root)
365 /***************************************************************************
366 * This method returns the previous non-hidden item in the list not
367 * considering the tree hierarchy.
369 static TREEVIEW_ITEM *
370 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
372 if (tvItem->prevSibling)
374 /* This item has a prevSibling, get the last item in the sibling's tree. */
375 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
377 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
378 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
384 /* this item does not have a prevSibling, get the parent */
385 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
390 /***************************************************************************
391 * This method returns the next physical item in the treeview not
392 * considering the tree hierarchy.
394 static TREEVIEW_ITEM *
395 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
398 * If this item has children and is expanded, return the first child
400 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
402 return tvItem->firstChild;
407 * try to get the sibling
409 if (tvItem->nextSibling)
410 return tvItem->nextSibling;
413 * Otherwise, get the parent's sibling.
415 while (tvItem->parent)
417 tvItem = tvItem->parent;
419 if (tvItem->nextSibling)
420 return tvItem->nextSibling;
426 /***************************************************************************
427 * This method returns the nth item starting at the given item. It returns
428 * the last item (or first) we we run out of items.
430 * Will scroll backward if count is <0.
431 * forward if count is >0.
433 static TREEVIEW_ITEM *
434 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
437 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
438 TREEVIEW_ITEM *previousItem;
440 assert(item != NULL);
444 next_item = TREEVIEW_GetNextListItem;
449 next_item = TREEVIEW_GetPrevListItem;
457 item = next_item(infoPtr, item);
459 } while (--count && item != NULL);
462 return item ? item : previousItem;
465 /* Notifications ************************************************************/
467 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
469 if (!infoPtr->bNtfUnicode) {
471 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
472 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
473 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
474 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
475 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
476 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
477 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
478 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
479 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
480 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
481 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
482 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
489 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPNMHDR pnmh)
491 TRACE("wParam=%ld, lParam=%p\n", wParam, pnmh);
492 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, (LPARAM)pnmh);
496 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
499 HWND hwnd = infoPtr->hwnd;
502 nmhdr.hwndFrom = hwnd;
503 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
504 nmhdr.code = get_notifycode(infoPtr, code);
506 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, &nmhdr);
510 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
513 tvItem->hItem = item;
514 tvItem->state = item->state;
515 tvItem->stateMask = 0;
516 tvItem->iImage = item->iImage;
517 tvItem->iSelectedImage = item->iSelectedImage;
518 tvItem->cChildren = item->cChildren;
519 tvItem->lParam = item->lParam;
523 if (!infoPtr->bNtfUnicode)
525 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
526 tvItem->pszText = Alloc (tvItem->cchTextMax);
527 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
531 tvItem->cchTextMax = item->cchTextMax;
532 tvItem->pszText = item->pszText;
537 tvItem->cchTextMax = 0;
538 tvItem->pszText = NULL;
543 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
544 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
546 HWND hwnd = infoPtr->hwnd;
550 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
551 code, action, oldItem, newItem);
553 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
555 nmhdr.hdr.hwndFrom = hwnd;
556 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
557 nmhdr.hdr.code = get_notifycode(infoPtr, code);
558 nmhdr.action = action;
561 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
564 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
569 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
570 if (!infoPtr->bNtfUnicode)
572 Free(nmhdr.itemOld.pszText);
573 Free(nmhdr.itemNew.pszText);
579 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
580 HTREEITEM dragItem, POINT pt)
582 HWND hwnd = infoPtr->hwnd;
585 TRACE("code:%d dragitem:%p\n", code, dragItem);
587 nmhdr.hdr.hwndFrom = hwnd;
588 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
589 nmhdr.hdr.code = get_notifycode(infoPtr, code);
591 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
592 nmhdr.itemNew.hItem = dragItem;
593 nmhdr.itemNew.state = dragItem->state;
594 nmhdr.itemNew.lParam = dragItem->lParam;
596 nmhdr.ptDrag.x = pt.x;
597 nmhdr.ptDrag.y = pt.y;
599 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
604 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
607 HWND hwnd = infoPtr->hwnd;
608 NMTVCUSTOMDRAW nmcdhdr;
611 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
613 nmcd = &nmcdhdr.nmcd;
614 nmcd->hdr.hwndFrom = hwnd;
615 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
616 nmcd->hdr.code = NM_CUSTOMDRAW;
617 nmcd->dwDrawStage = dwDrawStage;
620 nmcd->dwItemSpec = 0;
621 nmcd->uItemState = 0;
622 nmcd->lItemlParam = 0;
623 nmcdhdr.clrText = infoPtr->clrText;
624 nmcdhdr.clrTextBk = infoPtr->clrBk;
627 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr.nmcd.hdr);
632 /* FIXME: need to find out when the flags in uItemState need to be set */
635 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
636 TREEVIEW_ITEM *item, UINT uItemDrawState,
637 NMTVCUSTOMDRAW *nmcdhdr)
639 HWND hwnd = infoPtr->hwnd;
642 DWORD_PTR dwItemSpec;
645 dwDrawStage = CDDS_ITEM | uItemDrawState;
646 dwItemSpec = (DWORD_PTR)item;
648 if (item->state & TVIS_SELECTED)
649 uItemState |= CDIS_SELECTED;
650 if (item == infoPtr->selectedItem)
651 uItemState |= CDIS_FOCUS;
652 if (item == infoPtr->hotItem)
653 uItemState |= CDIS_HOT;
655 nmcd = &nmcdhdr->nmcd;
656 nmcd->hdr.hwndFrom = hwnd;
657 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
658 nmcd->hdr.code = NM_CUSTOMDRAW;
659 nmcd->dwDrawStage = dwDrawStage;
661 nmcd->rc = item->rect;
662 nmcd->dwItemSpec = dwItemSpec;
663 nmcd->uItemState = uItemState;
664 nmcd->lItemlParam = item->lParam;
665 nmcdhdr->iLevel = item->iLevel;
667 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
668 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
669 nmcd->uItemState, nmcd->lItemlParam);
671 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr->nmcd.hdr);
675 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
677 HWND hwnd = infoPtr->hwnd;
681 tvdi.hdr.hwndFrom = hwnd;
682 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
683 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
685 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
686 &tvdi.item, editItem);
688 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
690 if (!infoPtr->bNtfUnicode)
691 Free(tvdi.item.pszText);
697 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
700 NMTVDISPINFOEXW callback;
701 HWND hwnd = infoPtr->hwnd;
703 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
704 mask &= item->callbackMask;
706 if (mask == 0) return;
708 callback.hdr.hwndFrom = hwnd;
709 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
710 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
712 /* 'state' always contains valid value, as well as 'lParam'.
713 * All other parameters are uninitialized.
715 callback.item.pszText = item->pszText;
716 callback.item.cchTextMax = item->cchTextMax;
717 callback.item.mask = mask;
718 callback.item.hItem = item;
719 callback.item.state = item->state;
720 callback.item.lParam = item->lParam;
722 /* If text is changed we need to recalculate textWidth */
723 if (mask & TVIF_TEXT)
726 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, &callback.hdr);
727 TRACE("resulting code 0x%08x\n", callback.hdr.code);
729 /* It may have changed due to a call to SetItem. */
730 mask &= item->callbackMask;
732 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
734 /* Instead of copying text into our buffer user specified his own */
735 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
738 int len = MultiByteToWideChar( CP_ACP, 0,
739 (LPSTR)callback.item.pszText, -1,
741 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
742 newText = ReAlloc(item->pszText, buflen);
744 TRACE("returned str %s, len=%d, buflen=%d\n",
745 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
749 item->pszText = newText;
750 MultiByteToWideChar( CP_ACP, 0,
751 (LPSTR)callback.item.pszText, -1,
752 item->pszText, buflen/sizeof(WCHAR));
753 item->cchTextMax = buflen/sizeof(WCHAR);
755 /* If ReAlloc fails we have nothing to do, but keep original text */
758 int len = max(lstrlenW(callback.item.pszText) + 1,
760 LPWSTR newText = ReAlloc(item->pszText, len);
762 TRACE("returned wstr %s, len=%d\n",
763 debugstr_w(callback.item.pszText), len);
767 item->pszText = newText;
768 strcpyW(item->pszText, callback.item.pszText);
769 item->cchTextMax = len;
771 /* If ReAlloc fails we have nothing to do, but keep original text */
774 else if (mask & TVIF_TEXT) {
775 /* User put text into our buffer, that is ok unless A string */
776 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
779 int len = MultiByteToWideChar( CP_ACP, 0,
780 (LPSTR)callback.item.pszText, -1,
782 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
783 newText = Alloc(buflen);
785 TRACE("same buffer str %s, len=%d, buflen=%d\n",
786 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
790 LPWSTR oldText = item->pszText;
791 item->pszText = newText;
792 MultiByteToWideChar( CP_ACP, 0,
793 (LPSTR)callback.item.pszText, -1,
794 item->pszText, buflen/sizeof(WCHAR));
795 item->cchTextMax = buflen/sizeof(WCHAR);
801 if (mask & TVIF_IMAGE)
802 item->iImage = callback.item.iImage;
804 if (mask & TVIF_SELECTEDIMAGE)
805 item->iSelectedImage = callback.item.iSelectedImage;
807 if (mask & TVIF_EXPANDEDIMAGE)
808 item->iExpandedImage = callback.item.iExpandedImage;
810 if (mask & TVIF_CHILDREN)
811 item->cChildren = callback.item.cChildren;
813 if (callback.item.mask & TVIF_STATE)
815 item->state &= ~callback.item.stateMask;
816 item->state |= (callback.item.state & callback.item.stateMask);
819 /* These members are now permanently set. */
820 if (callback.item.mask & TVIF_DI_SETITEM)
821 item->callbackMask &= ~callback.item.mask;
824 /***************************************************************************
825 * This function uses cChildren field to decide whether the item has
827 * Note: if this returns TRUE, the child items may not actually exist,
828 * they could be virtual.
830 * Just use item->firstChild to check for physical children.
833 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
835 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
837 return item->cChildren > 0;
840 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
844 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
846 if (nCommand != NF_REQUERY) return 0;
848 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
849 TRACE("format=%d\n", format);
851 /* Invalid format returned by NF_QUERY defaults to ANSI*/
852 if (format != NFR_ANSI && format != NFR_UNICODE)
855 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
860 /* Item Position ********************************************************/
862 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
864 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
866 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
867 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
870 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
872 item->stateOffset = item->linesOffset + infoPtr->uIndent;
873 item->imageOffset = item->stateOffset
874 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
875 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
879 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
885 /* DRAW's OM docker creates items like this */
886 if (item->pszText == NULL)
898 hdc = GetDC(infoPtr->hwnd);
899 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
902 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
903 item->textWidth = sz.cx;
907 SelectObject(hdc, hOldFont);
913 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
915 item->rect.top = infoPtr->uItemHeight *
916 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
918 item->rect.bottom = item->rect.top
919 + infoPtr->uItemHeight * item->iIntegral - 1;
922 item->rect.right = infoPtr->clientWidth;
925 /* We know that only items after start need their order updated. */
927 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
934 start = infoPtr->root->firstChild;
938 order = start->visibleOrder;
940 for (item = start; item != NULL;
941 item = TREEVIEW_GetNextListItem(infoPtr, item))
943 if (!ISVISIBLE(item) && order > 0)
944 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
945 item->visibleOrder = order;
946 order += item->iIntegral;
949 infoPtr->maxVisibleOrder = order;
951 for (item = start; item != NULL;
952 item = TREEVIEW_GetNextListItem(infoPtr, item))
954 TREEVIEW_ComputeItemRect(infoPtr, item);
959 /* Update metrics of all items in selected subtree.
960 * root must be expanded
963 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
965 TREEVIEW_ITEM *sibling;
969 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
972 root->state &= ~TVIS_EXPANDED;
973 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
974 root->state |= TVIS_EXPANDED;
976 hdc = GetDC(infoPtr->hwnd);
977 hOldFont = SelectObject(hdc, infoPtr->hFont);
979 for (; root != sibling;
980 root = TREEVIEW_GetNextListItem(infoPtr, root))
982 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
984 if (root->callbackMask & TVIF_TEXT)
985 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
987 if (root->textWidth == 0)
989 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
990 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
994 SelectObject(hdc, hOldFont);
995 ReleaseDC(infoPtr->hwnd, hdc);
998 /* Item Allocation **********************************************************/
1000 static TREEVIEW_ITEM *
1001 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1003 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1008 /* I_IMAGENONE would make more sense but this is neither what is
1009 * documented (MSDN doesn't specify) nor what Windows actually does
1010 * (it sets it to zero)... and I can so imagine an application using
1011 * inc/dec to toggle the images. */
1012 newItem->iImage = 0;
1013 newItem->iSelectedImage = 0;
1014 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1015 newItem->infoPtr = infoPtr;
1017 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1026 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1027 * free item->pszText. */
1029 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1031 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1032 if (infoPtr->selectedItem == item)
1033 infoPtr->selectedItem = NULL;
1034 if (infoPtr->hotItem == item)
1035 infoPtr->hotItem = NULL;
1036 if (infoPtr->focusedItem == item)
1037 infoPtr->focusedItem = NULL;
1038 if (infoPtr->firstVisible == item)
1039 infoPtr->firstVisible = NULL;
1040 if (infoPtr->dropItem == item)
1041 infoPtr->dropItem = NULL;
1042 if (infoPtr->insertMarkItem == item)
1043 infoPtr->insertMarkItem = NULL;
1048 /* Item Insertion *******************************************************/
1050 /***************************************************************************
1051 * This method inserts newItem before sibling as a child of parent.
1052 * sibling can be NULL, but only if parent has no children.
1055 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1056 TREEVIEW_ITEM *parent)
1058 assert(parent != NULL);
1060 if (sibling != NULL)
1062 assert(sibling->parent == parent);
1064 if (sibling->prevSibling != NULL)
1065 sibling->prevSibling->nextSibling = newItem;
1067 newItem->prevSibling = sibling->prevSibling;
1068 sibling->prevSibling = newItem;
1071 newItem->prevSibling = NULL;
1073 newItem->nextSibling = sibling;
1075 if (parent->firstChild == sibling)
1076 parent->firstChild = newItem;
1078 if (parent->lastChild == NULL)
1079 parent->lastChild = newItem;
1082 /***************************************************************************
1083 * This method inserts newItem after sibling as a child of parent.
1084 * sibling can be NULL, but only if parent has no children.
1087 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1088 TREEVIEW_ITEM *parent)
1090 assert(parent != NULL);
1092 if (sibling != NULL)
1094 assert(sibling->parent == parent);
1096 if (sibling->nextSibling != NULL)
1097 sibling->nextSibling->prevSibling = newItem;
1099 newItem->nextSibling = sibling->nextSibling;
1100 sibling->nextSibling = newItem;
1103 newItem->nextSibling = NULL;
1105 newItem->prevSibling = sibling;
1107 if (parent->lastChild == sibling)
1108 parent->lastChild = newItem;
1110 if (parent->firstChild == NULL)
1111 parent->firstChild = newItem;
1115 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1116 const TVITEMEXW *tvItem, BOOL isW)
1118 UINT callbackClear = 0;
1119 UINT callbackSet = 0;
1121 TRACE("item %p\n", item);
1122 /* Do this first in case it fails. */
1123 if (tvItem->mask & TVIF_TEXT)
1125 item->textWidth = 0; /* force width recalculation */
1126 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1131 len = lstrlenW(tvItem->pszText) + 1;
1133 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1135 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1137 if (newText == NULL) return FALSE;
1139 callbackClear |= TVIF_TEXT;
1141 item->pszText = newText;
1142 item->cchTextMax = len;
1144 lstrcpynW(item->pszText, tvItem->pszText, len);
1146 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1147 item->pszText, len);
1149 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1153 callbackSet |= TVIF_TEXT;
1155 item->pszText = ReAlloc(item->pszText,
1156 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1157 item->cchTextMax = TEXT_CALLBACK_SIZE;
1158 TRACE("setting callback, item %p\n", item);
1162 if (tvItem->mask & TVIF_CHILDREN)
1164 item->cChildren = tvItem->cChildren;
1166 if (item->cChildren == I_CHILDRENCALLBACK)
1167 callbackSet |= TVIF_CHILDREN;
1169 callbackClear |= TVIF_CHILDREN;
1172 if (tvItem->mask & TVIF_IMAGE)
1174 item->iImage = tvItem->iImage;
1176 if (item->iImage == I_IMAGECALLBACK)
1177 callbackSet |= TVIF_IMAGE;
1179 callbackClear |= TVIF_IMAGE;
1182 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1184 item->iSelectedImage = tvItem->iSelectedImage;
1186 if (item->iSelectedImage == I_IMAGECALLBACK)
1187 callbackSet |= TVIF_SELECTEDIMAGE;
1189 callbackClear |= TVIF_SELECTEDIMAGE;
1192 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1194 item->iExpandedImage = tvItem->iExpandedImage;
1196 if (item->iExpandedImage == I_IMAGECALLBACK)
1197 callbackSet |= TVIF_EXPANDEDIMAGE;
1199 callbackClear |= TVIF_EXPANDEDIMAGE;
1202 if (tvItem->mask & TVIF_PARAM)
1203 item->lParam = tvItem->lParam;
1205 /* If the application sets TVIF_INTEGRAL without
1206 * supplying a TVITEMEX structure, it's toast. */
1207 if (tvItem->mask & TVIF_INTEGRAL)
1208 item->iIntegral = tvItem->iIntegral;
1210 if (tvItem->mask & TVIF_STATE)
1212 TRACE("prevstate,state,mask:%x,%x,%x\n", item->state, tvItem->state,
1214 item->state &= ~tvItem->stateMask;
1215 item->state |= (tvItem->state & tvItem->stateMask);
1218 if (tvItem->mask & TVIF_STATEEX)
1220 FIXME("New extended state: %x\n", tvItem->uStateEx);
1223 item->callbackMask |= callbackSet;
1224 item->callbackMask &= ~callbackClear;
1229 /* Note that the new item is pre-zeroed. */
1231 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1233 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1234 HTREEITEM insertAfter;
1235 TREEVIEW_ITEM *newItem, *parentItem;
1236 BOOL bTextUpdated = FALSE;
1238 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1240 parentItem = infoPtr->root;
1244 parentItem = ptdi->hParent;
1246 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1248 WARN("invalid parent %p\n", parentItem);
1253 insertAfter = ptdi->hInsertAfter;
1255 /* Validate this now for convenience. */
1256 switch ((DWORD_PTR)insertAfter)
1258 case (DWORD_PTR)TVI_FIRST:
1259 case (DWORD_PTR)TVI_LAST:
1260 case (DWORD_PTR)TVI_SORT:
1264 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1265 insertAfter->parent != parentItem)
1267 WARN("invalid insert after %p\n", insertAfter);
1268 insertAfter = TVI_LAST;
1272 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1273 (tvItem->mask & TVIF_TEXT)
1274 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1275 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1278 newItem = TREEVIEW_AllocateItem(infoPtr);
1279 if (newItem == NULL)
1282 newItem->parent = parentItem;
1283 newItem->iIntegral = 1;
1284 newItem->visibleOrder = -1;
1286 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1289 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1291 infoPtr->uNumItems++;
1293 switch ((DWORD_PTR)insertAfter)
1295 case (DWORD_PTR)TVI_FIRST:
1297 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1298 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1299 if (infoPtr->firstVisible == originalFirst)
1300 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1304 case (DWORD_PTR)TVI_LAST:
1305 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1308 /* hInsertAfter names a specific item we want to insert after */
1310 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1313 case (DWORD_PTR)TVI_SORT:
1315 TREEVIEW_ITEM *aChild;
1316 TREEVIEW_ITEM *previousChild = NULL;
1317 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1318 BOOL bItemInserted = FALSE;
1320 aChild = parentItem->firstChild;
1322 bTextUpdated = TRUE;
1323 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1325 /* Iterate the parent children to see where we fit in */
1326 while (aChild != NULL)
1330 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1331 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1333 if (comp < 0) /* we are smaller than the current one */
1335 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1336 if (infoPtr->firstVisible == originalFirst &&
1337 aChild == originalFirst)
1338 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1339 bItemInserted = TRUE;
1342 else if (comp > 0) /* we are bigger than the current one */
1344 previousChild = aChild;
1346 /* This will help us to exit if there is no more sibling */
1347 aChild = (aChild->nextSibling == 0)
1349 : aChild->nextSibling;
1351 /* Look at the next item */
1357 * An item with this name is already existing, therefore,
1358 * we add after the one we found
1360 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1361 bItemInserted = TRUE;
1367 * we reach the end of the child list and the item has not
1368 * yet been inserted, therefore, insert it after the last child.
1370 if ((!bItemInserted) && (aChild == NULL))
1371 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1378 TRACE("new item %p; parent %p, mask %x\n", newItem,
1379 newItem->parent, tvItem->mask);
1381 newItem->iLevel = newItem->parent->iLevel + 1;
1383 if (newItem->parent->cChildren == 0)
1384 newItem->parent->cChildren = 1;
1386 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1388 if (STATEIMAGEINDEX(newItem->state) == 0)
1389 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1392 if (infoPtr->firstVisible == NULL)
1393 infoPtr->firstVisible = newItem;
1395 TREEVIEW_VerifyTree(infoPtr);
1397 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1399 if (parentItem == infoPtr->root ||
1400 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1402 TREEVIEW_ITEM *item;
1403 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1405 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1406 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1409 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1411 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1412 TREEVIEW_UpdateScrollBars(infoPtr);
1414 * if the item was inserted in a visible part of the tree,
1415 * invalidate it, as well as those after it
1417 for (item = newItem;
1419 item = TREEVIEW_GetNextListItem(infoPtr, item))
1420 TREEVIEW_Invalidate(infoPtr, item);
1424 /* refresh treeview if newItem is the first item inserted under parentItem */
1425 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1427 /* parent got '+' - update it */
1428 TREEVIEW_Invalidate(infoPtr, parentItem);
1432 return (LRESULT)newItem;
1435 /* Item Deletion ************************************************************/
1437 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1440 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1442 TREEVIEW_ITEM *kill = parentItem->firstChild;
1444 while (kill != NULL)
1446 TREEVIEW_ITEM *next = kill->nextSibling;
1448 TREEVIEW_RemoveItem(infoPtr, kill);
1453 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1454 assert(parentItem->firstChild == NULL);
1455 assert(parentItem->lastChild == NULL);
1459 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1461 TREEVIEW_ITEM *parentItem = item->parent;
1463 assert(item != NULL);
1464 assert(item->parent != NULL); /* i.e. it must not be the root */
1466 if (parentItem->firstChild == item)
1467 parentItem->firstChild = item->nextSibling;
1469 if (parentItem->lastChild == item)
1470 parentItem->lastChild = item->prevSibling;
1472 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1473 && parentItem->cChildren > 0)
1474 parentItem->cChildren = 0;
1476 if (item->prevSibling)
1477 item->prevSibling->nextSibling = item->nextSibling;
1479 if (item->nextSibling)
1480 item->nextSibling->prevSibling = item->prevSibling;
1484 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1486 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1488 if (item->firstChild)
1489 TREEVIEW_RemoveAllChildren(infoPtr, item);
1491 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1492 TVIF_HANDLE | TVIF_PARAM, item, 0);
1494 TREEVIEW_UnlinkItem(item);
1496 infoPtr->uNumItems--;
1498 if (item->pszText != LPSTR_TEXTCALLBACKW)
1499 Free(item->pszText);
1501 TREEVIEW_FreeItem(infoPtr, item);
1505 /* Empty out the tree. */
1507 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1509 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1511 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1515 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1517 TREEVIEW_ITEM *newSelection = NULL;
1518 TREEVIEW_ITEM *newFirstVisible = NULL;
1519 TREEVIEW_ITEM *parent, *prev = NULL;
1520 BOOL visible = FALSE;
1522 if (item == TVI_ROOT || !item)
1524 TRACE("TVI_ROOT\n");
1525 parent = infoPtr->root;
1526 newSelection = NULL;
1528 TREEVIEW_RemoveTree(infoPtr);
1532 if (!TREEVIEW_ValidItem(infoPtr, item))
1535 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1536 parent = item->parent;
1538 if (ISVISIBLE(item))
1540 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1544 if (infoPtr->selectedItem != NULL
1545 && (item == infoPtr->selectedItem
1546 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1548 if (item->nextSibling)
1549 newSelection = item->nextSibling;
1550 else if (item->parent != infoPtr->root)
1551 newSelection = item->parent;
1553 newSelection = item->prevSibling;
1554 TRACE("newSelection = %p\n", newSelection);
1557 if (infoPtr->firstVisible == item)
1559 if (item->nextSibling)
1560 newFirstVisible = item->nextSibling;
1561 else if (item->prevSibling)
1562 newFirstVisible = item->prevSibling;
1563 else if (item->parent != infoPtr->root)
1564 newFirstVisible = item->parent;
1565 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1568 newFirstVisible = infoPtr->firstVisible;
1570 TREEVIEW_RemoveItem(infoPtr, item);
1573 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1574 if (!infoPtr->selectedItem && newSelection)
1576 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1577 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1580 /* Validate insertMark dropItem.
1581 * hotItem ??? - used for comparison only.
1583 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1584 infoPtr->insertMarkItem = 0;
1586 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1587 infoPtr->dropItem = 0;
1589 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1590 newFirstVisible = infoPtr->root->firstChild;
1592 TREEVIEW_VerifyTree(infoPtr);
1594 if (!infoPtr->bRedraw) return TRUE;
1598 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1599 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1600 TREEVIEW_UpdateScrollBars(infoPtr);
1601 TREEVIEW_Invalidate(infoPtr, NULL);
1603 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1605 /* parent lost '+/-' - update it */
1606 TREEVIEW_Invalidate(infoPtr, parent);
1613 /* Get/Set Messages *********************************************************/
1615 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1617 infoPtr->bRedraw = wParam != 0;
1619 if (infoPtr->bRedraw)
1621 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1622 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1623 TREEVIEW_UpdateScrollBars(infoPtr);
1624 TREEVIEW_Invalidate(infoPtr, NULL);
1630 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1633 return infoPtr->uIndent;
1637 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1641 if (newIndent < MINIMUM_INDENT)
1642 newIndent = MINIMUM_INDENT;
1644 if (infoPtr->uIndent != newIndent)
1646 infoPtr->uIndent = newIndent;
1647 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1648 TREEVIEW_UpdateScrollBars(infoPtr);
1649 TREEVIEW_Invalidate(infoPtr, NULL);
1657 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1660 return (LRESULT)infoPtr->hwndToolTip;
1664 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1669 prevToolTip = infoPtr->hwndToolTip;
1670 infoPtr->hwndToolTip = hwndTT;
1672 return (LRESULT)prevToolTip;
1676 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1678 BOOL rc = infoPtr->bNtfUnicode;
1679 infoPtr->bNtfUnicode = fUnicode;
1684 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1686 return infoPtr->bNtfUnicode;
1690 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1692 return infoPtr->uScrollTime;
1696 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1698 UINT uOldScrollTime = infoPtr->uScrollTime;
1700 infoPtr->uScrollTime = min(uScrollTime, 100);
1702 return uOldScrollTime;
1707 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1714 return (LRESULT)infoPtr->himlNormal;
1717 return (LRESULT)infoPtr->himlState;
1724 #define TVHEIGHT_MIN 16
1725 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1727 /* Compute the natural height for items. */
1729 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1733 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1736 /* Height is the maximum of:
1737 * 16 (a hack because our fonts are tiny), and
1738 * The text height + border & margin, and
1739 * The size of the normal image list
1741 GetTextMetricsW(hdc, &tm);
1742 SelectObject(hdc, hOldFont);
1745 height = TVHEIGHT_MIN;
1746 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1747 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1748 if (height < infoPtr->normalImageHeight)
1749 height = infoPtr->normalImageHeight;
1751 /* Round down, unless we support odd ("non even") heights. */
1752 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1759 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1761 HIMAGELIST himlOld = 0;
1762 int oldWidth = infoPtr->normalImageWidth;
1763 int oldHeight = infoPtr->normalImageHeight;
1765 TRACE("%u,%p\n", type, himlNew);
1770 himlOld = infoPtr->himlNormal;
1771 infoPtr->himlNormal = himlNew;
1774 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1775 &infoPtr->normalImageHeight);
1778 infoPtr->normalImageWidth = 0;
1779 infoPtr->normalImageHeight = 0;
1785 himlOld = infoPtr->himlState;
1786 infoPtr->himlState = himlNew;
1789 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1790 &infoPtr->stateImageHeight);
1793 infoPtr->stateImageWidth = 0;
1794 infoPtr->stateImageHeight = 0;
1800 ERR("unknown imagelist type %u\n", type);
1803 if (oldWidth != infoPtr->normalImageWidth ||
1804 oldHeight != infoPtr->normalImageHeight)
1806 BOOL bRecalcVisible = FALSE;
1808 if (oldHeight != infoPtr->normalImageHeight &&
1809 !infoPtr->bHeightSet)
1811 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1812 bRecalcVisible = TRUE;
1815 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1816 infoPtr->normalImageWidth != infoPtr->uIndent)
1818 infoPtr->uIndent = infoPtr->normalImageWidth;
1819 bRecalcVisible = TRUE;
1823 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1825 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1826 TREEVIEW_UpdateScrollBars(infoPtr);
1829 TREEVIEW_Invalidate(infoPtr, NULL);
1831 return (LRESULT)himlOld;
1835 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1837 INT prevHeight = infoPtr->uItemHeight;
1839 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1840 if (newHeight == -1)
1842 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1843 infoPtr->bHeightSet = FALSE;
1847 if (newHeight == 0) newHeight = 1;
1848 infoPtr->uItemHeight = newHeight;
1849 infoPtr->bHeightSet = TRUE;
1852 /* Round down, unless we support odd ("non even") heights. */
1853 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1855 infoPtr->uItemHeight &= ~1;
1856 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1859 if (infoPtr->uItemHeight != prevHeight)
1861 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1862 TREEVIEW_UpdateScrollBars(infoPtr);
1863 TREEVIEW_Invalidate(infoPtr, NULL);
1870 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1873 return infoPtr->uItemHeight;
1878 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1880 TRACE("%p\n", infoPtr->hFont);
1881 return (LRESULT)infoPtr->hFont;
1886 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1890 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1896 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1898 UINT uHeight = infoPtr->uItemHeight;
1900 TRACE("%p %i\n", hFont, bRedraw);
1902 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1904 DeleteObject(infoPtr->hBoldFont);
1905 DeleteObject(infoPtr->hUnderlineFont);
1906 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1907 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1909 if (!infoPtr->bHeightSet)
1910 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1912 if (uHeight != infoPtr->uItemHeight)
1913 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1915 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1917 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1918 TREEVIEW_UpdateScrollBars(infoPtr);
1921 TREEVIEW_Invalidate(infoPtr, NULL);
1928 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1931 return (LRESULT)infoPtr->clrLine;
1935 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1937 COLORREF prevColor = infoPtr->clrLine;
1940 infoPtr->clrLine = color;
1941 return (LRESULT)prevColor;
1946 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1949 return (LRESULT)infoPtr->clrText;
1953 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1955 COLORREF prevColor = infoPtr->clrText;
1958 infoPtr->clrText = color;
1960 if (infoPtr->clrText != prevColor)
1961 TREEVIEW_Invalidate(infoPtr, NULL);
1963 return (LRESULT)prevColor;
1968 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1971 return (LRESULT)infoPtr->clrBk;
1975 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1977 COLORREF prevColor = infoPtr->clrBk;
1980 infoPtr->clrBk = newColor;
1982 if (newColor != prevColor)
1983 TREEVIEW_Invalidate(infoPtr, NULL);
1985 return (LRESULT)prevColor;
1990 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1993 return (LRESULT)infoPtr->clrInsertMark;
1997 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1999 COLORREF prevColor = infoPtr->clrInsertMark;
2001 TRACE("%x\n", color);
2002 infoPtr->clrInsertMark = color;
2004 return (LRESULT)prevColor;
2009 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2011 TRACE("%d %p\n", wParam, item);
2013 if (!TREEVIEW_ValidItem(infoPtr, item))
2016 infoPtr->insertBeforeorAfter = wParam;
2017 infoPtr->insertMarkItem = item;
2019 TREEVIEW_Invalidate(infoPtr, NULL);
2025 /************************************************************************
2026 * Some serious braindamage here. lParam is a pointer to both the
2027 * input HTREEITEM and the output RECT.
2030 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2032 TREEVIEW_ITEM *item;
2033 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2041 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2045 * If wParam is TRUE return the text size otherwise return
2046 * the whole item size
2050 /* Windows does not send TVN_GETDISPINFO here. */
2052 lpRect->top = item->rect.top;
2053 lpRect->bottom = item->rect.bottom;
2055 lpRect->left = item->textOffset;
2056 if (!item->textWidth)
2057 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2059 lpRect->right = item->textOffset + item->textWidth + 4;
2063 *lpRect = item->rect;
2066 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2071 static inline LRESULT
2072 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2074 /* Surprise! This does not take integral height into account. */
2075 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2076 return infoPtr->clientHeight / infoPtr->uItemHeight;
2081 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2083 TREEVIEW_ITEM *item = tvItem->hItem;
2085 if (!TREEVIEW_ValidItem(infoPtr, item))
2087 if (!item) return FALSE;
2089 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2090 infoPtr = item->infoPtr;
2091 if (!TREEVIEW_ValidItem(infoPtr, item)) return FALSE;
2094 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2096 if (tvItem->mask & TVIF_CHILDREN)
2098 if (item->cChildren==I_CHILDRENCALLBACK)
2099 FIXME("I_CHILDRENCALLBACK not supported\n");
2100 tvItem->cChildren = item->cChildren;
2103 if (tvItem->mask & TVIF_HANDLE)
2104 tvItem->hItem = item;
2106 if (tvItem->mask & TVIF_IMAGE)
2107 tvItem->iImage = item->iImage;
2109 if (tvItem->mask & TVIF_INTEGRAL)
2110 tvItem->iIntegral = item->iIntegral;
2112 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2113 tvItem->lParam = item->lParam;
2115 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2116 tvItem->iSelectedImage = item->iSelectedImage;
2118 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2119 tvItem->iExpandedImage = item->iExpandedImage;
2121 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2122 tvItem->state = item->state;
2124 if (tvItem->mask & TVIF_TEXT)
2126 if (item->pszText == NULL)
2128 if (tvItem->cchTextMax > 0)
2129 tvItem->pszText[0] = '\0';
2133 if (item->pszText == LPSTR_TEXTCALLBACKW)
2135 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2136 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2140 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2145 if (item->pszText == LPSTR_TEXTCALLBACKW)
2147 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2148 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2152 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2153 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2158 if (tvItem->mask & TVIF_STATEEX)
2160 FIXME("Extended item state not supported, returning 0.\n");
2161 tvItem->uStateEx = 0;
2164 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2165 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2170 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2171 * which is wrong. */
2173 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2175 TREEVIEW_ITEM *item;
2176 TREEVIEW_ITEM originalItem;
2178 item = tvItem->hItem;
2180 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2183 if (!TREEVIEW_ValidItem(infoPtr, item))
2186 /* store the original item values */
2187 originalItem = *item;
2189 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2192 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2193 if ((tvItem->mask & TVIF_TEXT
2194 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2197 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2198 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2201 if (tvItem->mask != 0 && ISVISIBLE(item))
2203 /* The refresh updates everything, but we can't wait until then. */
2204 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2206 /* if any of the item's values changed and it's not a callback, redraw the item */
2207 if (item_changed(&originalItem, item, tvItem))
2209 if (tvItem->mask & TVIF_INTEGRAL)
2211 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2212 TREEVIEW_UpdateScrollBars(infoPtr);
2214 TREEVIEW_Invalidate(infoPtr, NULL);
2218 TREEVIEW_UpdateScrollBars(infoPtr);
2219 TREEVIEW_Invalidate(infoPtr, item);
2228 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2232 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2235 return (item->state & mask);
2239 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2241 TREEVIEW_ITEM *retval;
2245 /* handle all the global data here */
2248 case TVGN_CHILD: /* Special case: child of 0 is root */
2253 retval = infoPtr->root->firstChild;
2257 retval = infoPtr->selectedItem;
2260 case TVGN_FIRSTVISIBLE:
2261 retval = infoPtr->firstVisible;
2264 case TVGN_DROPHILITE:
2265 retval = infoPtr->dropItem;
2268 case TVGN_LASTVISIBLE:
2269 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2275 TRACE("flags:%x, returns %p\n", which, retval);
2276 return (LRESULT)retval;
2279 if (item == TVI_ROOT) item = infoPtr->root;
2281 if (!TREEVIEW_ValidItem(infoPtr, item))
2287 retval = item->nextSibling;
2290 retval = item->prevSibling;
2293 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2296 retval = item->firstChild;
2298 case TVGN_NEXTVISIBLE:
2299 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2301 case TVGN_PREVIOUSVISIBLE:
2302 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2305 TRACE("Unknown msg %x,item %p\n", which, item);
2309 TRACE("flags:%x, item %p;returns %p\n", which, item, retval);
2310 return (LRESULT)retval;
2315 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2317 TRACE(" %d\n", infoPtr->uNumItems);
2318 return (LRESULT)infoPtr->uNumItems;
2322 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2324 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2326 static const unsigned int state_table[] = { 0, 2, 1 };
2330 state = STATEIMAGEINDEX(item->state);
2331 TRACE("state:%x\n", state);
2332 item->state &= ~TVIS_STATEIMAGEMASK;
2335 state = state_table[state];
2337 item->state |= INDEXTOSTATEIMAGEMASK(state);
2339 TRACE("state:%x\n", state);
2340 TREEVIEW_Invalidate(infoPtr, item);
2345 /* Painting *************************************************************/
2347 /* Draw the lines and expand button for an item. Also draws one section
2348 * of the line from item's parent to item's parent's next sibling. */
2350 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2352 LONG centerx, centery;
2353 BOOL lar = ((infoPtr->dwStyle
2354 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2357 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2359 if (!lar && item->iLevel == 0)
2362 hbr = CreateSolidBrush(clrBk);
2363 hbrOld = SelectObject(hdc, hbr);
2365 centerx = (item->linesOffset + item->stateOffset) / 2;
2366 centery = (item->rect.top + item->rect.bottom) / 2;
2368 if (infoPtr->dwStyle & TVS_HASLINES)
2370 HPEN hOldPen, hNewPen;
2374 /* Get a dotted grey pen */
2375 lb.lbStyle = BS_SOLID;
2376 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2377 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2378 hOldPen = SelectObject(hdc, hNewPen);
2380 /* Make sure the center is on a dot (using +2 instead
2381 * of +1 gives us pixel-by-pixel compat with native) */
2382 centery = (centery + 2) & ~1;
2384 MoveToEx(hdc, item->stateOffset, centery, NULL);
2385 LineTo(hdc, centerx - 1, centery);
2387 if (item->prevSibling || item->parent != infoPtr->root)
2389 MoveToEx(hdc, centerx, item->rect.top, NULL);
2390 LineTo(hdc, centerx, centery);
2393 if (item->nextSibling)
2395 MoveToEx(hdc, centerx, centery, NULL);
2396 LineTo(hdc, centerx, item->rect.bottom + 1);
2399 /* Draw the line from our parent to its next sibling. */
2400 parent = item->parent;
2401 while (parent != infoPtr->root)
2403 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2405 if (parent->nextSibling
2406 /* skip top-levels unless TVS_LINESATROOT */
2407 && parent->stateOffset > parent->linesOffset)
2409 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2410 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2413 parent = parent->parent;
2416 SelectObject(hdc, hOldPen);
2417 DeleteObject(hNewPen);
2421 * Display the (+/-) signs
2424 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2426 if (item->cChildren)
2428 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2431 RECT glyphRect = item->rect;
2432 glyphRect.left = item->linesOffset;
2433 glyphRect.right = item->stateOffset;
2434 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2435 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2440 LONG height = item->rect.bottom - item->rect.top;
2441 LONG width = item->stateOffset - item->linesOffset;
2442 LONG rectsize = min(height, width) / 4;
2443 /* plussize = ceil(rectsize * 3/4) */
2444 LONG plussize = (rectsize + 1) * 3 / 4;
2446 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2447 HPEN old_pen = SelectObject(hdc, new_pen);
2449 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2450 centerx + rectsize + 2, centery + rectsize + 2);
2452 SelectObject(hdc, old_pen);
2453 DeleteObject(new_pen);
2455 /* draw +/- signs with current text color */
2456 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2457 old_pen = SelectObject(hdc, new_pen);
2459 if (height < 18 || width < 18)
2461 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2462 LineTo(hdc, centerx + plussize, centery);
2464 if (!(item->state & TVIS_EXPANDED) ||
2465 (item->state & TVIS_EXPANDPARTIAL))
2467 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2468 LineTo(hdc, centerx, centery + plussize);
2473 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2474 centerx + plussize, centery + 2);
2476 if (!(item->state & TVIS_EXPANDED) ||
2477 (item->state & TVIS_EXPANDPARTIAL))
2479 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2480 centerx + 2, centery + plussize);
2481 SetPixel(hdc, centerx - 1, centery, clrBk);
2482 SetPixel(hdc, centerx + 1, centery, clrBk);
2486 SelectObject(hdc, old_pen);
2487 DeleteObject(new_pen);
2491 SelectObject(hdc, hbrOld);
2496 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2500 COLORREF oldTextColor, oldTextBkColor;
2502 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2503 NMTVCUSTOMDRAW nmcdhdr;
2505 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2507 /* - If item is drop target or it is selected and window is in focus -
2508 * use blue background (COLOR_HIGHLIGHT).
2509 * - If item is selected, window is not in focus, but it has style
2510 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2511 * - Otherwise - use background color
2513 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2514 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2515 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2517 if ((item->state & TVIS_DROPHILITED) || inFocus)
2519 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2520 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2524 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2525 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2530 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2531 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2532 nmcdhdr.clrText = comctl32_color.clrHighlight;
2534 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2537 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2539 /* The custom draw handler can query the text rectangle,
2541 /* should already be known, set to 0 when changed */
2542 if (!item->textWidth)
2543 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2547 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2549 cditem = TREEVIEW_SendCustomDrawItemNotify
2550 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2551 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2553 if (cditem & CDRF_SKIPDEFAULT)
2555 SelectObject(hdc, hOldFont);
2560 if (cditem & CDRF_NEWFONT)
2561 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2563 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2565 /* Set colors. Custom draw handler can change these so we do this after it. */
2566 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2567 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2569 centery = (item->rect.top + item->rect.bottom) / 2;
2572 * Display the images associated with this item
2577 /* State images are displayed to the left of the Normal image
2578 * image number is in state; zero should be `display no image'.
2580 imageIndex = STATEIMAGEINDEX(item->state);
2582 if (infoPtr->himlState && imageIndex)
2584 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2586 centery - infoPtr->stateImageHeight / 2,
2590 /* Now, draw the normal image; can be either selected,
2591 * non-selected or expanded image.
2594 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2596 /* The item is currently selected */
2597 imageIndex = item->iSelectedImage;
2599 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2601 /* The item is currently not selected but expanded */
2602 imageIndex = item->iExpandedImage;
2606 /* The item is not selected and not expanded */
2607 imageIndex = item->iImage;
2610 if (infoPtr->himlNormal)
2612 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2614 style |= item->state & TVIS_OVERLAYMASK;
2616 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2617 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2618 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2625 * Display the text associated with this item
2628 /* Don't paint item's text if it's being edited */
2629 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2637 rcText.top = item->rect.top;
2638 rcText.bottom = item->rect.bottom;
2639 rcText.left = item->textOffset;
2640 rcText.right = rcText.left + item->textWidth + 4;
2642 TRACE("drawing text %s at (%s)\n",
2643 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2646 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2648 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2649 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2650 ETO_CLIPPED | ETO_OPAQUE,
2653 lstrlenW(item->pszText),
2655 SetTextAlign(hdc, align);
2657 /* Draw focus box around the selected item */
2658 if ((item == infoPtr->selectedItem) && inFocus)
2660 DrawFocusRect(hdc,&rcText);
2665 /* Draw insertion mark if necessary */
2667 if (infoPtr->insertMarkItem)
2668 TRACE("item:%d,mark:%p\n",
2669 TREEVIEW_GetItemIndex(infoPtr, item),
2670 infoPtr->insertMarkItem);
2672 if (item == infoPtr->insertMarkItem)
2674 HPEN hNewPen, hOldPen;
2678 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2679 hOldPen = SelectObject(hdc, hNewPen);
2681 if (infoPtr->insertBeforeorAfter)
2682 offset = item->rect.bottom - 1;
2684 offset = item->rect.top + 1;
2686 left = item->textOffset - 2;
2687 right = item->textOffset + item->textWidth + 2;
2689 MoveToEx(hdc, left, offset - 3, NULL);
2690 LineTo(hdc, left, offset + 4);
2692 MoveToEx(hdc, left, offset, NULL);
2693 LineTo(hdc, right + 1, offset);
2695 MoveToEx(hdc, right, offset + 3, NULL);
2696 LineTo(hdc, right, offset - 4);
2698 SelectObject(hdc, hOldPen);
2699 DeleteObject(hNewPen);
2702 if (cditem & CDRF_NOTIFYPOSTPAINT)
2704 cditem = TREEVIEW_SendCustomDrawItemNotify
2705 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2706 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2709 /* Restore the hdc state */
2710 SetTextColor(hdc, oldTextColor);
2711 SetBkColor(hdc, oldTextBkColor);
2712 SelectObject(hdc, hOldFont);
2715 /* Computes treeHeight and treeWidth and updates the scroll bars.
2718 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2720 TREEVIEW_ITEM *item;
2721 HWND hwnd = infoPtr->hwnd;
2725 LONG scrollX = infoPtr->scrollX;
2727 infoPtr->treeWidth = 0;
2728 infoPtr->treeHeight = 0;
2730 /* We iterate through all visible items in order to get the tree height
2732 item = infoPtr->root->firstChild;
2734 while (item != NULL)
2736 if (ISVISIBLE(item))
2738 /* actually we draw text at textOffset + 2 */
2739 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2740 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2742 /* This is scroll-adjusted, but we fix this below. */
2743 infoPtr->treeHeight = item->rect.bottom;
2746 item = TREEVIEW_GetNextListItem(infoPtr, item);
2749 /* Fix the scroll adjusted treeHeight and treeWidth. */
2750 if (infoPtr->root->firstChild)
2751 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2753 infoPtr->treeWidth += infoPtr->scrollX;
2755 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2757 /* Adding one scroll bar may take up enough space that it forces us
2758 * to add the other as well. */
2759 if (infoPtr->treeHeight > infoPtr->clientHeight)
2763 if (infoPtr->treeWidth
2764 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2767 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2770 if (!vert && horz && infoPtr->treeHeight
2771 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2774 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2776 si.cbSize = sizeof(SCROLLINFO);
2777 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2782 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2783 if ( si.nPage && NULL != infoPtr->firstVisible)
2785 si.nPos = infoPtr->firstVisible->visibleOrder;
2786 si.nMax = infoPtr->maxVisibleOrder - 1;
2788 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2790 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2791 ShowScrollBar(hwnd, SB_VERT, TRUE);
2792 infoPtr->uInternalStatus |= TV_VSCROLL;
2796 if (infoPtr->uInternalStatus & TV_VSCROLL)
2797 ShowScrollBar(hwnd, SB_VERT, FALSE);
2798 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2803 if (infoPtr->uInternalStatus & TV_VSCROLL)
2804 ShowScrollBar(hwnd, SB_VERT, FALSE);
2805 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2810 si.nPage = infoPtr->clientWidth;
2811 si.nPos = infoPtr->scrollX;
2812 si.nMax = infoPtr->treeWidth - 1;
2814 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2816 si.nPos = si.nMax - max( si.nPage-1, 0 );
2820 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2821 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2822 infoPtr->uInternalStatus |= TV_HSCROLL;
2824 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2825 TREEVIEW_HScroll(infoPtr,
2826 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2830 if (infoPtr->uInternalStatus & TV_HSCROLL)
2831 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2832 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2835 if (infoPtr->scrollX != 0)
2837 TREEVIEW_HScroll(infoPtr,
2838 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2843 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2847 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2850 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2852 hBrush = CreateSolidBrush(clrBk);
2853 FillRect(hdc, rc, hBrush);
2854 DeleteObject(hBrush);
2857 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2859 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2863 TRACE("%p\n", infoPtr);
2865 GetClientRect(infoPtr->hwnd, &rect);
2866 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2872 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2874 HWND hwnd = infoPtr->hwnd;
2876 TREEVIEW_ITEM *item;
2878 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2880 TRACE("empty window\n");
2884 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2887 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2889 ReleaseDC(hwnd, hdc);
2893 for (item = infoPtr->root->firstChild;
2895 item = TREEVIEW_GetNextListItem(infoPtr, item))
2897 if (ISVISIBLE(item))
2899 /* Avoid unneeded calculations */
2900 if (item->rect.top > rect.bottom)
2902 if (item->rect.bottom < rect.top)
2905 TREEVIEW_DrawItem(infoPtr, hdc, item);
2909 TREEVIEW_UpdateScrollBars(infoPtr);
2911 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2913 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2917 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2919 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2923 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2926 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2928 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2932 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2938 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2943 GetClientRect(infoPtr->hwnd, &rc);
2944 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2948 hdc = BeginPaint(infoPtr->hwnd, &ps);
2951 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2954 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2955 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2958 EndPaint(infoPtr->hwnd, &ps);
2964 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2966 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2968 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2971 if (options & PRF_ERASEBKGND)
2972 TREEVIEW_EraseBackground(infoPtr, hdc);
2974 if (options & PRF_CLIENT)
2977 GetClientRect(infoPtr->hwnd, &rc);
2978 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2984 /* Sorting **************************************************************/
2986 /***************************************************************************
2987 * Forward the DPA local callback to the treeview owner callback
2990 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2991 const TVSORTCB *pCallBackSort)
2993 /* Forward the call to the client-defined callback */
2994 return pCallBackSort->lpfnCompare(first->lParam,
2996 pCallBackSort->lParam);
2999 /***************************************************************************
3000 * Treeview native sort routine: sort on item text.
3003 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3004 const TREEVIEW_INFO *infoPtr)
3006 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3007 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3009 if(first->pszText && second->pszText)
3010 return lstrcmpiW(first->pszText, second->pszText);
3011 else if(first->pszText)
3013 else if(second->pszText)
3019 /* Returns the number of physical children belonging to item. */
3021 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3026 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3032 /* Returns a DPA containing a pointer to each physical child of item in
3033 * sibling order. If item has no children, an empty DPA is returned. */
3035 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3039 HDPA list = DPA_Create(8);
3040 if (list == 0) return NULL;
3042 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3044 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3054 /***************************************************************************
3055 * Setup the treeview structure with regards of the sort method
3056 * and sort the children of the TV item specified in lParam
3057 * fRecurse: currently unused. Should be zero.
3058 * parent: if pSort!=NULL, should equal pSort->hParent.
3059 * otherwise, item which child items are to be sorted.
3060 * pSort: sort method info. if NULL, sort on item text.
3061 * if non-NULL, sort on item's lParam content, and let the
3062 * application decide what that means. See also TVM_SORTCHILDRENCB.
3066 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3070 PFNDPACOMPARE pfnCompare;
3073 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3074 if (parent == TVI_ROOT || parent == NULL)
3075 parent = infoPtr->root;
3077 /* Check for a valid handle to the parent item */
3078 if (!TREEVIEW_ValidItem(infoPtr, parent))
3080 ERR("invalid item hParent=%p\n", parent);
3086 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3087 lpCompare = (LPARAM)pSort;
3091 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3092 lpCompare = (LPARAM)infoPtr;
3095 cChildren = TREEVIEW_CountChildren(parent);
3097 /* Make sure there is something to sort */
3100 /* TREEVIEW_ITEM rechaining */
3103 HTREEITEM nextItem = 0;
3104 HTREEITEM prevItem = 0;
3106 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3108 if (sortList == NULL)
3111 /* let DPA sort the list */
3112 DPA_Sort(sortList, pfnCompare, lpCompare);
3114 /* The order of DPA entries has been changed, so fixup the
3115 * nextSibling and prevSibling pointers. */
3117 item = DPA_GetPtr(sortList, count++);
3118 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3120 /* link the two current item together */
3121 item->nextSibling = nextItem;
3122 nextItem->prevSibling = item;
3124 if (prevItem == NULL)
3126 /* this is the first item, update the parent */
3127 parent->firstChild = item;
3128 item->prevSibling = NULL;
3132 /* fix the back chaining */
3133 item->prevSibling = prevItem;
3136 /* get ready for the next one */
3141 /* the last item is pointed to by item and never has a sibling */
3142 item->nextSibling = NULL;
3143 parent->lastChild = item;
3145 DPA_Destroy(sortList);
3147 TREEVIEW_VerifyTree(infoPtr);
3149 if (parent->state & TVIS_EXPANDED)
3151 int visOrder = infoPtr->firstVisible->visibleOrder;
3153 if (parent == infoPtr->root)
3154 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3156 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3158 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3160 TREEVIEW_ITEM *item;
3162 for (item = infoPtr->root->firstChild; item != NULL;
3163 item = TREEVIEW_GetNextListItem(infoPtr, item))
3165 if (item->visibleOrder == visOrder)
3169 if (!item) item = parent->firstChild;
3170 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3173 TREEVIEW_Invalidate(infoPtr, NULL);
3182 /***************************************************************************
3183 * Setup the treeview structure with regards of the sort method
3184 * and sort the children of the TV item specified in lParam
3187 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3189 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3193 /***************************************************************************
3194 * Sort the children of the TV item specified in lParam.
3197 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3199 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3203 /* Expansion/Collapse ***************************************************/
3206 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3209 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3210 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3211 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3216 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3219 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3220 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3221 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3226 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3227 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3229 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3230 BOOL bRemoveChildren, BOOL bUser)
3232 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3233 BOOL bSetSelection, bSetFirstVisible;
3235 LONG scrollDist = 0;
3236 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3238 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3240 if (!(item->state & TVIS_EXPANDED))
3243 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3244 TREEVIEW_SendExpanding(infoPtr, item, action);
3246 if (item->firstChild == NULL)
3249 item->state &= ~TVIS_EXPANDED;
3251 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3252 TREEVIEW_SendExpanded(infoPtr, item, action);
3254 bSetSelection = (infoPtr->selectedItem != NULL
3255 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3257 bSetFirstVisible = (infoPtr->firstVisible != NULL
3258 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3263 if (tmpItem->nextSibling)
3265 nextItem = tmpItem->nextSibling;
3268 tmpItem = tmpItem->parent;
3272 scrollDist = nextItem->rect.top;
3274 if (bRemoveChildren)
3276 INT old_cChildren = item->cChildren;
3277 TRACE("TVE_COLLAPSERESET\n");
3278 item->state &= ~TVIS_EXPANDEDONCE;
3279 TREEVIEW_RemoveAllChildren(infoPtr, item);
3280 item->cChildren = old_cChildren;
3283 if (item->firstChild)
3285 TREEVIEW_ITEM *i, *sibling;
3287 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3289 for (i = item->firstChild; i != sibling;
3290 i = TREEVIEW_GetNextListItem(infoPtr, i))
3292 i->visibleOrder = -1;
3296 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3299 scrollDist = -(scrollDist - nextItem->rect.top);
3303 /* Don't call DoSelectItem, it sends notifications. */
3304 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3305 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3306 item->state |= TVIS_SELECTED;
3307 infoPtr->selectedItem = item;
3310 TREEVIEW_UpdateScrollBars(infoPtr);
3312 scrollRect.left = 0;
3313 scrollRect.right = infoPtr->clientWidth;
3314 scrollRect.bottom = infoPtr->clientHeight;
3318 scrollRect.top = nextItem->rect.top;
3320 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3321 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3322 TREEVIEW_Invalidate(infoPtr, item);
3324 scrollRect.top = item->rect.top;
3325 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3328 TREEVIEW_SetFirstVisible(infoPtr,
3329 bSetFirstVisible ? item : infoPtr->firstVisible,
3336 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3337 BOOL partial, BOOL user)
3340 LONG orgNextTop = 0;
3342 TREEVIEW_ITEM *nextItem, *tmpItem;
3343 BOOL sendsNotifications;
3345 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, item, partial, user);
3347 if (item->state & TVIS_EXPANDED)
3350 tmpItem = item; nextItem = NULL;
3353 if (tmpItem->nextSibling)
3355 nextItem = tmpItem->nextSibling;
3358 tmpItem = tmpItem->parent;
3362 orgNextTop = nextItem->rect.top;
3364 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3366 sendsNotifications = user || ((item->cChildren != 0) &&
3367 !(item->state & TVIS_EXPANDEDONCE));
3368 if (sendsNotifications)
3370 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3372 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3376 if (!item->firstChild)
3379 item->state |= TVIS_EXPANDED;
3382 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3384 if (ISVISIBLE(item))
3386 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3387 TREEVIEW_UpdateSubTree(infoPtr, item);
3388 TREEVIEW_UpdateScrollBars(infoPtr);
3390 scrollRect.left = 0;
3391 scrollRect.bottom = infoPtr->treeHeight;
3392 scrollRect.right = infoPtr->clientWidth;
3395 scrollDist = nextItem->rect.top - orgNextTop;
3396 scrollRect.top = orgNextTop;
3398 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3399 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3400 TREEVIEW_Invalidate (infoPtr, item);
3402 scrollRect.top = item->rect.top;
3403 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3406 /* Scroll up so that as many children as possible are visible.
3407 * This fails when expanding causes an HScroll bar to appear, but we
3408 * don't know that yet, so the last item is obscured. */
3409 if (item->firstChild != NULL)
3411 int nChildren = item->lastChild->visibleOrder
3412 - item->firstChild->visibleOrder + 1;
3414 int visible_pos = item->visibleOrder
3415 - infoPtr->firstVisible->visibleOrder;
3417 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3419 if (visible_pos > 0 && nChildren > rows_below)
3421 int scroll = nChildren - rows_below;
3423 if (scroll > visible_pos)
3424 scroll = visible_pos;
3428 TREEVIEW_ITEM *newFirstVisible
3429 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3433 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3439 if (sendsNotifications) {
3440 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3441 item->state |= TVIS_EXPANDEDONCE;
3447 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3448 to mouse messages and TVM_SELECTITEM.
3450 selection - previously selected item, used to collapse a part of a tree
3451 item - new selected item
3453 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3454 HTREEITEM selection, HTREEITEM item)
3456 TREEVIEW_ITEM *SelItem;
3458 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3460 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3463 * Close the previous selection all the way to the root
3464 * as long as the new selection is not a child
3466 if(selection && (selection != item))
3468 BOOL closeit = TRUE;
3471 /* determine if the hitItem is a child of the currently selected item */
3472 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3473 (SelItem->parent != infoPtr->root))
3475 closeit = (SelItem != selection);
3476 SelItem = SelItem->parent;
3481 if(TREEVIEW_ValidItem(infoPtr, selection))
3482 SelItem = selection;
3484 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3485 SelItem->parent != infoPtr->root)
3487 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3488 SelItem = SelItem->parent;
3494 * Expand the current item
3496 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3500 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3502 TRACE("item=%p, user=%d\n", item, user);
3504 if (item->state & TVIS_EXPANDED)
3505 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3507 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3511 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3513 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3515 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3517 if (TREEVIEW_HasChildren(infoPtr, item))
3518 TREEVIEW_ExpandAll(infoPtr, item);
3522 /* Note:If the specified item is the child of a collapsed parent item,
3523 the parent's list of child items is (recursively) expanded to reveal the
3524 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3525 know if it also applies here.
3529 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3531 if (!TREEVIEW_ValidItem(infoPtr, item))
3534 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3535 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3538 switch (flag & TVE_TOGGLE)
3541 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3545 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3549 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3556 /* Hit-Testing **********************************************************/
3558 static TREEVIEW_ITEM *
3559 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3561 TREEVIEW_ITEM *item;
3564 if (!infoPtr->firstVisible)
3567 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3569 for (item = infoPtr->firstVisible; item != NULL;
3570 item = TREEVIEW_GetNextListItem(infoPtr, item))
3572 if (row >= item->visibleOrder
3573 && row < item->visibleOrder + item->iIntegral)
3581 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3583 TREEVIEW_ITEM *item;
3589 GetClientRect(infoPtr->hwnd, &rect);
3596 status |= TVHT_TOLEFT;
3598 else if (x > rect.right)
3600 status |= TVHT_TORIGHT;
3605 status |= TVHT_ABOVE;
3607 else if (y > rect.bottom)
3609 status |= TVHT_BELOW;
3614 lpht->flags = status;
3618 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3621 lpht->flags = TVHT_NOWHERE;
3625 if (x >= item->textOffset + item->textWidth)
3627 lpht->flags = TVHT_ONITEMRIGHT;
3629 else if (x >= item->textOffset)
3631 lpht->flags = TVHT_ONITEMLABEL;
3633 else if (x >= item->imageOffset)
3635 lpht->flags = TVHT_ONITEMICON;
3637 else if (x >= item->stateOffset)
3639 lpht->flags = TVHT_ONITEMSTATEICON;
3641 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3643 lpht->flags = TVHT_ONITEMBUTTON;
3647 lpht->flags = TVHT_ONITEMINDENT;
3651 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3653 return (LRESULT)item;
3656 /* Item Label Editing ***************************************************/
3659 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3661 return (LRESULT)infoPtr->hwndEdit;
3664 static LRESULT CALLBACK
3665 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3667 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3668 BOOL bCancel = FALSE;
3674 TRACE("WM_PAINT start\n");
3675 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3677 TRACE("WM_PAINT done\n");
3681 if (infoPtr->bIgnoreEditKillFocus)
3687 WNDPROC editProc = infoPtr->wpEditOrig;
3688 infoPtr->wpEditOrig = 0;
3689 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3690 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3694 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3697 if (wParam == VK_ESCAPE)
3702 else if (wParam == VK_RETURN)
3709 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3712 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3713 /* eg. Using a messagebox */
3715 infoPtr->bIgnoreEditKillFocus = TRUE;
3716 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3717 infoPtr->bIgnoreEditKillFocus = FALSE;
3723 /* should handle edit control messages here */
3726 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3728 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3730 switch (HIWORD(wParam))
3735 * Adjust the edit window size
3738 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3739 HDC hdc = GetDC(infoPtr->hwndEdit);
3741 HFONT hFont, hOldFont = 0;
3743 TRACE("edit=%p\n", infoPtr->hwndEdit);
3745 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3747 infoPtr->bLabelChanged = TRUE;
3749 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3751 /* Select font to get the right dimension of the string */
3752 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3756 hOldFont = SelectObject(hdc, hFont);
3759 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3761 TEXTMETRICW textMetric;
3763 /* Add Extra spacing for the next character */
3764 GetTextMetricsW(hdc, &textMetric);
3765 sz.cx += (textMetric.tmMaxCharWidth * 2);
3767 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3769 infoPtr->clientWidth - editItem->textOffset + 2);
3771 SetWindowPos(infoPtr->hwndEdit,
3776 editItem->rect.bottom - editItem->rect.top + 3,
3777 SWP_NOMOVE | SWP_DRAWFRAME);
3782 SelectObject(hdc, hOldFont);
3785 ReleaseDC(infoPtr->hwnd, hdc);
3789 /* apparently we should respect passed handle value */
3790 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3792 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3796 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3803 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3805 HWND hwnd = infoPtr->hwnd;
3808 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3811 TEXTMETRICW textMetric;
3813 TRACE("%p %p\n", hwnd, hItem);
3814 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3817 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3820 if (infoPtr->hwndEdit)
3821 return infoPtr->hwndEdit;
3823 infoPtr->bLabelChanged = FALSE;
3825 /* make edit item visible */
3826 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3828 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3831 /* Select the font to get appropriate metric dimensions */
3832 if (infoPtr->hFont != 0)
3834 hOldFont = SelectObject(hdc, infoPtr->hFont);
3837 /* Get string length in pixels */
3839 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3842 GetTextExtentPoint32A(hdc, "", 0, &sz);
3844 /* Add Extra spacing for the next character */
3845 GetTextMetricsW(hdc, &textMetric);
3846 sz.cx += (textMetric.tmMaxCharWidth * 2);
3848 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3849 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3851 if (infoPtr->hFont != 0)
3853 SelectObject(hdc, hOldFont);
3856 ReleaseDC(hwnd, hdc);
3858 infoPtr->editItem = hItem;
3860 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3863 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3864 WS_CLIPSIBLINGS | ES_WANTRETURN |
3865 ES_LEFT, hItem->textOffset - 2,
3866 hItem->rect.top - 1, sz.cx + 3,
3867 hItem->rect.bottom -
3868 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3869 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3871 infoPtr->hwndEdit = hwndEdit;
3873 /* Get a 2D border. */
3874 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3875 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3876 SetWindowLongW(hwndEdit, GWL_STYLE,
3877 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3879 SendMessageW(hwndEdit, WM_SETFONT,
3880 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3882 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3884 TREEVIEW_Edit_SubclassProc);
3886 SetWindowTextW(hwndEdit, hItem->pszText);
3888 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3890 DestroyWindow(hwndEdit);
3891 infoPtr->hwndEdit = 0;
3892 infoPtr->editItem = NULL;
3897 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3898 ShowWindow(hwndEdit, SW_SHOW);
3905 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3907 HWND hwnd = infoPtr->hwnd;
3908 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3911 WCHAR tmpText[1024] = { '\0' };
3912 WCHAR *newText = tmpText;
3915 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3917 tvdi.hdr.hwndFrom = hwnd;
3918 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3919 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3921 tvdi.item.hItem = editedItem;
3922 tvdi.item.state = editedItem->state;
3923 tvdi.item.lParam = editedItem->lParam;
3927 if (!infoPtr->bNtfUnicode)
3928 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3930 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3932 if (iLength >= 1023)
3934 ERR("Insufficient space to retrieve new item label\n");
3937 tvdi.item.mask = TVIF_TEXT;
3938 tvdi.item.pszText = tmpText;
3939 tvdi.item.cchTextMax = iLength + 1;
3943 tvdi.item.pszText = NULL;
3944 tvdi.item.cchTextMax = 0;
3947 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
3949 if (!bCancel && bCommit) /* Apply the changes */
3951 if (!infoPtr->bNtfUnicode)
3953 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3954 newText = Alloc(len * sizeof(WCHAR));
3955 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3959 if (strcmpW(newText, editedItem->pszText) != 0)
3961 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3964 ERR("OutOfMemory, cannot allocate space for label\n");
3965 if(newText != tmpText) Free(newText);
3966 DestroyWindow(infoPtr->hwndEdit);
3967 infoPtr->hwndEdit = 0;
3968 infoPtr->editItem = NULL;
3973 editedItem->pszText = ptr;
3974 editedItem->cchTextMax = iLength + 1;
3975 strcpyW(editedItem->pszText, newText);
3976 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3979 if(newText != tmpText) Free(newText);
3982 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3983 DestroyWindow(infoPtr->hwndEdit);
3984 infoPtr->hwndEdit = 0;
3985 infoPtr->editItem = NULL;
3990 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3992 if (wParam != TV_EDIT_TIMER)
3994 ERR("got unknown timer\n");
3998 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3999 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4001 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4007 /* Mouse Tracking/Drag **************************************************/
4009 /***************************************************************************
4010 * This is quite unusual piece of code, but that's how it's implemented in
4014 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4016 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4017 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4021 r.top = pt.y - cyDrag;
4022 r.left = pt.x - cxDrag;
4023 r.bottom = pt.y + cyDrag;
4024 r.right = pt.x + cxDrag;
4026 SetCapture(infoPtr->hwnd);
4030 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4032 if (msg.message == WM_MOUSEMOVE)
4034 pt.x = (short)LOWORD(msg.lParam);
4035 pt.y = (short)HIWORD(msg.lParam);
4036 if (PtInRect(&r, pt))
4044 else if (msg.message >= WM_LBUTTONDOWN &&
4045 msg.message <= WM_RBUTTONDBLCLK)
4050 DispatchMessageW(&msg);
4053 if (GetCapture() != infoPtr->hwnd)
4063 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4065 TREEVIEW_ITEM *item;
4069 SetFocus(infoPtr->hwnd);
4071 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4073 /* If there is pending 'edit label' event - kill it now */
4074 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4077 hit.pt.x = (short)LOWORD(lParam);
4078 hit.pt.y = (short)HIWORD(lParam);
4080 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4083 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4085 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4089 case TVHT_ONITEMRIGHT:
4090 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4093 case TVHT_ONITEMINDENT:
4094 if (!(infoPtr->dwStyle & TVS_HASLINES))
4100 int level = hit.pt.x / infoPtr->uIndent;
4101 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4103 while (item->iLevel > level)
4105 item = item->parent;
4111 case TVHT_ONITEMLABEL:
4112 case TVHT_ONITEMICON:
4113 case TVHT_ONITEMBUTTON:
4114 TREEVIEW_Toggle(infoPtr, item, TRUE);
4117 case TVHT_ONITEMSTATEICON:
4118 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4119 TREEVIEW_ToggleItemState(infoPtr, item);
4121 TREEVIEW_Toggle(infoPtr, item, TRUE);
4130 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4132 HWND hwnd = infoPtr->hwnd;
4134 BOOL bTrack, bDoLabelEdit;
4136 /* If Edit control is active - kill it and return.
4137 * The best way to do it is to set focus to itself.
4138 * Edit control subclassed procedure will automatically call
4141 if (infoPtr->hwndEdit)
4147 ht.pt.x = (short)LOWORD(lParam);
4148 ht.pt.y = (short)HIWORD(lParam);
4150 TREEVIEW_HitTest(infoPtr, &ht);
4151 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4153 /* update focusedItem and redraw both items */
4154 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4156 infoPtr->focusedItem = ht.hItem;
4157 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4158 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4161 bTrack = (ht.flags & TVHT_ONITEM)
4162 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4165 * If the style allows editing and the node is already selected
4166 * and the click occurred on the item label...
4168 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4169 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4171 /* Send NM_CLICK right away */
4173 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4176 if (ht.flags & TVHT_ONITEMBUTTON)
4178 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4182 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4183 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4185 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4186 infoPtr->dropItem = ht.hItem;
4188 /* clean up focusedItem as we dragged and won't select this item */
4189 if(infoPtr->focusedItem)
4191 /* refresh the item that was focused */
4192 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4193 infoPtr->focusedItem = NULL;
4195 /* refresh the selected item to return the filled background */
4196 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4203 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4208 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4209 KillTimer(hwnd, TV_EDIT_TIMER);
4211 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4212 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4214 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4216 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4218 /* Select the current item */
4219 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4220 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4222 else if (ht.flags & TVHT_ONITEMSTATEICON)
4224 /* TVS_CHECKBOXES requires us to toggle the current state */
4225 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4226 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4236 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4240 if (infoPtr->hwndEdit)
4242 SetFocus(infoPtr->hwnd);
4246 ht.pt.x = (short)LOWORD(lParam);
4247 ht.pt.y = (short)HIWORD(lParam);
4249 TREEVIEW_HitTest(infoPtr, &ht);
4251 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4255 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4256 infoPtr->dropItem = ht.hItem;
4261 SetFocus(infoPtr->hwnd);
4262 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4264 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4265 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4266 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4274 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4276 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4280 HBITMAP hbmp, hOldbmp;
4287 if (!(infoPtr->himlNormal))
4290 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4293 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4295 hwtop = GetDesktopWindow();
4296 htopdc = GetDC(hwtop);
4297 hdc = CreateCompatibleDC(htopdc);
4299 hOldFont = SelectObject(hdc, infoPtr->hFont);
4301 if (dragItem->pszText)
4302 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4305 GetTextExtentPoint32A(hdc, "", 0, &size);
4307 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4308 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4309 hOldbmp = SelectObject(hdc, hbmp);
4311 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4316 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4317 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4321 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4322 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4325 /* draw item text */
4327 SetRect(&rc, cx, 0, size.cx, size.cy);
4329 if (dragItem->pszText)
4330 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4333 SelectObject(hdc, hOldFont);
4334 SelectObject(hdc, hOldbmp);
4336 ImageList_Add(infoPtr->dragList, hbmp, 0);
4340 ReleaseDC(hwtop, htopdc);
4342 return (LRESULT)infoPtr->dragList;
4345 /* Selection ************************************************************/
4348 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4351 TREEVIEW_ITEM *prevSelect;
4353 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4355 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4356 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4357 newSelect ? newSelect->state : 0);
4359 /* reset and redraw focusedItem if focusedItem was set so we don't */
4360 /* have to worry about the previously focused item when we set a new one */
4361 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4362 infoPtr->focusedItem = NULL;
4366 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4367 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4370 prevSelect = infoPtr->selectedItem;
4372 if (prevSelect == newSelect) {
4373 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4377 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4380 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4386 prevSelect->state &= ~TVIS_SELECTED;
4388 newSelect->state |= TVIS_SELECTED;
4390 infoPtr->selectedItem = newSelect;
4392 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4394 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4395 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4397 TREEVIEW_SendTreeviewNotify(infoPtr,
4400 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4405 case TVGN_DROPHILITE:
4406 prevSelect = infoPtr->dropItem;
4409 prevSelect->state &= ~TVIS_DROPHILITED;
4411 infoPtr->dropItem = newSelect;
4414 newSelect->state |= TVIS_DROPHILITED;
4416 TREEVIEW_Invalidate(infoPtr, prevSelect);
4417 TREEVIEW_Invalidate(infoPtr, newSelect);
4420 case TVGN_FIRSTVISIBLE:
4421 if (newSelect != NULL)
4423 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4424 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4425 TREEVIEW_Invalidate(infoPtr, NULL);
4430 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4434 /* FIXME: handle NM_KILLFOCUS etc */
4436 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4438 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4440 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4443 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4445 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4448 TREEVIEW_SingleExpand(infoPtr, selection, item);
4453 /*************************************************************************
4454 * TREEVIEW_ProcessLetterKeys
4456 * Processes keyboard messages generated by pressing the letter keys
4458 * What this does is perform a case insensitive search from the
4459 * current position with the following quirks:
4460 * - If two chars or more are pressed in quick succession we search
4461 * for the corresponding string (e.g. 'abc').
4462 * - If there is a delay we wipe away the current search string and
4463 * restart with just that char.
4464 * - If the user keeps pressing the same character, whether slowly or
4465 * fast, so that the search string is entirely composed of this
4466 * character ('aaaaa' for instance), then we search for first item
4467 * that starting with that character.
4468 * - If the user types the above character in quick succession, then
4469 * we must also search for the corresponding string ('aaaaa'), and
4470 * go to that string if there is a match.
4478 * - The current implementation has a list of characters it will
4479 * accept and it ignores everything else. In particular it will
4480 * ignore accentuated characters which seems to match what
4481 * Windows does. But I'm not sure it makes sense to follow
4483 * - We don't sound a beep when the search fails.
4484 * - The search should start from the focused item, not from the selected
4485 * item. One reason for this is to allow for multiple selections in trees.
4486 * But currently infoPtr->focusedItem does not seem very usable.
4490 * TREEVIEW_ProcessLetterKeys
4492 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4495 HTREEITEM endidx,idx;
4497 WCHAR buffer[MAX_PATH];
4498 DWORD timestamp,elapsed;
4500 /* simple parameter checking */
4501 if (!charCode || !keyData) return 0;
4503 /* only allow the valid WM_CHARs through */
4504 if (!isalnum(charCode) &&
4505 charCode != '.' && charCode != '`' && charCode != '!' &&
4506 charCode != '@' && charCode != '#' && charCode != '$' &&
4507 charCode != '%' && charCode != '^' && charCode != '&' &&
4508 charCode != '*' && charCode != '(' && charCode != ')' &&
4509 charCode != '-' && charCode != '_' && charCode != '+' &&
4510 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4511 charCode != '}' && charCode != '[' && charCode != '{' &&
4512 charCode != '/' && charCode != '?' && charCode != '>' &&
4513 charCode != '<' && charCode != ',' && charCode != '~')
4516 /* compute how much time elapsed since last keypress */
4517 timestamp = GetTickCount();
4518 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4519 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4521 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4524 /* update the search parameters */
4525 infoPtr->lastKeyPressTimestamp=timestamp;
4526 if (elapsed < KEY_DELAY) {
4527 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4528 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4530 if (infoPtr->charCode != charCode) {
4531 infoPtr->charCode=charCode=0;
4534 infoPtr->charCode=charCode;
4535 infoPtr->szSearchParam[0]=charCode;
4536 infoPtr->nSearchParamLength=1;
4537 /* Redundant with the 1 char string */
4541 /* and search from the current position */
4543 if (infoPtr->selectedItem != NULL) {
4544 endidx=infoPtr->selectedItem;
4545 /* if looking for single character match,
4546 * then we must always move forward
4548 if (infoPtr->nSearchParamLength == 1)
4549 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4554 idx=infoPtr->root->firstChild;
4557 /* At the end point, sort out wrapping */
4560 /* If endidx is null, stop at the last item (ie top to bottom) */
4564 /* Otherwise, start again at the very beginning */
4565 idx=infoPtr->root->firstChild;
4567 /* But if we are stopping on the first child, end now! */
4568 if (idx == endidx) break;
4572 ZeroMemory(&item, sizeof(item));
4573 item.mask = TVIF_TEXT;
4575 item.pszText = buffer;
4576 item.cchTextMax = sizeof(buffer);
4577 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4579 /* check for a match */
4580 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4583 } else if ( (charCode != 0) && (nItem == NULL) &&
4584 (nItem != infoPtr->selectedItem) &&
4585 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4586 /* This would work but we must keep looking for a longer match */
4589 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4590 } while (idx != endidx);
4592 if (nItem != NULL) {
4593 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4594 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4601 /* Scrolling ************************************************************/
4604 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4607 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4608 HTREEITEM newFirstVisible = NULL;
4609 int visible_pos = -1;
4611 if (!TREEVIEW_ValidItem(infoPtr, item))
4614 if (!ISVISIBLE(item))
4616 /* Expand parents as necessary. */
4619 /* see if we are trying to ensure that root is visible */
4620 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4621 parent = item->parent;
4623 parent = item; /* this item is the topmost item */
4625 while (parent != infoPtr->root)
4627 if (!(parent->state & TVIS_EXPANDED))
4628 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4630 parent = parent->parent;
4634 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4636 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4637 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4639 if (hasFirstVisible)
4640 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4642 if (visible_pos < 0)
4644 /* item is before the start of the list: put it at the top. */
4645 newFirstVisible = item;
4647 else if (visible_pos >= viscount
4648 /* Sometimes, before we are displayed, GVC is 0, causing us to
4649 * spuriously scroll up. */
4650 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4652 /* item is past the end of the list. */
4653 int scroll = visible_pos - viscount;
4655 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4661 /* Scroll window so item's text is visible as much as possible */
4662 /* Calculation of amount of extra space is taken from EditLabel code */
4664 TEXTMETRICW textMetric;
4665 HDC hdc = GetWindowDC(infoPtr->hwnd);
4667 x = item->textWidth;
4669 GetTextMetricsW(hdc, &textMetric);
4670 ReleaseDC(infoPtr->hwnd, hdc);
4672 x += (textMetric.tmMaxCharWidth * 2);
4673 x = max(x, textMetric.tmMaxCharWidth * 3);
4675 if (item->textOffset < 0)
4676 pos = item->textOffset;
4677 else if (item->textOffset + x > infoPtr->clientWidth)
4679 if (x > infoPtr->clientWidth)
4680 pos = item->textOffset;
4682 pos = item->textOffset + x - infoPtr->clientWidth;
4687 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4690 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4692 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4701 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4702 TREEVIEW_ITEM *newFirstVisible,
4703 BOOL bUpdateScrollPos)
4707 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4709 if (newFirstVisible != NULL)
4711 /* Prevent an empty gap from appearing at the bottom... */
4712 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4713 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4717 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4720 /* ... unless we just don't have enough items. */
4721 if (newFirstVisible == NULL)
4722 newFirstVisible = infoPtr->root->firstChild;
4726 if (infoPtr->firstVisible != newFirstVisible)
4728 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4730 infoPtr->firstVisible = newFirstVisible;
4731 TREEVIEW_Invalidate(infoPtr, NULL);
4735 TREEVIEW_ITEM *item;
4736 int scroll = infoPtr->uItemHeight *
4737 (infoPtr->firstVisible->visibleOrder
4738 - newFirstVisible->visibleOrder);
4740 infoPtr->firstVisible = newFirstVisible;
4742 for (item = infoPtr->root->firstChild; item != NULL;
4743 item = TREEVIEW_GetNextListItem(infoPtr, item))
4745 item->rect.top += scroll;
4746 item->rect.bottom += scroll;
4749 if (bUpdateScrollPos)
4750 SetScrollPos(infoPtr->hwnd, SB_VERT,
4751 newFirstVisible->visibleOrder, TRUE);
4753 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4758 /************************************************************************
4759 * VScroll is always in units of visible items. i.e. we always have a
4760 * visible item aligned to the top of the control. (Unless we have no
4764 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4766 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4767 TREEVIEW_ITEM *newFirstVisible = NULL;
4769 int nScrollCode = LOWORD(wParam);
4771 TRACE("wp %lx\n", wParam);
4773 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4776 if (!oldFirstVisible)
4778 assert(infoPtr->root->firstChild == NULL);
4782 switch (nScrollCode)
4785 newFirstVisible = infoPtr->root->firstChild;
4789 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4793 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4797 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4801 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4802 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4806 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4807 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4811 case SB_THUMBPOSITION:
4812 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4813 infoPtr->root->firstChild,
4814 (LONG)(SHORT)HIWORD(wParam));
4821 if (newFirstVisible != NULL)
4823 if (newFirstVisible != oldFirstVisible)
4824 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4825 nScrollCode != SB_THUMBTRACK);
4826 else if (nScrollCode == SB_THUMBPOSITION)
4827 SetScrollPos(infoPtr->hwnd, SB_VERT,
4828 newFirstVisible->visibleOrder, TRUE);
4835 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4838 int scrollX = infoPtr->scrollX;
4839 int nScrollCode = LOWORD(wParam);
4841 TRACE("wp %lx\n", wParam);
4843 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4846 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4847 /* shall never occur */
4854 switch (nScrollCode)
4857 scrollX -= infoPtr->uItemHeight;
4860 scrollX += infoPtr->uItemHeight;
4863 scrollX -= infoPtr->clientWidth;
4866 scrollX += infoPtr->clientWidth;
4870 case SB_THUMBPOSITION:
4871 scrollX = (int)(SHORT)HIWORD(wParam);
4878 if (scrollX > maxWidth)
4880 else if (scrollX < 0)
4884 if (scrollX != infoPtr->scrollX)
4886 TREEVIEW_ITEM *item;
4887 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4889 for (item = infoPtr->root->firstChild; item != NULL;
4890 item = TREEVIEW_GetNextListItem(infoPtr, item))
4892 item->linesOffset += scroll_pixels;
4893 item->stateOffset += scroll_pixels;
4894 item->imageOffset += scroll_pixels;
4895 item->textOffset += scroll_pixels;
4898 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4899 infoPtr->scrollX = scrollX;
4900 UpdateWindow(infoPtr->hwnd);
4903 if (nScrollCode != SB_THUMBTRACK)
4904 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4910 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4913 UINT pulScrollLines = 3;
4915 if (wParam & (MK_SHIFT | MK_CONTROL))
4916 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4918 if (infoPtr->firstVisible == NULL)
4921 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4923 gcWheelDelta = -(short)HIWORD(wParam);
4924 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4926 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4928 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4929 int maxDy = infoPtr->maxVisibleOrder;
4937 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4942 /* Create/Destroy *******************************************************/
4945 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
4948 HBITMAP hbm, hbmOld;
4952 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4954 hdcScreen = GetDC(0);
4956 hdc = CreateCompatibleDC(hdcScreen);
4957 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4958 hbmOld = SelectObject(hdc, hbm);
4960 SetRect(&rc, 0, 0, 48, 16);
4961 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4963 SetRect(&rc, 18, 2, 30, 14);
4964 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4965 DFCS_BUTTONCHECK|DFCS_FLAT);
4967 SetRect(&rc, 34, 2, 46, 14);
4968 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4969 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4971 SelectObject(hdc, hbmOld);
4972 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4973 comctl32_color.clrWindow);
4974 TRACE("checkbox index %d\n", nIndex);
4978 ReleaseDC(0, hdcScreen);
4980 infoPtr->stateImageWidth = 16;
4981 infoPtr->stateImageHeight = 16;
4985 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4988 TREEVIEW_INFO *infoPtr;
4991 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4993 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4995 if (infoPtr == NULL)
4997 ERR("could not allocate info memory!\n");
5001 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5003 infoPtr->hwnd = hwnd;
5004 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5006 infoPtr->uNumItems = 0;
5007 infoPtr->cdmode = 0;
5008 infoPtr->uScrollTime = 300; /* milliseconds */
5009 infoPtr->bRedraw = TRUE;
5011 GetClientRect(hwnd, &rcClient);
5013 /* No scroll bars yet. */
5014 infoPtr->clientWidth = rcClient.right;
5015 infoPtr->clientHeight = rcClient.bottom;
5016 infoPtr->uInternalStatus = 0;
5018 infoPtr->treeWidth = 0;
5019 infoPtr->treeHeight = 0;
5021 infoPtr->uIndent = MINIMUM_INDENT;
5022 infoPtr->selectedItem = NULL;
5023 infoPtr->focusedItem = NULL;
5024 infoPtr->hotItem = NULL;
5025 infoPtr->editItem = NULL;
5026 infoPtr->firstVisible = NULL;
5027 infoPtr->maxVisibleOrder = 0;
5028 infoPtr->dropItem = NULL;
5029 infoPtr->insertMarkItem = NULL;
5030 infoPtr->insertBeforeorAfter = 0;
5033 infoPtr->scrollX = 0;
5035 infoPtr->clrBk = CLR_NONE; /* use system color */
5036 infoPtr->clrText = CLR_NONE; /* use system color */
5037 infoPtr->clrLine = CLR_DEFAULT;
5038 infoPtr->clrInsertMark = CLR_DEFAULT;
5042 infoPtr->hwndEdit = NULL;
5043 infoPtr->wpEditOrig = NULL;
5044 infoPtr->bIgnoreEditKillFocus = FALSE;
5045 infoPtr->bLabelChanged = FALSE;
5047 infoPtr->himlNormal = NULL;
5048 infoPtr->himlState = NULL;
5049 infoPtr->normalImageWidth = 0;
5050 infoPtr->normalImageHeight = 0;
5051 infoPtr->stateImageWidth = 0;
5052 infoPtr->stateImageHeight = 0;
5054 infoPtr->items = DPA_Create(16);
5056 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5057 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5058 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5059 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5060 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5062 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5064 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5065 infoPtr->root->state = TVIS_EXPANDED;
5066 infoPtr->root->iLevel = -1;
5067 infoPtr->root->visibleOrder = -1;
5069 infoPtr->hwndNotify = lpcs->hwndParent;
5070 infoPtr->hwndToolTip = 0;
5072 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5073 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5075 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5076 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5077 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5080 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5081 TREEVIEW_InitCheckboxes(infoPtr);
5083 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5084 ShowScrollBar(hwnd, SB_VERT, FALSE);
5085 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5087 OpenThemeData (hwnd, themeClass);
5094 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5098 /* free item data */
5099 TREEVIEW_RemoveTree(infoPtr);
5100 /* root isn't freed with other items */
5101 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5102 DPA_Destroy(infoPtr->items);
5104 /* tool tip is automatically destroyed: we are its owner */
5106 /* Restore original wndproc */
5107 if (infoPtr->hwndEdit)
5108 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5109 (DWORD_PTR)infoPtr->wpEditOrig);
5111 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5113 /* Deassociate treeview from the window before doing anything drastic. */
5114 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5116 DeleteObject(infoPtr->hDefaultFont);
5117 DeleteObject(infoPtr->hBoldFont);
5118 DeleteObject(infoPtr->hUnderlineFont);
5124 /* Miscellaneous Messages ***********************************************/
5127 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5135 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5136 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5137 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5138 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5139 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5140 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5141 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5142 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5143 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5147 if (key >= VK_PRIOR && key <= VK_DOWN)
5149 unsigned char code = scroll[key - VK_PRIOR].code;
5151 (((code & (1 << 7)) == (SB_HORZ << 7))
5153 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5159 /************************************************************************
5162 * VK_UP Move selection to the previous non-hidden item.
5163 * VK_DOWN Move selection to the next non-hidden item.
5164 * VK_HOME Move selection to the first item.
5165 * VK_END Move selection to the last item.
5166 * VK_LEFT If expanded then collapse, otherwise move to parent.
5167 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5169 * VK_SUBTRACT Collapse.
5170 * VK_MULTIPLY Expand all.
5171 * VK_PRIOR Move up GetVisibleCount items.
5172 * VK_NEXT Move down GetVisibleCount items.
5173 * VK_BACK Move to parent.
5174 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5177 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5179 /* If it is non-NULL and different, it will be selected and visible. */
5180 TREEVIEW_ITEM *newSelection = NULL;
5182 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5184 TRACE("%lx\n", wParam);
5186 if (prevItem == NULL)
5189 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5190 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5195 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5197 newSelection = infoPtr->root->firstChild;
5201 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5205 newSelection = infoPtr->root->firstChild;
5209 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5213 if (prevItem->state & TVIS_EXPANDED)
5215 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5217 else if (prevItem->parent != infoPtr->root)
5219 newSelection = prevItem->parent;
5224 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5226 if (!(prevItem->state & TVIS_EXPANDED))
5227 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5230 newSelection = prevItem->firstChild;
5237 TREEVIEW_ExpandAll(infoPtr, prevItem);
5241 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5245 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5250 = TREEVIEW_GetListItem(infoPtr, prevItem,
5251 -TREEVIEW_GetVisibleCount(infoPtr));
5256 = TREEVIEW_GetListItem(infoPtr, prevItem,
5257 TREEVIEW_GetVisibleCount(infoPtr));
5261 newSelection = prevItem->parent;
5262 if (newSelection == infoPtr->root)
5263 newSelection = NULL;
5267 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5268 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5272 if (newSelection && newSelection != prevItem)
5274 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5277 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5285 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5287 /* remove hot effect from item */
5288 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5289 infoPtr->hotItem = NULL;
5295 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5298 TRACKMOUSEEVENT trackinfo;
5299 TREEVIEW_ITEM * item;
5301 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5303 /* fill in the TRACKMOUSEEVENT struct */
5304 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5305 trackinfo.dwFlags = TME_QUERY;
5306 trackinfo.hwndTrack = infoPtr->hwnd;
5308 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5309 _TrackMouseEvent(&trackinfo);
5311 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5312 if(!(trackinfo.dwFlags & TME_LEAVE))
5314 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5315 trackinfo.hwndTrack = infoPtr->hwnd;
5316 /* do it as fast as possible, minimal systimer latency will be used */
5317 trackinfo.dwHoverTime = 1;
5319 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5320 /* and can properly deactivate the hot item */
5321 _TrackMouseEvent(&trackinfo);
5324 pt.x = (short)LOWORD(lParam);
5325 pt.y = (short)HIWORD(lParam);
5327 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5329 if (item != infoPtr->hotItem)
5331 /* redraw old hot item */
5332 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5333 infoPtr->hotItem = item;
5334 /* redraw new hot item */
5335 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5341 /* Draw themed border */
5342 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5344 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5348 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5349 cyEdge = GetSystemMetrics (SM_CYEDGE);
5352 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5354 GetWindowRect(infoPtr->hwnd, &r);
5356 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5357 r.right - cxEdge, r.bottom - cyEdge);
5358 if (region != (HRGN)1)
5359 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5360 OffsetRect(&r, -r.left, -r.top);
5362 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5363 OffsetRect(&r, -r.left, -r.top);
5365 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5366 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5367 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5368 ReleaseDC(infoPtr->hwnd, dc);
5370 /* Call default proc to get the scrollbars etc. painted */
5371 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5377 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5379 LPNMHDR lpnmh = (LPNMHDR)lParam;
5381 if (lpnmh->code == PGN_CALCSIZE) {
5382 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5384 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5385 lppgc->iWidth = infoPtr->treeWidth;
5386 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5387 infoPtr->treeWidth, infoPtr->clientWidth);
5390 lppgc->iHeight = infoPtr->treeHeight;
5391 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5392 infoPtr->treeHeight, infoPtr->clientHeight);
5396 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5400 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5402 if (wParam == SIZE_RESTORED)
5404 infoPtr->clientWidth = (short)LOWORD(lParam);
5405 infoPtr->clientHeight = (short)HIWORD(lParam);
5407 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5408 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5409 TREEVIEW_UpdateScrollBars(infoPtr);
5413 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5416 TREEVIEW_Invalidate(infoPtr, NULL);
5420 static void TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5422 TREEVIEW_ITEM *child = item->firstChild;
5424 item->state &= ~TVIS_STATEIMAGEMASK;
5425 item->state |= INDEXTOSTATEIMAGEMASK(1);
5429 TREEVIEW_ITEM *next = child->nextSibling;
5430 TREEVIEW_ResetImageStateIndex(infoPtr, child);
5436 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5438 TRACE("(%lx %lx)\n", wParam, lParam);
5440 if (wParam == GWL_STYLE)
5442 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5444 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5446 if (dwNewStyle & TVS_CHECKBOXES)
5448 TREEVIEW_InitCheckboxes(infoPtr);
5449 TRACE("checkboxes enabled\n");
5451 /* set all items to state image index 1 */
5452 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5456 FIXME("tried to disable checkboxes\n");
5460 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5462 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5464 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5465 TRACE("tooltips enabled\n");
5469 DestroyWindow(infoPtr->hwndToolTip);
5470 infoPtr->hwndToolTip = 0;
5471 TRACE("tooltips disabled\n");
5475 infoPtr->dwStyle = dwNewStyle;
5478 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5479 TREEVIEW_UpdateScrollBars(infoPtr);
5480 TREEVIEW_Invalidate(infoPtr, NULL);
5486 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5489 TREEVIEW_ITEM * item;
5493 ScreenToClient(infoPtr->hwnd, &pt);
5495 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5497 memset(&nmmouse, 0, sizeof(nmmouse));
5498 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5499 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5500 nmmouse.hdr.code = NM_SETCURSOR;
5503 nmmouse.dwItemSpec = (DWORD_PTR)item;
5504 nmmouse.dwItemData = item->lParam;
5508 nmmouse.dwHitInfo = lParam;
5509 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, &nmmouse.hdr))
5512 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5514 SetCursor(infoPtr->hcurHand);
5518 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5522 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5526 if (!infoPtr->selectedItem)
5528 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5532 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5533 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5538 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5542 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5543 UpdateWindow(infoPtr->hwnd);
5544 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5548 /* update theme after a WM_THEMECHANGED message */
5549 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5551 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5552 CloseThemeData (theme);
5553 OpenThemeData (infoPtr->hwnd, themeClass);
5558 static LRESULT WINAPI
5559 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5561 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5563 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5565 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5568 if (uMsg == WM_CREATE)
5569 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5576 case TVM_CREATEDRAGIMAGE:
5577 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5579 case TVM_DELETEITEM:
5580 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5582 case TVM_EDITLABELA:
5583 case TVM_EDITLABELW:
5584 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5586 case TVM_ENDEDITLABELNOW:
5587 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5589 case TVM_ENSUREVISIBLE:
5590 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5593 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5595 case TVM_GETBKCOLOR:
5596 return TREEVIEW_GetBkColor(infoPtr);
5599 return TREEVIEW_GetCount(infoPtr);
5601 case TVM_GETEDITCONTROL:
5602 return TREEVIEW_GetEditControl(infoPtr);
5604 case TVM_GETIMAGELIST:
5605 return TREEVIEW_GetImageList(infoPtr, wParam);
5608 return TREEVIEW_GetIndent(infoPtr);
5610 case TVM_GETINSERTMARKCOLOR:
5611 return TREEVIEW_GetInsertMarkColor(infoPtr);
5613 case TVM_GETISEARCHSTRINGA:
5614 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5617 case TVM_GETISEARCHSTRINGW:
5618 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5623 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5624 uMsg == TVM_GETITEMW);
5625 case TVM_GETITEMHEIGHT:
5626 return TREEVIEW_GetItemHeight(infoPtr);
5628 case TVM_GETITEMRECT:
5629 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5631 case TVM_GETITEMSTATE:
5632 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5634 case TVM_GETLINECOLOR:
5635 return TREEVIEW_GetLineColor(infoPtr);
5637 case TVM_GETNEXTITEM:
5638 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5640 case TVM_GETSCROLLTIME:
5641 return TREEVIEW_GetScrollTime(infoPtr);
5643 case TVM_GETTEXTCOLOR:
5644 return TREEVIEW_GetTextColor(infoPtr);
5646 case TVM_GETTOOLTIPS:
5647 return TREEVIEW_GetToolTips(infoPtr);
5649 case TVM_GETUNICODEFORMAT:
5650 return TREEVIEW_GetUnicodeFormat(infoPtr);
5652 case TVM_GETVISIBLECOUNT:
5653 return TREEVIEW_GetVisibleCount(infoPtr);
5656 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5658 case TVM_INSERTITEMA:
5659 case TVM_INSERTITEMW:
5660 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5661 uMsg == TVM_INSERTITEMW);
5662 case TVM_SELECTITEM:
5663 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5665 case TVM_SETBKCOLOR:
5666 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5668 case TVM_SETIMAGELIST:
5669 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5672 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5674 case TVM_SETINSERTMARK:
5675 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5677 case TVM_SETINSERTMARKCOLOR:
5678 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5682 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5683 uMsg == TVM_SETITEMW);
5684 case TVM_SETLINECOLOR:
5685 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5687 case TVM_SETITEMHEIGHT:
5688 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5690 case TVM_SETSCROLLTIME:
5691 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5693 case TVM_SETTEXTCOLOR:
5694 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5696 case TVM_SETTOOLTIPS:
5697 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5699 case TVM_SETUNICODEFORMAT:
5700 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5702 case TVM_SORTCHILDREN:
5703 return TREEVIEW_SortChildren(infoPtr, lParam);
5705 case TVM_SORTCHILDRENCB:
5706 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5709 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5712 return TREEVIEW_Command(infoPtr, wParam, lParam);
5715 return TREEVIEW_Destroy(infoPtr);
5720 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5723 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5726 return TREEVIEW_GetFont(infoPtr);
5729 return TREEVIEW_HScroll(infoPtr, wParam);
5732 return TREEVIEW_KeyDown(infoPtr, wParam);
5735 return TREEVIEW_KillFocus(infoPtr);
5737 case WM_LBUTTONDBLCLK:
5738 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5740 case WM_LBUTTONDOWN:
5741 return TREEVIEW_LButtonDown(infoPtr, lParam);
5743 /* WM_MBUTTONDOWN */
5746 return TREEVIEW_MouseLeave(infoPtr);
5749 return TREEVIEW_MouseMove(infoPtr, lParam);
5751 case WM_NCLBUTTONDOWN:
5752 if (infoPtr->hwndEdit)
5753 SetFocus(infoPtr->hwnd);
5757 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5760 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5762 case WM_NOTIFYFORMAT:
5763 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5765 case WM_PRINTCLIENT:
5766 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5769 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5771 case WM_RBUTTONDOWN:
5772 return TREEVIEW_RButtonDown(infoPtr, lParam);
5775 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5778 return TREEVIEW_SetFocus(infoPtr);
5781 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5784 return TREEVIEW_SetRedraw(infoPtr, wParam);
5787 return TREEVIEW_Size(infoPtr, wParam, lParam);
5789 case WM_STYLECHANGED:
5790 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5792 case WM_SYSCOLORCHANGE:
5793 COMCTL32_RefreshSysColors();
5799 return TREEVIEW_HandleTimer(infoPtr, wParam);
5801 case WM_THEMECHANGED:
5802 return TREEVIEW_ThemeChanged (infoPtr);
5805 return TREEVIEW_VScroll(infoPtr, wParam);
5807 /* WM_WININICHANGE */
5810 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5813 TRACE("drawItem\n");
5817 /* This mostly catches MFC and Delphi messages. :( */
5818 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5819 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5821 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5826 /* Class Registration ***************************************************/
5829 TREEVIEW_Register(void)
5835 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5836 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5837 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5838 wndClass.cbClsExtra = 0;
5839 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5841 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5842 wndClass.hbrBackground = 0;
5843 wndClass.lpszClassName = WC_TREEVIEWW;
5845 RegisterClassW(&wndClass);
5850 TREEVIEW_Unregister(void)
5852 UnregisterClassW(WC_TREEVIEWW, NULL);
5856 /* Tree Verification ****************************************************/
5859 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5861 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5862 const TREEVIEW_ITEM *item)
5864 assert(infoPtr != NULL);
5865 assert(item != NULL);
5867 /* both NULL, or both non-null */
5868 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5870 assert(item->firstChild != item);
5871 assert(item->lastChild != item);
5873 if (item->firstChild)
5875 assert(item->firstChild->parent == item);
5876 assert(item->firstChild->prevSibling == NULL);
5879 if (item->lastChild)
5881 assert(item->lastChild->parent == item);
5882 assert(item->lastChild->nextSibling == NULL);
5885 assert(item->nextSibling != item);
5886 if (item->nextSibling)
5888 assert(item->nextSibling->parent == item->parent);
5889 assert(item->nextSibling->prevSibling == item);
5892 assert(item->prevSibling != item);
5893 if (item->prevSibling)
5895 assert(item->prevSibling->parent == item->parent);
5896 assert(item->prevSibling->nextSibling == item);
5901 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5903 assert(item != NULL);
5905 assert(item->parent != NULL);
5906 assert(item->parent != item);
5907 assert(item->iLevel == item->parent->iLevel + 1);
5909 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5911 TREEVIEW_VerifyItemCommon(infoPtr, item);
5913 TREEVIEW_VerifyChildren(infoPtr, item);
5917 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5919 const TREEVIEW_ITEM *child;
5920 assert(item != NULL);
5922 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5923 TREEVIEW_VerifyItem(infoPtr, child);
5927 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5929 TREEVIEW_ITEM *root = infoPtr->root;
5931 assert(root != NULL);
5932 assert(root->iLevel == -1);
5933 assert(root->parent == NULL);
5934 assert(root->prevSibling == NULL);
5936 TREEVIEW_VerifyItemCommon(infoPtr, root);
5938 TREEVIEW_VerifyChildren(infoPtr, root);
5942 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5944 if (!TRACE_ON(treeview)) return;
5946 assert(infoPtr != NULL);
5947 TREEVIEW_VerifyRoot(infoPtr);