3 * Copyright 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de>
4 * Copyright 1998,1999 Alex Priem <alexp@sci.kun.nl>
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
26 * Note2: If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
27 * of size TEXT_CALLBACK_SIZE in DoSetItem.
28 * We use callbackMask to keep track of fields to be updated.
31 * missing notifications: TVN_GETINFOTIP, TVN_KEYDOWN,
32 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
38 * Make the insertion mark look right.
39 * Scroll (instead of repaint) as much as possible.
43 #include "wine/port.h"
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
63 #include "wine/unicode.h"
64 #include "wine/debug.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
68 /* internal structures */
70 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
81 int iIntegral; /* item height multiplier (1 is normal) */
82 int iLevel; /* indentation level:0=root level */
83 HTREEITEM parent; /* handle to parent or 0 if at root */
84 HTREEITEM firstChild; /* handle to first child or 0 if no child */
86 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
87 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
93 LONG textWidth; /* horizontal text extent for pszText */
94 LONG visibleOrder; /* visible ordering, 0 is first visible item */
98 typedef struct tagTREEVIEW_INFO
101 HWND hwndNotify; /* Owner window to send notifications to */
104 UINT uInternalStatus;
106 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
107 INT cdmode; /* last custom draw setting */
108 UINT uScrollTime; /* max. time for scrolling in milliseconds */
109 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
111 UINT uItemHeight; /* item height */
114 LONG clientWidth; /* width of control window */
115 LONG clientHeight; /* height of control window */
117 LONG treeWidth; /* width of visible tree items */
118 LONG treeHeight; /* height of visible tree items */
120 UINT uIndent; /* indentation in pixels */
121 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
122 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
123 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
124 HTREEITEM editItem; /* item being edited with builtin edit box */
126 HTREEITEM firstVisible; /* handle to first visible item */
127 LONG maxVisibleOrder;
128 HTREEITEM dropItem; /* handle to item selected by drag cursor */
129 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
130 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
131 HIMAGELIST dragList; /* Bitmap of dragged item */
136 COLORREF clrInsertMark;
140 HFONT hUnderlineFont;
145 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
146 BOOL bIgnoreEditKillFocus;
149 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
150 HIMAGELIST himlNormal;
151 int normalImageHeight;
152 int normalImageWidth;
153 HIMAGELIST himlState;
154 int stateImageHeight;
158 DWORD lastKeyPressTimestamp;
160 INT nSearchParamLength;
161 WCHAR szSearchParam[ MAX_PATH ];
165 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
166 #define KEY_DELAY 450
168 /* bitflags for infoPtr->uInternalStatus */
170 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
171 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
172 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
173 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
174 #define TV_RDRAG 0x10 /* ditto Rbutton */
175 #define TV_RDRAGGING 0x20
177 /* bitflags for infoPtr->timer */
179 #define TV_EDIT_TIMER 2
180 #define TV_EDIT_TIMER_SET 2
182 #define TEXT_CALLBACK_SIZE 260
184 #define TREEVIEW_LEFT_MARGIN 8
186 #define MINIMUM_INDENT 19
188 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
190 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
191 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
192 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
194 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
195 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
196 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
197 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
199 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
202 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
205 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
207 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
208 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
209 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
210 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
211 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
212 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
213 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
215 /* Random Utilities *****************************************************/
216 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
218 /* Returns the treeview private data if hwnd is a treeview.
219 * Otherwise returns an undefined value. */
220 static inline TREEVIEW_INFO *
221 TREEVIEW_GetInfoPtr(HWND hwnd)
223 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
226 /* Don't call this. Nothing wants an item index. */
228 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
230 return DPA_GetPtrIndex(infoPtr->items, handle);
233 /* Checks if item has changed and needs to be redrawn */
234 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
235 const TVITEMEXW *tvChange)
237 /* Number of children has changed */
238 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
241 /* Image has changed and it's not a callback */
242 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
243 tiNew->iImage != I_IMAGECALLBACK)
246 /* Selected image has changed and it's not a callback */
247 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
248 tiNew->iSelectedImage != I_IMAGECALLBACK)
251 /* Text has changed and it's not a callback */
252 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
253 tiNew->pszText != LPSTR_TEXTCALLBACKW)
256 /* Indent has changed */
257 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
260 /* Item state has changed */
261 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
267 /***************************************************************************
268 * This method checks that handle is an item for this tree.
271 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
273 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
275 TRACE("invalid item %p\n", handle);
283 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
287 GetObjectW(hOrigFont, sizeof(font), &font);
288 font.lfWeight = FW_BOLD;
289 return CreateFontIndirectW(&font);
293 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
297 GetObjectW(hOrigFont, sizeof(font), &font);
298 font.lfUnderline = TRUE;
299 return CreateFontIndirectW(&font);
303 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
305 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
306 return infoPtr->hUnderlineFont;
307 if (item->state & TVIS_BOLD)
308 return infoPtr->hBoldFont;
309 return infoPtr->hFont;
312 /* for trace/debugging purposes only */
314 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
316 if (item == NULL) return "<null item>";
317 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
318 if (item->pszText == NULL) return "<null>";
319 return debugstr_w(item->pszText);
322 /* An item is not a child of itself. */
324 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
328 child = child->parent;
329 if (child == parent) return TRUE;
330 } while (child != NULL);
336 /* Tree Traversal *******************************************************/
338 /***************************************************************************
339 * This method returns the last expanded sibling or child child item
342 static TREEVIEW_ITEM *
343 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
348 while (wineItem->lastChild)
350 if (wineItem->state & TVIS_EXPANDED)
351 wineItem = wineItem->lastChild;
356 if (wineItem == infoPtr->root)
362 /***************************************************************************
363 * This method returns the previous non-hidden item in the list not
364 * considering the tree hierarchy.
366 static TREEVIEW_ITEM *
367 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
369 if (tvItem->prevSibling)
371 /* This item has a prevSibling, get the last item in the sibling's tree. */
372 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
374 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
375 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
381 /* this item does not have a prevSibling, get the parent */
382 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
387 /***************************************************************************
388 * This method returns the next physical item in the treeview not
389 * considering the tree hierarchy.
391 static TREEVIEW_ITEM *
392 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
395 * If this item has children and is expanded, return the first child
397 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
399 return tvItem->firstChild;
404 * try to get the sibling
406 if (tvItem->nextSibling)
407 return tvItem->nextSibling;
410 * Otherwise, get the parent's sibling.
412 while (tvItem->parent)
414 tvItem = tvItem->parent;
416 if (tvItem->nextSibling)
417 return tvItem->nextSibling;
423 /***************************************************************************
424 * This method returns the nth item starting at the given item. It returns
425 * the last item (or first) we we run out of items.
427 * Will scroll backward if count is <0.
428 * forward if count is >0.
430 static TREEVIEW_ITEM *
431 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
434 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
435 TREEVIEW_ITEM *previousItem;
437 assert(wineItem != NULL);
441 next_item = TREEVIEW_GetNextListItem;
446 next_item = TREEVIEW_GetPrevListItem;
453 previousItem = wineItem;
454 wineItem = next_item(infoPtr, wineItem);
456 } while (--count && wineItem != NULL);
459 return wineItem ? wineItem : previousItem;
462 /* Notifications ************************************************************/
464 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
466 if (!infoPtr->bNtfUnicode) {
468 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
469 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
470 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
471 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
472 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
473 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
474 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
475 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
476 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
477 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
478 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
479 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
486 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
488 TRACE("wParam=%ld, lParam=%ld\n", wParam, lParam);
489 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
493 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
496 HWND hwnd = infoPtr->hwnd;
499 nmhdr.hwndFrom = hwnd;
500 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
501 nmhdr.code = get_notifycode(infoPtr, code);
503 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, (LPARAM)&nmhdr);
507 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
510 tvItem->hItem = item;
511 tvItem->state = item->state;
512 tvItem->stateMask = 0;
513 tvItem->iImage = item->iImage;
514 tvItem->iSelectedImage = item->iSelectedImage;
515 tvItem->cChildren = item->cChildren;
516 tvItem->lParam = item->lParam;
520 if (!infoPtr->bNtfUnicode)
522 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
523 tvItem->pszText = Alloc (tvItem->cchTextMax);
524 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
528 tvItem->cchTextMax = item->cchTextMax;
529 tvItem->pszText = item->pszText;
534 tvItem->cchTextMax = 0;
535 tvItem->pszText = NULL;
540 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
541 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
543 HWND hwnd = infoPtr->hwnd;
547 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
548 code, action, oldItem, newItem);
550 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
552 nmhdr.hdr.hwndFrom = hwnd;
553 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
554 nmhdr.hdr.code = get_notifycode(infoPtr, code);
555 nmhdr.action = action;
558 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
561 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
566 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
567 if (!infoPtr->bNtfUnicode)
569 Free(nmhdr.itemOld.pszText);
570 Free(nmhdr.itemNew.pszText);
576 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
577 HTREEITEM dragItem, POINT pt)
579 HWND hwnd = infoPtr->hwnd;
582 TRACE("code:%d dragitem:%p\n", code, dragItem);
584 nmhdr.hdr.hwndFrom = hwnd;
585 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
586 nmhdr.hdr.code = get_notifycode(infoPtr, code);
588 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
589 nmhdr.itemNew.hItem = dragItem;
590 nmhdr.itemNew.state = dragItem->state;
591 nmhdr.itemNew.lParam = dragItem->lParam;
593 nmhdr.ptDrag.x = pt.x;
594 nmhdr.ptDrag.y = pt.y;
596 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
601 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
604 HWND hwnd = infoPtr->hwnd;
605 NMTVCUSTOMDRAW nmcdhdr;
608 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
610 nmcd = &nmcdhdr.nmcd;
611 nmcd->hdr.hwndFrom = hwnd;
612 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
613 nmcd->hdr.code = NM_CUSTOMDRAW;
614 nmcd->dwDrawStage = dwDrawStage;
617 nmcd->dwItemSpec = 0;
618 nmcd->uItemState = 0;
619 nmcd->lItemlParam = 0;
620 nmcdhdr.clrText = infoPtr->clrText;
621 nmcdhdr.clrTextBk = infoPtr->clrBk;
624 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)&nmcdhdr);
629 /* FIXME: need to find out when the flags in uItemState need to be set */
632 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
633 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
634 NMTVCUSTOMDRAW *nmcdhdr)
636 HWND hwnd = infoPtr->hwnd;
639 DWORD_PTR dwItemSpec;
642 dwDrawStage = CDDS_ITEM | uItemDrawState;
643 dwItemSpec = (DWORD_PTR)wineItem;
645 if (wineItem->state & TVIS_SELECTED)
646 uItemState |= CDIS_SELECTED;
647 if (wineItem == infoPtr->selectedItem)
648 uItemState |= CDIS_FOCUS;
649 if (wineItem == infoPtr->hotItem)
650 uItemState |= CDIS_HOT;
652 nmcd = &nmcdhdr->nmcd;
653 nmcd->hdr.hwndFrom = hwnd;
654 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
655 nmcd->hdr.code = NM_CUSTOMDRAW;
656 nmcd->dwDrawStage = dwDrawStage;
658 nmcd->rc = wineItem->rect;
659 nmcd->dwItemSpec = dwItemSpec;
660 nmcd->uItemState = uItemState;
661 nmcd->lItemlParam = wineItem->lParam;
662 nmcdhdr->iLevel = wineItem->iLevel;
664 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
665 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
666 nmcd->uItemState, nmcd->lItemlParam);
668 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)nmcdhdr);
672 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
674 HWND hwnd = infoPtr->hwnd;
678 tvdi.hdr.hwndFrom = hwnd;
679 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
680 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
682 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
683 &tvdi.item, editItem);
685 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
687 if (!infoPtr->bNtfUnicode)
688 Free(tvdi.item.pszText);
694 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
697 NMTVDISPINFOEXW callback;
698 HWND hwnd = infoPtr->hwnd;
700 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
701 mask &= wineItem->callbackMask;
703 if (mask == 0) return;
705 callback.hdr.hwndFrom = hwnd;
706 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
707 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
709 /* 'state' always contains valid value, as well as 'lParam'.
710 * All other parameters are uninitialized.
712 callback.item.pszText = wineItem->pszText;
713 callback.item.cchTextMax = wineItem->cchTextMax;
714 callback.item.mask = mask;
715 callback.item.hItem = wineItem;
716 callback.item.state = wineItem->state;
717 callback.item.lParam = wineItem->lParam;
719 /* If text is changed we need to recalculate textWidth */
720 if (mask & TVIF_TEXT)
721 wineItem->textWidth = 0;
723 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, (LPARAM)&callback);
725 /* It may have changed due to a call to SetItem. */
726 mask &= wineItem->callbackMask;
728 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
730 /* Instead of copying text into our buffer user specified its own */
731 if (!infoPtr->bNtfUnicode) {
734 int len = MultiByteToWideChar( CP_ACP, 0,
735 (LPSTR)callback.item.pszText, -1,
737 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
738 newText = ReAlloc(wineItem->pszText, buflen);
740 TRACE("returned str %s, len=%d, buflen=%d\n",
741 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
745 wineItem->pszText = newText;
746 MultiByteToWideChar( CP_ACP, 0,
747 (LPSTR)callback.item.pszText, -1,
748 wineItem->pszText, buflen/sizeof(WCHAR));
749 wineItem->cchTextMax = buflen/sizeof(WCHAR);
751 /* If ReAlloc fails we have nothing to do, but keep original text */
754 int len = max(lstrlenW(callback.item.pszText) + 1,
756 LPWSTR newText = ReAlloc(wineItem->pszText, len);
758 TRACE("returned wstr %s, len=%d\n",
759 debugstr_w(callback.item.pszText), len);
763 wineItem->pszText = newText;
764 strcpyW(wineItem->pszText, callback.item.pszText);
765 wineItem->cchTextMax = len;
767 /* If ReAlloc fails we have nothing to do, but keep original text */
770 else if (mask & TVIF_TEXT) {
771 /* User put text into our buffer, that is ok unless A string */
772 if (!infoPtr->bNtfUnicode) {
774 LPWSTR oldText = NULL;
776 int len = MultiByteToWideChar( CP_ACP, 0,
777 (LPSTR)callback.item.pszText, -1,
779 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
780 newText = Alloc(buflen);
782 TRACE("same buffer str %s, len=%d, buflen=%d\n",
783 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
787 oldText = wineItem->pszText;
788 wineItem->pszText = newText;
789 MultiByteToWideChar( CP_ACP, 0,
790 (LPSTR)callback.item.pszText, -1,
791 wineItem->pszText, buflen/sizeof(WCHAR));
792 wineItem->cchTextMax = buflen/sizeof(WCHAR);
798 if (mask & TVIF_IMAGE)
799 wineItem->iImage = callback.item.iImage;
801 if (mask & TVIF_SELECTEDIMAGE)
802 wineItem->iSelectedImage = callback.item.iSelectedImage;
804 if (mask & TVIF_CHILDREN)
805 wineItem->cChildren = callback.item.cChildren;
807 /* These members are now permanently set. */
808 if (callback.item.mask & TVIF_DI_SETITEM)
809 wineItem->callbackMask &= ~callback.item.mask;
812 /***************************************************************************
813 * This function uses cChildren field to decide whether the item has
815 * Note: if this returns TRUE, the child items may not actually exist,
816 * they could be virtual.
818 * Just use wineItem->firstChild to check for physical children.
821 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
823 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
825 return wineItem->cChildren > 0;
828 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
832 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
834 if (nCommand != NF_REQUERY) return 0;
836 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
837 TRACE("format=%d\n", format);
839 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
841 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
846 /* Item Position ********************************************************/
848 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
850 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
852 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
853 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
856 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
858 item->stateOffset = item->linesOffset + infoPtr->uIndent;
859 item->imageOffset = item->stateOffset
860 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
861 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
865 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
871 /* DRAW's OM docker creates items like this */
872 if (item->pszText == NULL)
884 hdc = GetDC(infoPtr->hwnd);
885 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
888 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
889 item->textWidth = sz.cx;
893 SelectObject(hdc, hOldFont);
899 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
901 item->rect.top = infoPtr->uItemHeight *
902 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
904 item->rect.bottom = item->rect.top
905 + infoPtr->uItemHeight * item->iIntegral - 1;
908 item->rect.right = infoPtr->clientWidth;
911 /* We know that only items after start need their order updated. */
913 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
920 start = infoPtr->root->firstChild;
924 order = start->visibleOrder;
926 for (item = start; item != NULL;
927 item = TREEVIEW_GetNextListItem(infoPtr, item))
929 if (!ISVISIBLE(item) && order > 0)
930 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
931 item->visibleOrder = order;
932 order += item->iIntegral;
935 infoPtr->maxVisibleOrder = order;
937 for (item = start; item != NULL;
938 item = TREEVIEW_GetNextListItem(infoPtr, item))
940 TREEVIEW_ComputeItemRect(infoPtr, item);
945 /* Update metrics of all items in selected subtree.
946 * root must be expanded
949 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
951 TREEVIEW_ITEM *sibling;
955 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
958 root->state &= ~TVIS_EXPANDED;
959 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
960 root->state |= TVIS_EXPANDED;
962 hdc = GetDC(infoPtr->hwnd);
963 hOldFont = SelectObject(hdc, infoPtr->hFont);
965 for (; root != sibling;
966 root = TREEVIEW_GetNextListItem(infoPtr, root))
968 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
970 if (root->callbackMask & TVIF_TEXT)
971 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
973 if (root->textWidth == 0)
975 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
976 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
980 SelectObject(hdc, hOldFont);
981 ReleaseDC(infoPtr->hwnd, hdc);
984 /* Item Allocation **********************************************************/
986 static TREEVIEW_ITEM *
987 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
989 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
994 /* I_IMAGENONE would make more sense but this is neither what is
995 * documented (MSDN doesn't specify) nor what Windows actually does
996 * (it sets it to zero)... and I can so imagine an application using
997 * inc/dec to toggle the images. */
999 newItem->iSelectedImage = 0;
1001 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1010 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1011 * free item->pszText. */
1013 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1015 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1016 if (infoPtr->selectedItem == item)
1017 infoPtr->selectedItem = NULL;
1018 if (infoPtr->hotItem == item)
1019 infoPtr->hotItem = NULL;
1020 if (infoPtr->focusedItem == item)
1021 infoPtr->focusedItem = NULL;
1022 if (infoPtr->firstVisible == item)
1023 infoPtr->firstVisible = NULL;
1024 if (infoPtr->dropItem == item)
1025 infoPtr->dropItem = NULL;
1026 if (infoPtr->insertMarkItem == item)
1027 infoPtr->insertMarkItem = NULL;
1032 /* Item Insertion *******************************************************/
1034 /***************************************************************************
1035 * This method inserts newItem before sibling as a child of parent.
1036 * sibling can be NULL, but only if parent has no children.
1039 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1040 TREEVIEW_ITEM *parent)
1042 assert(parent != NULL);
1044 if (sibling != NULL)
1046 assert(sibling->parent == parent);
1048 if (sibling->prevSibling != NULL)
1049 sibling->prevSibling->nextSibling = newItem;
1051 newItem->prevSibling = sibling->prevSibling;
1052 sibling->prevSibling = newItem;
1055 newItem->prevSibling = NULL;
1057 newItem->nextSibling = sibling;
1059 if (parent->firstChild == sibling)
1060 parent->firstChild = newItem;
1062 if (parent->lastChild == NULL)
1063 parent->lastChild = newItem;
1066 /***************************************************************************
1067 * This method inserts newItem after sibling as a child of parent.
1068 * sibling can be NULL, but only if parent has no children.
1071 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1072 TREEVIEW_ITEM *parent)
1074 assert(parent != NULL);
1076 if (sibling != NULL)
1078 assert(sibling->parent == parent);
1080 if (sibling->nextSibling != NULL)
1081 sibling->nextSibling->prevSibling = newItem;
1083 newItem->nextSibling = sibling->nextSibling;
1084 sibling->nextSibling = newItem;
1087 newItem->nextSibling = NULL;
1089 newItem->prevSibling = sibling;
1091 if (parent->lastChild == sibling)
1092 parent->lastChild = newItem;
1094 if (parent->firstChild == NULL)
1095 parent->firstChild = newItem;
1099 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1100 const TVITEMEXW *tvItem, BOOL isW)
1102 UINT callbackClear = 0;
1103 UINT callbackSet = 0;
1105 TRACE("item %p\n", wineItem);
1106 /* Do this first in case it fails. */
1107 if (tvItem->mask & TVIF_TEXT)
1109 wineItem->textWidth = 0; /* force width recalculation */
1110 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1115 len = lstrlenW(tvItem->pszText) + 1;
1117 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1119 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1121 if (newText == NULL) return FALSE;
1123 callbackClear |= TVIF_TEXT;
1125 wineItem->pszText = newText;
1126 wineItem->cchTextMax = len;
1128 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1130 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1131 wineItem->pszText, len);
1133 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1137 callbackSet |= TVIF_TEXT;
1139 wineItem->pszText = ReAlloc(wineItem->pszText,
1140 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1141 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1142 TRACE("setting callback, item %p\n", wineItem);
1146 if (tvItem->mask & TVIF_CHILDREN)
1148 wineItem->cChildren = tvItem->cChildren;
1150 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1151 callbackSet |= TVIF_CHILDREN;
1153 callbackClear |= TVIF_CHILDREN;
1156 if (tvItem->mask & TVIF_IMAGE)
1158 wineItem->iImage = tvItem->iImage;
1160 if (wineItem->iImage == I_IMAGECALLBACK)
1161 callbackSet |= TVIF_IMAGE;
1163 callbackClear |= TVIF_IMAGE;
1166 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1168 wineItem->iSelectedImage = tvItem->iSelectedImage;
1170 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1171 callbackSet |= TVIF_SELECTEDIMAGE;
1173 callbackClear |= TVIF_SELECTEDIMAGE;
1176 if (tvItem->mask & TVIF_PARAM)
1177 wineItem->lParam = tvItem->lParam;
1179 /* If the application sets TVIF_INTEGRAL without
1180 * supplying a TVITEMEX structure, it's toast. */
1181 if (tvItem->mask & TVIF_INTEGRAL)
1182 wineItem->iIntegral = tvItem->iIntegral;
1184 if (tvItem->mask & TVIF_STATE)
1186 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1188 wineItem->state &= ~tvItem->stateMask;
1189 wineItem->state |= (tvItem->state & tvItem->stateMask);
1192 wineItem->callbackMask |= callbackSet;
1193 wineItem->callbackMask &= ~callbackClear;
1198 /* Note that the new item is pre-zeroed. */
1200 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1202 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1203 HTREEITEM insertAfter;
1204 TREEVIEW_ITEM *newItem, *parentItem;
1205 BOOL bTextUpdated = FALSE;
1207 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1209 parentItem = infoPtr->root;
1213 parentItem = ptdi->hParent;
1215 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1217 WARN("invalid parent %p\n", parentItem);
1222 insertAfter = ptdi->hInsertAfter;
1224 /* Validate this now for convenience. */
1225 switch ((DWORD_PTR)insertAfter)
1227 case (DWORD_PTR)TVI_FIRST:
1228 case (DWORD_PTR)TVI_LAST:
1229 case (DWORD_PTR)TVI_SORT:
1233 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1234 insertAfter->parent != parentItem)
1236 WARN("invalid insert after %p\n", insertAfter);
1237 insertAfter = TVI_LAST;
1241 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1242 (tvItem->mask & TVIF_TEXT)
1243 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1244 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1247 newItem = TREEVIEW_AllocateItem(infoPtr);
1248 if (newItem == NULL)
1251 newItem->parent = parentItem;
1252 newItem->iIntegral = 1;
1253 newItem->visibleOrder = -1;
1255 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1258 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1260 infoPtr->uNumItems++;
1262 switch ((DWORD_PTR)insertAfter)
1264 case (DWORD_PTR)TVI_FIRST:
1266 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1267 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1268 if (infoPtr->firstVisible == originalFirst)
1269 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1273 case (DWORD_PTR)TVI_LAST:
1274 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1277 /* hInsertAfter names a specific item we want to insert after */
1279 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1282 case (DWORD_PTR)TVI_SORT:
1284 TREEVIEW_ITEM *aChild;
1285 TREEVIEW_ITEM *previousChild = NULL;
1286 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1287 BOOL bItemInserted = FALSE;
1289 aChild = parentItem->firstChild;
1291 bTextUpdated = TRUE;
1292 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1294 /* Iterate the parent children to see where we fit in */
1295 while (aChild != NULL)
1299 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1300 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1302 if (comp < 0) /* we are smaller than the current one */
1304 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1305 if (infoPtr->firstVisible == originalFirst &&
1306 aChild == originalFirst)
1307 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1308 bItemInserted = TRUE;
1311 else if (comp > 0) /* we are bigger than the current one */
1313 previousChild = aChild;
1315 /* This will help us to exit if there is no more sibling */
1316 aChild = (aChild->nextSibling == 0)
1318 : aChild->nextSibling;
1320 /* Look at the next item */
1326 * An item with this name is already existing, therefore,
1327 * we add after the one we found
1329 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1330 bItemInserted = TRUE;
1336 * we reach the end of the child list and the item has not
1337 * yet been inserted, therefore, insert it after the last child.
1339 if ((!bItemInserted) && (aChild == NULL))
1340 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1347 TRACE("new item %p; parent %p, mask %x\n", newItem,
1348 newItem->parent, tvItem->mask);
1350 newItem->iLevel = newItem->parent->iLevel + 1;
1352 if (newItem->parent->cChildren == 0)
1353 newItem->parent->cChildren = 1;
1355 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1357 if (STATEIMAGEINDEX(newItem->state) == 0)
1358 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1361 if (infoPtr->firstVisible == NULL)
1362 infoPtr->firstVisible = newItem;
1364 TREEVIEW_VerifyTree(infoPtr);
1366 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1368 if (parentItem == infoPtr->root ||
1369 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1371 TREEVIEW_ITEM *item;
1372 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1374 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1375 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1378 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1380 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1381 TREEVIEW_UpdateScrollBars(infoPtr);
1383 * if the item was inserted in a visible part of the tree,
1384 * invalidate it, as well as those after it
1386 for (item = newItem;
1388 item = TREEVIEW_GetNextListItem(infoPtr, item))
1389 TREEVIEW_Invalidate(infoPtr, item);
1393 /* refresh treeview if newItem is the first item inserted under parentItem */
1394 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1396 /* parent got '+' - update it */
1397 TREEVIEW_Invalidate(infoPtr, parentItem);
1401 return (LRESULT)newItem;
1404 /* Item Deletion ************************************************************/
1406 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1409 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1411 TREEVIEW_ITEM *kill = parentItem->firstChild;
1413 while (kill != NULL)
1415 TREEVIEW_ITEM *next = kill->nextSibling;
1417 TREEVIEW_RemoveItem(infoPtr, kill);
1422 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1423 assert(parentItem->firstChild == NULL);
1424 assert(parentItem->lastChild == NULL);
1428 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1430 TREEVIEW_ITEM *parentItem = item->parent;
1432 assert(item != NULL);
1433 assert(item->parent != NULL); /* i.e. it must not be the root */
1435 if (parentItem->firstChild == item)
1436 parentItem->firstChild = item->nextSibling;
1438 if (parentItem->lastChild == item)
1439 parentItem->lastChild = item->prevSibling;
1441 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1442 && parentItem->cChildren > 0)
1443 parentItem->cChildren = 0;
1445 if (item->prevSibling)
1446 item->prevSibling->nextSibling = item->nextSibling;
1448 if (item->nextSibling)
1449 item->nextSibling->prevSibling = item->prevSibling;
1453 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1455 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1457 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1458 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1460 if (wineItem->firstChild)
1461 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1463 TREEVIEW_UnlinkItem(wineItem);
1465 infoPtr->uNumItems--;
1467 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1468 Free(wineItem->pszText);
1470 TREEVIEW_FreeItem(infoPtr, wineItem);
1474 /* Empty out the tree. */
1476 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1478 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1480 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1484 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1486 TREEVIEW_ITEM *newSelection = NULL;
1487 TREEVIEW_ITEM *newFirstVisible = NULL;
1488 TREEVIEW_ITEM *parent, *prev = NULL;
1489 BOOL visible = FALSE;
1491 if (wineItem == TVI_ROOT)
1493 TRACE("TVI_ROOT\n");
1494 parent = infoPtr->root;
1495 newSelection = NULL;
1497 TREEVIEW_RemoveTree(infoPtr);
1501 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1504 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1505 parent = wineItem->parent;
1507 if (ISVISIBLE(wineItem))
1509 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1513 if (infoPtr->selectedItem != NULL
1514 && (wineItem == infoPtr->selectedItem
1515 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1517 if (wineItem->nextSibling)
1518 newSelection = wineItem->nextSibling;
1519 else if (wineItem->parent != infoPtr->root)
1520 newSelection = wineItem->parent;
1522 newSelection = wineItem->prevSibling;
1523 TRACE("newSelection = %p\n", newSelection);
1526 if (infoPtr->firstVisible == wineItem)
1528 if (wineItem->nextSibling)
1529 newFirstVisible = wineItem->nextSibling;
1530 else if (wineItem->prevSibling)
1531 newFirstVisible = wineItem->prevSibling;
1532 else if (wineItem->parent != infoPtr->root)
1533 newFirstVisible = wineItem->parent;
1534 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1537 newFirstVisible = infoPtr->firstVisible;
1539 TREEVIEW_RemoveItem(infoPtr, wineItem);
1542 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1543 if (!infoPtr->selectedItem && newSelection)
1545 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1546 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1549 /* Validate insertMark dropItem.
1550 * hotItem ??? - used for comparison only.
1552 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1553 infoPtr->insertMarkItem = 0;
1555 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1556 infoPtr->dropItem = 0;
1558 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1559 newFirstVisible = infoPtr->root->firstChild;
1561 TREEVIEW_VerifyTree(infoPtr);
1563 if (!infoPtr->bRedraw) return TRUE;
1567 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1568 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1569 TREEVIEW_UpdateScrollBars(infoPtr);
1570 TREEVIEW_Invalidate(infoPtr, NULL);
1572 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1574 /* parent lost '+/-' - update it */
1575 TREEVIEW_Invalidate(infoPtr, parent);
1582 /* Get/Set Messages *********************************************************/
1584 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1586 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1588 if (infoPtr->bRedraw)
1590 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1591 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1592 TREEVIEW_UpdateScrollBars(infoPtr);
1593 TREEVIEW_Invalidate(infoPtr, NULL);
1599 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1602 return infoPtr->uIndent;
1606 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1610 if (newIndent < MINIMUM_INDENT)
1611 newIndent = MINIMUM_INDENT;
1613 if (infoPtr->uIndent != newIndent)
1615 infoPtr->uIndent = newIndent;
1616 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1617 TREEVIEW_UpdateScrollBars(infoPtr);
1618 TREEVIEW_Invalidate(infoPtr, NULL);
1626 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1629 return (LRESULT)infoPtr->hwndToolTip;
1633 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1638 prevToolTip = infoPtr->hwndToolTip;
1639 infoPtr->hwndToolTip = hwndTT;
1641 return (LRESULT)prevToolTip;
1645 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1647 BOOL rc = infoPtr->bNtfUnicode;
1648 infoPtr->bNtfUnicode = fUnicode;
1653 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1655 return infoPtr->bNtfUnicode;
1659 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1661 return infoPtr->uScrollTime;
1665 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1667 UINT uOldScrollTime = infoPtr->uScrollTime;
1669 infoPtr->uScrollTime = min(uScrollTime, 100);
1671 return uOldScrollTime;
1676 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1683 return (LRESULT)infoPtr->himlNormal;
1686 return (LRESULT)infoPtr->himlState;
1693 #define TVHEIGHT_MIN 16
1694 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1696 /* Compute the natural height for items. */
1698 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1702 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1705 /* Height is the maximum of:
1706 * 16 (a hack because our fonts are tiny), and
1707 * The text height + border & margin, and
1708 * The size of the normal image list
1710 GetTextMetricsW(hdc, &tm);
1711 SelectObject(hdc, hOldFont);
1714 height = TVHEIGHT_MIN;
1715 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1716 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1717 if (height < infoPtr->normalImageHeight)
1718 height = infoPtr->normalImageHeight;
1720 /* Round down, unless we support odd ("non even") heights. */
1721 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1728 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1730 HIMAGELIST himlOld = 0;
1731 int oldWidth = infoPtr->normalImageWidth;
1732 int oldHeight = infoPtr->normalImageHeight;
1735 TRACE("%lx,%p\n", wParam, himlNew);
1740 himlOld = infoPtr->himlNormal;
1741 infoPtr->himlNormal = himlNew;
1743 if (himlNew != NULL)
1744 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1745 &infoPtr->normalImageHeight);
1748 infoPtr->normalImageWidth = 0;
1749 infoPtr->normalImageHeight = 0;
1755 himlOld = infoPtr->himlState;
1756 infoPtr->himlState = himlNew;
1758 if (himlNew != NULL)
1759 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1760 &infoPtr->stateImageHeight);
1763 infoPtr->stateImageWidth = 0;
1764 infoPtr->stateImageHeight = 0;
1770 if (oldWidth != infoPtr->normalImageWidth ||
1771 oldHeight != infoPtr->normalImageHeight)
1773 BOOL bRecalcVisible = FALSE;
1775 if (oldHeight != infoPtr->normalImageHeight &&
1776 !infoPtr->bHeightSet)
1778 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1779 bRecalcVisible = TRUE;
1782 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1783 infoPtr->normalImageWidth != infoPtr->uIndent)
1785 infoPtr->uIndent = infoPtr->normalImageWidth;
1786 bRecalcVisible = TRUE;
1790 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1792 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1793 TREEVIEW_UpdateScrollBars(infoPtr);
1796 TREEVIEW_Invalidate(infoPtr, NULL);
1798 return (LRESULT)himlOld;
1802 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1804 INT prevHeight = infoPtr->uItemHeight;
1806 TRACE("%d\n", newHeight);
1807 if (newHeight == -1)
1809 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1810 infoPtr->bHeightSet = FALSE;
1814 infoPtr->uItemHeight = newHeight;
1815 infoPtr->bHeightSet = TRUE;
1818 /* Round down, unless we support odd ("non even") heights. */
1819 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1820 infoPtr->uItemHeight &= ~1;
1822 if (infoPtr->uItemHeight != prevHeight)
1824 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1825 TREEVIEW_UpdateScrollBars(infoPtr);
1826 TREEVIEW_Invalidate(infoPtr, NULL);
1833 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1836 return infoPtr->uItemHeight;
1841 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1843 TRACE("%p\n", infoPtr->hFont);
1844 return (LRESULT)infoPtr->hFont;
1849 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1853 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1859 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1861 UINT uHeight = infoPtr->uItemHeight;
1863 TRACE("%p %i\n", hFont, bRedraw);
1865 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1867 DeleteObject(infoPtr->hBoldFont);
1868 DeleteObject(infoPtr->hUnderlineFont);
1869 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1870 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1872 if (!infoPtr->bHeightSet)
1873 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1875 if (uHeight != infoPtr->uItemHeight)
1876 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1878 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1880 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1881 TREEVIEW_UpdateScrollBars(infoPtr);
1884 TREEVIEW_Invalidate(infoPtr, NULL);
1891 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1894 return (LRESULT)infoPtr->clrLine;
1898 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1900 COLORREF prevColor = infoPtr->clrLine;
1903 infoPtr->clrLine = color;
1904 return (LRESULT)prevColor;
1909 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1912 return (LRESULT)infoPtr->clrText;
1916 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1918 COLORREF prevColor = infoPtr->clrText;
1921 infoPtr->clrText = color;
1923 if (infoPtr->clrText != prevColor)
1924 TREEVIEW_Invalidate(infoPtr, NULL);
1926 return (LRESULT)prevColor;
1931 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1934 return (LRESULT)infoPtr->clrBk;
1938 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1940 COLORREF prevColor = infoPtr->clrBk;
1943 infoPtr->clrBk = newColor;
1945 if (newColor != prevColor)
1946 TREEVIEW_Invalidate(infoPtr, NULL);
1948 return (LRESULT)prevColor;
1953 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1956 return (LRESULT)infoPtr->clrInsertMark;
1960 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1962 COLORREF prevColor = infoPtr->clrInsertMark;
1964 TRACE("%x\n", color);
1965 infoPtr->clrInsertMark = color;
1967 return (LRESULT)prevColor;
1972 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1974 TRACE("%d %p\n", wParam, item);
1976 if (!TREEVIEW_ValidItem(infoPtr, item))
1979 infoPtr->insertBeforeorAfter = wParam;
1980 infoPtr->insertMarkItem = item;
1982 TREEVIEW_Invalidate(infoPtr, NULL);
1988 /************************************************************************
1989 * Some serious braindamage here. lParam is a pointer to both the
1990 * input HTREEITEM and the output RECT.
1993 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1995 TREEVIEW_ITEM *wineItem;
1996 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2000 * validate parameters
2006 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2010 * If wParam is TRUE return the text size otherwise return
2011 * the whole item size
2015 /* Windows does not send TVN_GETDISPINFO here. */
2017 lpRect->top = wineItem->rect.top;
2018 lpRect->bottom = wineItem->rect.bottom;
2020 lpRect->left = wineItem->textOffset;
2021 if (!wineItem->textWidth)
2022 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2024 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2028 *lpRect = wineItem->rect;
2031 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2036 static inline LRESULT
2037 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2039 /* Surprise! This does not take integral height into account. */
2040 return infoPtr->clientHeight / infoPtr->uItemHeight;
2045 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2047 TREEVIEW_ITEM *wineItem;
2049 wineItem = tvItem->hItem;
2050 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2053 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2055 if (tvItem->mask & TVIF_CHILDREN)
2057 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2058 FIXME("I_CHILDRENCALLBACK not supported\n");
2059 tvItem->cChildren = wineItem->cChildren;
2062 if (tvItem->mask & TVIF_HANDLE)
2063 tvItem->hItem = wineItem;
2065 if (tvItem->mask & TVIF_IMAGE)
2066 tvItem->iImage = wineItem->iImage;
2068 if (tvItem->mask & TVIF_INTEGRAL)
2069 tvItem->iIntegral = wineItem->iIntegral;
2071 /* undocumented: windows ignores TVIF_PARAM and
2072 * * always sets lParam
2074 tvItem->lParam = wineItem->lParam;
2076 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2077 tvItem->iSelectedImage = wineItem->iSelectedImage;
2079 if (tvItem->mask & TVIF_STATE)
2080 /* Careful here - Windows ignores the stateMask when you get the state
2081 That contradicts the documentation, but makes more common sense, masking
2082 retrieval in this way seems overkill */
2083 tvItem->state = wineItem->state;
2085 if (tvItem->mask & TVIF_TEXT)
2087 if (wineItem->pszText == NULL)
2089 if (tvItem->cchTextMax > 0)
2090 tvItem->pszText[0] = '\0';
2094 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2096 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2097 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2101 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2106 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2108 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2109 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2113 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2114 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2118 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2119 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2124 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2125 * which is wrong. */
2127 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2129 TREEVIEW_ITEM *wineItem;
2130 TREEVIEW_ITEM originalItem;
2132 wineItem = tvItem->hItem;
2134 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2137 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2140 /* store the original item values */
2141 originalItem = *wineItem;
2143 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2146 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2147 if ((tvItem->mask & TVIF_TEXT
2148 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2149 && ISVISIBLE(wineItem))
2151 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2152 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2155 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2157 /* The refresh updates everything, but we can't wait until then. */
2158 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2160 /* if any of the item's values changed and it's not a callback, redraw the item */
2161 if (item_changed(&originalItem, wineItem, tvItem))
2163 if (tvItem->mask & TVIF_INTEGRAL)
2165 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2166 TREEVIEW_UpdateScrollBars(infoPtr);
2168 TREEVIEW_Invalidate(infoPtr, NULL);
2172 TREEVIEW_UpdateScrollBars(infoPtr);
2173 TREEVIEW_Invalidate(infoPtr, wineItem);
2182 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2186 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2189 return (wineItem->state & mask);
2193 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2195 TREEVIEW_ITEM *retval;
2199 /* handle all the global data here */
2202 case TVGN_CHILD: /* Special case: child of 0 is root */
2207 retval = infoPtr->root->firstChild;
2211 retval = infoPtr->selectedItem;
2214 case TVGN_FIRSTVISIBLE:
2215 retval = infoPtr->firstVisible;
2218 case TVGN_DROPHILITE:
2219 retval = infoPtr->dropItem;
2222 case TVGN_LASTVISIBLE:
2223 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2229 TRACE("flags:%x, returns %p\n", which, retval);
2230 return (LRESULT)retval;
2233 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2235 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2241 retval = wineItem->nextSibling;
2244 retval = wineItem->prevSibling;
2247 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2250 retval = wineItem->firstChild;
2252 case TVGN_NEXTVISIBLE:
2253 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2255 case TVGN_PREVIOUSVISIBLE:
2256 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2259 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2263 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2264 return (LRESULT)retval;
2269 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2271 TRACE(" %d\n", infoPtr->uNumItems);
2272 return (LRESULT)infoPtr->uNumItems;
2276 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2278 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2280 static const unsigned int state_table[] = { 0, 2, 1 };
2284 state = STATEIMAGEINDEX(item->state);
2285 TRACE("state:%x\n", state);
2286 item->state &= ~TVIS_STATEIMAGEMASK;
2289 state = state_table[state];
2291 item->state |= INDEXTOSTATEIMAGEMASK(state);
2293 TRACE("state:%x\n", state);
2294 TREEVIEW_Invalidate(infoPtr, item);
2299 /* Painting *************************************************************/
2301 /* Draw the lines and expand button for an item. Also draws one section
2302 * of the line from item's parent to item's parent's next sibling. */
2304 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2306 LONG centerx, centery;
2307 BOOL lar = ((infoPtr->dwStyle
2308 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2311 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2313 if (!lar && item->iLevel == 0)
2316 hbr = CreateSolidBrush(clrBk);
2317 hbrOld = SelectObject(hdc, hbr);
2319 centerx = (item->linesOffset + item->stateOffset) / 2;
2320 centery = (item->rect.top + item->rect.bottom) / 2;
2322 if (infoPtr->dwStyle & TVS_HASLINES)
2324 HPEN hOldPen, hNewPen;
2328 /* Get a dotted grey pen */
2329 lb.lbStyle = BS_SOLID;
2330 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2331 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2332 hOldPen = SelectObject(hdc, hNewPen);
2334 /* Make sure the center is on a dot (using +2 instead
2335 * of +1 gives us pixel-by-pixel compat with native) */
2336 centery = (centery + 2) & ~1;
2338 MoveToEx(hdc, item->stateOffset, centery, NULL);
2339 LineTo(hdc, centerx - 1, centery);
2341 if (item->prevSibling || item->parent != infoPtr->root)
2343 MoveToEx(hdc, centerx, item->rect.top, NULL);
2344 LineTo(hdc, centerx, centery);
2347 if (item->nextSibling)
2349 MoveToEx(hdc, centerx, centery, NULL);
2350 LineTo(hdc, centerx, item->rect.bottom + 1);
2353 /* Draw the line from our parent to its next sibling. */
2354 parent = item->parent;
2355 while (parent != infoPtr->root)
2357 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2359 if (parent->nextSibling
2360 /* skip top-levels unless TVS_LINESATROOT */
2361 && parent->stateOffset > parent->linesOffset)
2363 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2364 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2367 parent = parent->parent;
2370 SelectObject(hdc, hOldPen);
2371 DeleteObject(hNewPen);
2375 * Display the (+/-) signs
2378 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2380 if (item->cChildren)
2382 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2385 RECT glyphRect = item->rect;
2386 glyphRect.left = item->linesOffset;
2387 glyphRect.right = item->stateOffset;
2388 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2389 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2394 LONG height = item->rect.bottom - item->rect.top;
2395 LONG width = item->stateOffset - item->linesOffset;
2396 LONG rectsize = min(height, width) / 4;
2397 /* plussize = ceil(rectsize * 3/4) */
2398 LONG plussize = (rectsize + 1) * 3 / 4;
2400 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2401 HPEN old_pen = SelectObject(hdc, new_pen);
2403 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2404 centerx + rectsize + 2, centery + rectsize + 2);
2406 SelectObject(hdc, old_pen);
2407 DeleteObject(new_pen);
2409 /* draw +/- signs with current text color */
2410 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2411 old_pen = SelectObject(hdc, new_pen);
2413 if (height < 18 || width < 18)
2415 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2416 LineTo(hdc, centerx + plussize, centery);
2418 if (!(item->state & TVIS_EXPANDED))
2420 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2421 LineTo(hdc, centerx, centery + plussize);
2426 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2427 centerx + plussize, centery + 2);
2429 if (!(item->state & TVIS_EXPANDED))
2431 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2432 centerx + 2, centery + plussize);
2433 SetPixel(hdc, centerx - 1, centery, clrBk);
2434 SetPixel(hdc, centerx + 1, centery, clrBk);
2438 SelectObject(hdc, old_pen);
2439 DeleteObject(new_pen);
2443 SelectObject(hdc, hbrOld);
2448 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2452 COLORREF oldTextColor, oldTextBkColor;
2454 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2455 NMTVCUSTOMDRAW nmcdhdr;
2457 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2459 /* - If item is drop target or it is selected and window is in focus -
2460 * use blue background (COLOR_HIGHLIGHT).
2461 * - If item is selected, window is not in focus, but it has style
2462 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2463 * - Otherwise - use background color
2465 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2466 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2467 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2469 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2471 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2472 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2476 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2477 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2482 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2483 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2484 nmcdhdr.clrText = comctl32_color.clrHighlight;
2486 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2489 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2491 /* The custom draw handler can query the text rectangle,
2493 /* should already be known, set to 0 when changed */
2494 if (!wineItem->textWidth)
2495 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2499 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2501 cditem = TREEVIEW_SendCustomDrawItemNotify
2502 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2503 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2505 if (cditem & CDRF_SKIPDEFAULT)
2507 SelectObject(hdc, hOldFont);
2512 if (cditem & CDRF_NEWFONT)
2513 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2515 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2517 /* Set colors. Custom draw handler can change these so we do this after it. */
2518 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2519 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2521 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2524 * Display the images associated with this item
2529 /* State images are displayed to the left of the Normal image
2530 * image number is in state; zero should be `display no image'.
2532 imageIndex = STATEIMAGEINDEX(wineItem->state);
2534 if (infoPtr->himlState && imageIndex)
2536 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2537 wineItem->stateOffset,
2538 centery - infoPtr->stateImageHeight / 2,
2542 /* Now, draw the normal image; can be either selected or
2543 * non-selected image.
2546 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2548 /* The item is currently selected */
2549 imageIndex = wineItem->iSelectedImage;
2553 /* The item is not selected */
2554 imageIndex = wineItem->iImage;
2557 if (infoPtr->himlNormal)
2559 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2561 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2562 wineItem->imageOffset,
2563 centery - infoPtr->normalImageHeight / 2,
2564 ILD_NORMAL | ovlIdx);
2570 * Display the text associated with this item
2573 /* Don't paint item's text if it's being edited */
2574 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2576 if (wineItem->pszText)
2580 rcText.top = wineItem->rect.top;
2581 rcText.bottom = wineItem->rect.bottom;
2582 rcText.left = wineItem->textOffset;
2583 rcText.right = rcText.left + wineItem->textWidth + 4;
2585 TRACE("drawing text %s at (%s)\n",
2586 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2589 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2590 ETO_CLIPPED | ETO_OPAQUE,
2593 lstrlenW(wineItem->pszText),
2596 /* Draw the box around the selected item */
2597 if ((wineItem == infoPtr->selectedItem) && inFocus)
2599 DrawFocusRect(hdc,&rcText);
2605 /* Draw insertion mark if necessary */
2607 if (infoPtr->insertMarkItem)
2608 TRACE("item:%d,mark:%p\n",
2609 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2610 infoPtr->insertMarkItem);
2612 if (wineItem == infoPtr->insertMarkItem)
2614 HPEN hNewPen, hOldPen;
2618 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2619 hOldPen = SelectObject(hdc, hNewPen);
2621 if (infoPtr->insertBeforeorAfter)
2622 offset = wineItem->rect.bottom - 1;
2624 offset = wineItem->rect.top + 1;
2626 left = wineItem->textOffset - 2;
2627 right = wineItem->textOffset + wineItem->textWidth + 2;
2629 MoveToEx(hdc, left, offset - 3, NULL);
2630 LineTo(hdc, left, offset + 4);
2632 MoveToEx(hdc, left, offset, NULL);
2633 LineTo(hdc, right + 1, offset);
2635 MoveToEx(hdc, right, offset + 3, NULL);
2636 LineTo(hdc, right, offset - 4);
2638 SelectObject(hdc, hOldPen);
2639 DeleteObject(hNewPen);
2642 if (cditem & CDRF_NOTIFYPOSTPAINT)
2644 cditem = TREEVIEW_SendCustomDrawItemNotify
2645 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2646 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2649 /* Restore the hdc state */
2650 SetTextColor(hdc, oldTextColor);
2651 SetBkColor(hdc, oldTextBkColor);
2652 SelectObject(hdc, hOldFont);
2655 /* Computes treeHeight and treeWidth and updates the scroll bars.
2658 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2660 TREEVIEW_ITEM *wineItem;
2661 HWND hwnd = infoPtr->hwnd;
2665 LONG scrollX = infoPtr->scrollX;
2667 infoPtr->treeWidth = 0;
2668 infoPtr->treeHeight = 0;
2670 /* We iterate through all visible items in order to get the tree height
2672 wineItem = infoPtr->root->firstChild;
2674 while (wineItem != NULL)
2676 if (ISVISIBLE(wineItem))
2678 /* actually we draw text at textOffset + 2 */
2679 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2680 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2682 /* This is scroll-adjusted, but we fix this below. */
2683 infoPtr->treeHeight = wineItem->rect.bottom;
2686 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2689 /* Fix the scroll adjusted treeHeight and treeWidth. */
2690 if (infoPtr->root->firstChild)
2691 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2693 infoPtr->treeWidth += infoPtr->scrollX;
2695 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2697 /* Adding one scroll bar may take up enough space that it forces us
2698 * to add the other as well. */
2699 if (infoPtr->treeHeight > infoPtr->clientHeight)
2703 if (infoPtr->treeWidth
2704 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2707 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2710 if (!vert && horz && infoPtr->treeHeight
2711 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2714 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2716 si.cbSize = sizeof(SCROLLINFO);
2717 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2722 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2723 if ( si.nPage && NULL != infoPtr->firstVisible)
2725 si.nPos = infoPtr->firstVisible->visibleOrder;
2726 si.nMax = infoPtr->maxVisibleOrder - 1;
2728 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2730 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2731 ShowScrollBar(hwnd, SB_VERT, TRUE);
2732 infoPtr->uInternalStatus |= TV_VSCROLL;
2736 if (infoPtr->uInternalStatus & TV_VSCROLL)
2737 ShowScrollBar(hwnd, SB_VERT, FALSE);
2738 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2743 if (infoPtr->uInternalStatus & TV_VSCROLL)
2744 ShowScrollBar(hwnd, SB_VERT, FALSE);
2745 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2750 si.nPage = infoPtr->clientWidth;
2751 si.nPos = infoPtr->scrollX;
2752 si.nMax = infoPtr->treeWidth - 1;
2754 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2756 si.nPos = si.nMax - max( si.nPage-1, 0 );
2760 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2761 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2762 infoPtr->uInternalStatus |= TV_HSCROLL;
2764 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2765 TREEVIEW_HScroll(infoPtr,
2766 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2770 if (infoPtr->uInternalStatus & TV_HSCROLL)
2771 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2772 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2775 if (infoPtr->scrollX != 0)
2777 TREEVIEW_HScroll(infoPtr,
2778 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2783 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2787 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2790 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2792 hBrush = CreateSolidBrush(clrBk);
2793 FillRect(hdc, rc, hBrush);
2794 DeleteObject(hBrush);
2797 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2799 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2803 TRACE("%p\n", infoPtr);
2805 GetClientRect(infoPtr->hwnd, &rect);
2806 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2812 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2814 HWND hwnd = infoPtr->hwnd;
2816 TREEVIEW_ITEM *wineItem;
2818 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2820 TRACE("empty window\n");
2824 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2827 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2829 ReleaseDC(hwnd, hdc);
2833 for (wineItem = infoPtr->root->firstChild;
2835 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2837 if (ISVISIBLE(wineItem))
2839 /* Avoid unneeded calculations */
2840 if (wineItem->rect.top > rect.bottom)
2842 if (wineItem->rect.bottom < rect.top)
2845 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2849 TREEVIEW_UpdateScrollBars(infoPtr);
2851 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2853 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2857 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2859 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2863 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2866 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2868 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2872 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2878 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2883 GetClientRect(infoPtr->hwnd, &rc);
2887 hdc = BeginPaint(infoPtr->hwnd, &ps);
2890 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2893 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2894 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2897 EndPaint(infoPtr->hwnd, &ps);
2903 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2905 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2907 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2910 if (options & PRF_ERASEBKGND)
2911 TREEVIEW_EraseBackground(infoPtr, hdc);
2913 if (options & PRF_CLIENT)
2916 GetClientRect(infoPtr->hwnd, &rc);
2917 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2923 /* Sorting **************************************************************/
2925 /***************************************************************************
2926 * Forward the DPA local callback to the treeview owner callback
2929 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2930 const TVSORTCB *pCallBackSort)
2932 /* Forward the call to the client-defined callback */
2933 return pCallBackSort->lpfnCompare(first->lParam,
2935 pCallBackSort->lParam);
2938 /***************************************************************************
2939 * Treeview native sort routine: sort on item text.
2942 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2943 const TREEVIEW_INFO *infoPtr)
2945 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2946 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2948 if(first->pszText && second->pszText)
2949 return lstrcmpiW(first->pszText, second->pszText);
2950 else if(first->pszText)
2952 else if(second->pszText)
2958 /* Returns the number of physical children belonging to item. */
2960 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
2965 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2971 /* Returns a DPA containing a pointer to each physical child of item in
2972 * sibling order. If item has no children, an empty DPA is returned. */
2974 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
2976 HTREEITEM child = item->firstChild;
2978 HDPA list = DPA_Create(8);
2979 if (list == 0) return NULL;
2981 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2983 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2993 /***************************************************************************
2994 * Setup the treeview structure with regards of the sort method
2995 * and sort the children of the TV item specified in lParam
2996 * fRecurse: currently unused. Should be zero.
2997 * parent: if pSort!=NULL, should equal pSort->hParent.
2998 * otherwise, item which child items are to be sorted.
2999 * pSort: sort method info. if NULL, sort on item text.
3000 * if non-NULL, sort on item's lParam content, and let the
3001 * application decide what that means. See also TVM_SORTCHILDRENCB.
3005 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3009 PFNDPACOMPARE pfnCompare;
3012 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3013 if (parent == TVI_ROOT || parent == NULL)
3014 parent = infoPtr->root;
3016 /* Check for a valid handle to the parent item */
3017 if (!TREEVIEW_ValidItem(infoPtr, parent))
3019 ERR("invalid item hParent=%p\n", parent);
3025 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3026 lpCompare = (LPARAM)pSort;
3030 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3031 lpCompare = (LPARAM)infoPtr;
3034 cChildren = TREEVIEW_CountChildren(parent);
3036 /* Make sure there is something to sort */
3039 /* TREEVIEW_ITEM rechaining */
3042 HTREEITEM nextItem = 0;
3043 HTREEITEM prevItem = 0;
3045 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3047 if (sortList == NULL)
3050 /* let DPA sort the list */
3051 DPA_Sort(sortList, pfnCompare, lpCompare);
3053 /* The order of DPA entries has been changed, so fixup the
3054 * nextSibling and prevSibling pointers. */
3056 item = DPA_GetPtr(sortList, count++);
3057 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3059 /* link the two current item together */
3060 item->nextSibling = nextItem;
3061 nextItem->prevSibling = item;
3063 if (prevItem == NULL)
3065 /* this is the first item, update the parent */
3066 parent->firstChild = item;
3067 item->prevSibling = NULL;
3071 /* fix the back chaining */
3072 item->prevSibling = prevItem;
3075 /* get ready for the next one */
3080 /* the last item is pointed to by item and never has a sibling */
3081 item->nextSibling = NULL;
3082 parent->lastChild = item;
3084 DPA_Destroy(sortList);
3086 TREEVIEW_VerifyTree(infoPtr);
3088 if (parent->state & TVIS_EXPANDED)
3090 int visOrder = infoPtr->firstVisible->visibleOrder;
3092 if (parent == infoPtr->root)
3093 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3095 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3097 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3099 TREEVIEW_ITEM *item;
3101 for (item = infoPtr->root->firstChild; item != NULL;
3102 item = TREEVIEW_GetNextListItem(infoPtr, item))
3104 if (item->visibleOrder == visOrder)
3108 if (!item) item = parent->firstChild;
3109 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3112 TREEVIEW_Invalidate(infoPtr, NULL);
3121 /***************************************************************************
3122 * Setup the treeview structure with regards of the sort method
3123 * and sort the children of the TV item specified in lParam
3126 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3128 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3132 /***************************************************************************
3133 * Sort the children of the TV item specified in lParam.
3136 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3138 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3142 /* Expansion/Collapse ***************************************************/
3145 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3148 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3149 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3150 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3155 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3158 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3159 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3160 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3165 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3166 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3168 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3169 BOOL bRemoveChildren, BOOL bUser)
3171 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3172 BOOL bSetSelection, bSetFirstVisible;
3174 LONG scrollDist = 0;
3175 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3177 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3179 if (!(wineItem->state & TVIS_EXPANDED))
3182 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3183 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3185 if (wineItem->firstChild == NULL)
3188 wineItem->state &= ~TVIS_EXPANDED;
3190 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3191 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3193 bSetSelection = (infoPtr->selectedItem != NULL
3194 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3196 bSetFirstVisible = (infoPtr->firstVisible != NULL
3197 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3202 if (tmpItem->nextSibling)
3204 nextItem = tmpItem->nextSibling;
3207 tmpItem = tmpItem->parent;
3211 scrollDist = nextItem->rect.top;
3213 if (bRemoveChildren)
3215 INT old_cChildren = wineItem->cChildren;
3216 TRACE("TVE_COLLAPSERESET\n");
3217 wineItem->state &= ~TVIS_EXPANDEDONCE;
3218 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3219 wineItem->cChildren = old_cChildren;
3222 if (wineItem->firstChild)
3224 TREEVIEW_ITEM *item, *sibling;
3226 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3228 for (item = wineItem->firstChild; item != sibling;
3229 item = TREEVIEW_GetNextListItem(infoPtr, item))
3231 item->visibleOrder = -1;
3235 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3238 scrollDist = -(scrollDist - nextItem->rect.top);
3242 /* Don't call DoSelectItem, it sends notifications. */
3243 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3244 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3245 wineItem->state |= TVIS_SELECTED;
3246 infoPtr->selectedItem = wineItem;
3249 TREEVIEW_UpdateScrollBars(infoPtr);
3251 scrollRect.left = 0;
3252 scrollRect.right = infoPtr->clientWidth;
3253 scrollRect.bottom = infoPtr->clientHeight;
3257 scrollRect.top = nextItem->rect.top;
3259 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3260 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3261 TREEVIEW_Invalidate(infoPtr, wineItem);
3263 scrollRect.top = wineItem->rect.top;
3264 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3267 TREEVIEW_SetFirstVisible(infoPtr,
3268 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3275 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3276 BOOL bExpandPartial, BOOL bUser)
3279 LONG orgNextTop = 0;
3281 TREEVIEW_ITEM *nextItem, *tmpItem;
3283 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, wineItem, bExpandPartial, bUser);
3285 if (wineItem->state & TVIS_EXPANDED)
3288 tmpItem = wineItem; nextItem = NULL;
3291 if (tmpItem->nextSibling)
3293 nextItem = tmpItem->nextSibling;
3296 tmpItem = tmpItem->parent;
3300 orgNextTop = nextItem->rect.top;
3302 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3304 if (bUser || ((wineItem->cChildren != 0) &&
3305 !(wineItem->state & TVIS_EXPANDEDONCE)))
3307 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3309 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3313 if (!wineItem->firstChild)
3316 wineItem->state |= TVIS_EXPANDED;
3317 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3318 wineItem->state |= TVIS_EXPANDEDONCE;
3322 if (!wineItem->firstChild)
3325 /* this item has already been expanded */
3326 wineItem->state |= TVIS_EXPANDED;
3330 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3332 if (ISVISIBLE(wineItem))
3334 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3335 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3336 TREEVIEW_UpdateScrollBars(infoPtr);
3338 scrollRect.left = 0;
3339 scrollRect.bottom = infoPtr->treeHeight;
3340 scrollRect.right = infoPtr->clientWidth;
3343 scrollDist = nextItem->rect.top - orgNextTop;
3344 scrollRect.top = orgNextTop;
3346 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3347 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3348 TREEVIEW_Invalidate (infoPtr, wineItem);
3350 scrollRect.top = wineItem->rect.top;
3351 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3354 /* Scroll up so that as many children as possible are visible.
3355 * This fails when expanding causes an HScroll bar to appear, but we
3356 * don't know that yet, so the last item is obscured. */
3357 if (wineItem->firstChild != NULL)
3359 int nChildren = wineItem->lastChild->visibleOrder
3360 - wineItem->firstChild->visibleOrder + 1;
3362 int visible_pos = wineItem->visibleOrder
3363 - infoPtr->firstVisible->visibleOrder;
3365 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3367 if (visible_pos > 0 && nChildren > rows_below)
3369 int scroll = nChildren - rows_below;
3371 if (scroll > visible_pos)
3372 scroll = visible_pos;
3376 TREEVIEW_ITEM *newFirstVisible
3377 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3381 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3391 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3395 if (wineItem->state & TVIS_EXPANDED)
3396 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3398 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3402 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3404 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3406 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3408 if (TREEVIEW_HasChildren(infoPtr, item))
3409 TREEVIEW_ExpandAll(infoPtr, item);
3413 /* Note:If the specified item is the child of a collapsed parent item,
3414 the parent's list of child items is (recursively) expanded to reveal the
3415 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3416 know if it also applies here.
3420 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3422 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3425 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3426 TREEVIEW_ItemName(wineItem), TREEVIEW_GetItemIndex(infoPtr, wineItem),
3427 flag, wineItem->state);
3429 switch (flag & TVE_TOGGLE)
3432 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3436 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3440 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3447 /* Hit-Testing **********************************************************/
3449 static TREEVIEW_ITEM *
3450 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3452 TREEVIEW_ITEM *wineItem;
3455 if (!infoPtr->firstVisible)
3458 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3460 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3461 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3463 if (row >= wineItem->visibleOrder
3464 && row < wineItem->visibleOrder + wineItem->iIntegral)
3472 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3474 TREEVIEW_ITEM *wineItem;
3480 GetClientRect(infoPtr->hwnd, &rect);
3487 status |= TVHT_TOLEFT;
3489 else if (x > rect.right)
3491 status |= TVHT_TORIGHT;
3496 status |= TVHT_ABOVE;
3498 else if (y > rect.bottom)
3500 status |= TVHT_BELOW;
3505 lpht->flags = status;
3509 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3512 lpht->flags = TVHT_NOWHERE;
3516 if (x >= wineItem->textOffset + wineItem->textWidth)
3518 lpht->flags = TVHT_ONITEMRIGHT;
3520 else if (x >= wineItem->textOffset)
3522 lpht->flags = TVHT_ONITEMLABEL;
3524 else if (x >= wineItem->imageOffset)
3526 lpht->flags = TVHT_ONITEMICON;
3528 else if (x >= wineItem->stateOffset)
3530 lpht->flags = TVHT_ONITEMSTATEICON;
3532 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3534 lpht->flags = TVHT_ONITEMBUTTON;
3538 lpht->flags = TVHT_ONITEMINDENT;
3541 lpht->hItem = wineItem;
3542 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3544 return (LRESULT)wineItem;
3547 /* Item Label Editing ***************************************************/
3550 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3552 return (LRESULT)infoPtr->hwndEdit;
3555 static LRESULT CALLBACK
3556 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3558 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3559 BOOL bCancel = FALSE;
3565 TRACE("WM_PAINT start\n");
3566 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3568 TRACE("WM_PAINT done\n");
3572 if (infoPtr->bIgnoreEditKillFocus)
3578 WNDPROC editProc = infoPtr->wpEditOrig;
3579 infoPtr->wpEditOrig = 0;
3580 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3581 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3585 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3588 if (wParam == VK_ESCAPE)
3593 else if (wParam == VK_RETURN)
3600 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3603 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3604 /* eg. Using a messagebox */
3606 infoPtr->bIgnoreEditKillFocus = TRUE;
3607 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3608 infoPtr->bIgnoreEditKillFocus = FALSE;
3614 /* should handle edit control messages here */
3617 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3619 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3621 switch (HIWORD(wParam))
3626 * Adjust the edit window size
3629 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3630 HDC hdc = GetDC(infoPtr->hwndEdit);
3632 HFONT hFont, hOldFont = 0;
3634 TRACE("edit=%p\n", infoPtr->hwndEdit);
3636 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3638 infoPtr->bLabelChanged = TRUE;
3640 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3642 /* Select font to get the right dimension of the string */
3643 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3647 hOldFont = SelectObject(hdc, hFont);
3650 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3652 TEXTMETRICW textMetric;
3654 /* Add Extra spacing for the next character */
3655 GetTextMetricsW(hdc, &textMetric);
3656 sz.cx += (textMetric.tmMaxCharWidth * 2);
3658 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3660 infoPtr->clientWidth - editItem->textOffset + 2);
3662 SetWindowPos(infoPtr->hwndEdit,
3667 editItem->rect.bottom - editItem->rect.top + 3,
3668 SWP_NOMOVE | SWP_DRAWFRAME);
3673 SelectObject(hdc, hOldFont);
3676 ReleaseDC(infoPtr->hwnd, hdc);
3680 /* apparently we should respect passed handle value */
3681 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3683 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3687 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3694 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3696 HWND hwnd = infoPtr->hwnd;
3699 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3702 TEXTMETRICW textMetric;
3704 TRACE("%p %p\n", hwnd, hItem);
3705 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3708 if (infoPtr->hwndEdit)
3709 return infoPtr->hwndEdit;
3711 infoPtr->bLabelChanged = FALSE;
3713 /* make edit item visible */
3714 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3716 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3719 /* Select the font to get appropriate metric dimensions */
3720 if (infoPtr->hFont != 0)
3722 hOldFont = SelectObject(hdc, infoPtr->hFont);
3725 /* Get string length in pixels */
3727 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3730 GetTextExtentPoint32A(hdc, "", 0, &sz);
3732 /* Add Extra spacing for the next character */
3733 GetTextMetricsW(hdc, &textMetric);
3734 sz.cx += (textMetric.tmMaxCharWidth * 2);
3736 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3737 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3739 if (infoPtr->hFont != 0)
3741 SelectObject(hdc, hOldFont);
3744 ReleaseDC(hwnd, hdc);
3746 infoPtr->editItem = hItem;
3748 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3751 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3752 WS_CLIPSIBLINGS | ES_WANTRETURN |
3753 ES_LEFT, hItem->textOffset - 2,
3754 hItem->rect.top - 1, sz.cx + 3,
3755 hItem->rect.bottom -
3756 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3757 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3759 infoPtr->hwndEdit = hwndEdit;
3761 /* Get a 2D border. */
3762 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3763 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3764 SetWindowLongW(hwndEdit, GWL_STYLE,
3765 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3767 SendMessageW(hwndEdit, WM_SETFONT,
3768 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3770 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3772 TREEVIEW_Edit_SubclassProc);
3774 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3776 DestroyWindow(hwndEdit);
3777 infoPtr->hwndEdit = 0;
3778 infoPtr->editItem = NULL;
3783 SetWindowTextW(hwndEdit, hItem->pszText);
3786 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3787 ShowWindow(hwndEdit, SW_SHOW);
3794 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3796 HWND hwnd = infoPtr->hwnd;
3797 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3800 WCHAR tmpText[1024] = { '\0' };
3801 WCHAR *newText = tmpText;
3804 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3806 tvdi.hdr.hwndFrom = hwnd;
3807 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3808 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3810 tvdi.item.hItem = editedItem;
3811 tvdi.item.state = editedItem->state;
3812 tvdi.item.lParam = editedItem->lParam;
3816 if (!infoPtr->bNtfUnicode)
3817 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3819 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3821 if (iLength >= 1023)
3823 ERR("Insufficient space to retrieve new item label\n");
3826 tvdi.item.mask = TVIF_TEXT;
3827 tvdi.item.pszText = tmpText;
3828 tvdi.item.cchTextMax = iLength + 1;
3832 tvdi.item.pszText = NULL;
3833 tvdi.item.cchTextMax = 0;
3836 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3838 if (!bCancel && bCommit) /* Apply the changes */
3840 if (!infoPtr->bNtfUnicode)
3842 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3843 newText = Alloc(len * sizeof(WCHAR));
3844 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3848 if (strcmpW(newText, editedItem->pszText) != 0)
3850 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3853 ERR("OutOfMemory, cannot allocate space for label\n");
3854 if(newText != tmpText) Free(newText);
3855 DestroyWindow(infoPtr->hwndEdit);
3856 infoPtr->hwndEdit = 0;
3857 infoPtr->editItem = NULL;
3862 editedItem->pszText = ptr;
3863 editedItem->cchTextMax = iLength + 1;
3864 strcpyW(editedItem->pszText, newText);
3865 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3868 if(newText != tmpText) Free(newText);
3871 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3872 DestroyWindow(infoPtr->hwndEdit);
3873 infoPtr->hwndEdit = 0;
3874 infoPtr->editItem = NULL;
3879 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3881 if (wParam != TV_EDIT_TIMER)
3883 ERR("got unknown timer\n");
3887 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3888 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3890 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3896 /* Mouse Tracking/Drag **************************************************/
3898 /***************************************************************************
3899 * This is quite unusual piece of code, but that's how it's implemented in
3903 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3905 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3906 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3910 r.top = pt.y - cyDrag;
3911 r.left = pt.x - cxDrag;
3912 r.bottom = pt.y + cyDrag;
3913 r.right = pt.x + cxDrag;
3915 SetCapture(infoPtr->hwnd);
3919 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3921 if (msg.message == WM_MOUSEMOVE)
3923 pt.x = (short)LOWORD(msg.lParam);
3924 pt.y = (short)HIWORD(msg.lParam);
3925 if (PtInRect(&r, pt))
3933 else if (msg.message >= WM_LBUTTONDOWN &&
3934 msg.message <= WM_RBUTTONDBLCLK)
3936 if (msg.message == WM_RBUTTONUP)
3937 TREEVIEW_RButtonUp(infoPtr, &pt);
3941 DispatchMessageW(&msg);
3944 if (GetCapture() != infoPtr->hwnd)
3954 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3956 TREEVIEW_ITEM *wineItem;
3960 SetFocus(infoPtr->hwnd);
3962 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3964 /* If there is pending 'edit label' event - kill it now */
3965 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3968 hit.pt.x = (short)LOWORD(lParam);
3969 hit.pt.y = (short)HIWORD(lParam);
3971 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3974 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3976 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3980 case TVHT_ONITEMRIGHT:
3981 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3984 case TVHT_ONITEMINDENT:
3985 if (!(infoPtr->dwStyle & TVS_HASLINES))
3991 int level = hit.pt.x / infoPtr->uIndent;
3992 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3994 while (wineItem->iLevel > level)
3996 wineItem = wineItem->parent;
4002 case TVHT_ONITEMLABEL:
4003 case TVHT_ONITEMICON:
4004 case TVHT_ONITEMBUTTON:
4005 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4008 case TVHT_ONITEMSTATEICON:
4009 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4010 TREEVIEW_ToggleItemState(infoPtr, wineItem);
4012 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4021 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4023 HWND hwnd = infoPtr->hwnd;
4025 BOOL bTrack, bDoLabelEdit;
4027 /* If Edit control is active - kill it and return.
4028 * The best way to do it is to set focus to itself.
4029 * Edit control subclassed procedure will automatically call
4032 if (infoPtr->hwndEdit)
4038 ht.pt.x = (short)LOWORD(lParam);
4039 ht.pt.y = (short)HIWORD(lParam);
4041 TREEVIEW_HitTest(infoPtr, &ht);
4042 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4044 /* update focusedItem and redraw both items */
4045 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4047 infoPtr->focusedItem = ht.hItem;
4048 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4049 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4052 bTrack = (ht.flags & TVHT_ONITEM)
4053 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4056 * If the style allows editing and the node is already selected
4057 * and the click occurred on the item label...
4059 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4060 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4062 /* Send NM_CLICK right away */
4064 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4067 if (ht.flags & TVHT_ONITEMBUTTON)
4069 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4073 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4074 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4076 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4077 infoPtr->dropItem = ht.hItem;
4079 /* clean up focusedItem as we dragged and won't select this item */
4080 if(infoPtr->focusedItem)
4082 /* refresh the item that was focused */
4083 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4084 infoPtr->focusedItem = NULL;
4086 /* refresh the selected item to return the filled background */
4087 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4094 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4099 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4100 KillTimer(hwnd, TV_EDIT_TIMER);
4102 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4103 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4105 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4108 * if we are TVS_SINGLEEXPAND then we want this single click to
4109 * do a bunch of things.
4111 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4112 (infoPtr->hwndEdit == 0))
4114 TREEVIEW_ITEM *SelItem;
4117 * Send the notification
4119 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4122 * Close the previous selection all the way to the root
4123 * as long as the new selection is not a child
4125 if((infoPtr->selectedItem)
4126 && (infoPtr->selectedItem != ht.hItem))
4128 BOOL closeit = TRUE;
4131 /* determine if the hitItem is a child of the currently selected item */
4132 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4134 closeit = (SelItem != infoPtr->selectedItem);
4135 SelItem = SelItem->parent;
4140 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4141 SelItem = infoPtr->selectedItem;
4143 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4145 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4146 SelItem = SelItem->parent;
4152 * Expand the current item
4154 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4157 /* Select the current item */
4158 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4160 else if (ht.flags & TVHT_ONITEMSTATEICON)
4162 /* TVS_CHECKBOXES requires us to toggle the current state */
4163 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4164 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4174 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4178 if (infoPtr->hwndEdit)
4180 SetFocus(infoPtr->hwnd);
4184 ht.pt.x = (short)LOWORD(lParam);
4185 ht.pt.y = (short)HIWORD(lParam);
4187 TREEVIEW_HitTest(infoPtr, &ht);
4189 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4193 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4194 infoPtr->dropItem = ht.hItem;
4199 SetFocus(infoPtr->hwnd);
4200 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4207 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4213 TREEVIEW_HitTest(infoPtr, &ht);
4217 /* Change to screen coordinate for WM_CONTEXTMENU */
4218 ClientToScreen(infoPtr->hwnd, &ht.pt);
4220 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4221 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4222 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4229 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4231 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4235 HBITMAP hbmp, hOldbmp;
4242 if (!(infoPtr->himlNormal))
4245 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4248 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4250 hwtop = GetDesktopWindow();
4251 htopdc = GetDC(hwtop);
4252 hdc = CreateCompatibleDC(htopdc);
4254 hOldFont = SelectObject(hdc, infoPtr->hFont);
4256 if (dragItem->pszText)
4257 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4260 GetTextExtentPoint32A(hdc, "", 0, &size);
4262 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4263 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4264 hOldbmp = SelectObject(hdc, hbmp);
4266 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4271 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4272 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4276 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4277 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4280 /* draw item text */
4282 SetRect(&rc, cx, 0, size.cx, size.cy);
4284 if (dragItem->pszText)
4285 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4288 SelectObject(hdc, hOldFont);
4289 SelectObject(hdc, hOldbmp);
4291 ImageList_Add(infoPtr->dragList, hbmp, 0);
4295 ReleaseDC(hwtop, htopdc);
4297 return (LRESULT)infoPtr->dragList;
4300 /* Selection ************************************************************/
4303 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4306 TREEVIEW_ITEM *prevSelect;
4308 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4310 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4311 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4312 newSelect ? newSelect->state : 0);
4314 /* reset and redraw focusedItem if focusedItem was set so we don't */
4315 /* have to worry about the previously focused item when we set a new one */
4316 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4317 infoPtr->focusedItem = NULL;
4322 prevSelect = infoPtr->selectedItem;
4324 if (prevSelect == newSelect) {
4325 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4329 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4332 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4338 prevSelect->state &= ~TVIS_SELECTED;
4340 newSelect->state |= TVIS_SELECTED;
4342 infoPtr->selectedItem = newSelect;
4344 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4346 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4347 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4349 TREEVIEW_SendTreeviewNotify(infoPtr,
4352 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4357 case TVGN_DROPHILITE:
4358 prevSelect = infoPtr->dropItem;
4361 prevSelect->state &= ~TVIS_DROPHILITED;
4363 infoPtr->dropItem = newSelect;
4366 newSelect->state |= TVIS_DROPHILITED;
4368 TREEVIEW_Invalidate(infoPtr, prevSelect);
4369 TREEVIEW_Invalidate(infoPtr, newSelect);
4372 case TVGN_FIRSTVISIBLE:
4373 if (newSelect != NULL)
4375 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4376 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4377 TREEVIEW_Invalidate(infoPtr, NULL);
4382 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4386 /* FIXME: handle NM_KILLFOCUS etc */
4388 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4390 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4393 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4395 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4401 /*************************************************************************
4402 * TREEVIEW_ProcessLetterKeys
4404 * Processes keyboard messages generated by pressing the letter keys
4406 * What this does is perform a case insensitive search from the
4407 * current position with the following quirks:
4408 * - If two chars or more are pressed in quick succession we search
4409 * for the corresponding string (e.g. 'abc').
4410 * - If there is a delay we wipe away the current search string and
4411 * restart with just that char.
4412 * - If the user keeps pressing the same character, whether slowly or
4413 * fast, so that the search string is entirely composed of this
4414 * character ('aaaaa' for instance), then we search for first item
4415 * that starting with that character.
4416 * - If the user types the above character in quick succession, then
4417 * we must also search for the corresponding string ('aaaaa'), and
4418 * go to that string if there is a match.
4426 * - The current implementation has a list of characters it will
4427 * accept and it ignores everything else. In particular it will
4428 * ignore accentuated characters which seems to match what
4429 * Windows does. But I'm not sure it makes sense to follow
4431 * - We don't sound a beep when the search fails.
4432 * - The search should start from the focused item, not from the selected
4433 * item. One reason for this is to allow for multiple selections in trees.
4434 * But currently infoPtr->focusedItem does not seem very usable.
4438 * TREEVIEW_ProcessLetterKeys
4440 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4443 HTREEITEM endidx,idx;
4445 WCHAR buffer[MAX_PATH];
4446 DWORD timestamp,elapsed;
4448 /* simple parameter checking */
4449 if (!charCode || !keyData) return 0;
4451 /* only allow the valid WM_CHARs through */
4452 if (!isalnum(charCode) &&
4453 charCode != '.' && charCode != '`' && charCode != '!' &&
4454 charCode != '@' && charCode != '#' && charCode != '$' &&
4455 charCode != '%' && charCode != '^' && charCode != '&' &&
4456 charCode != '*' && charCode != '(' && charCode != ')' &&
4457 charCode != '-' && charCode != '_' && charCode != '+' &&
4458 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4459 charCode != '}' && charCode != '[' && charCode != '{' &&
4460 charCode != '/' && charCode != '?' && charCode != '>' &&
4461 charCode != '<' && charCode != ',' && charCode != '~')
4464 /* compute how much time elapsed since last keypress */
4465 timestamp = GetTickCount();
4466 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4467 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4469 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4472 /* update the search parameters */
4473 infoPtr->lastKeyPressTimestamp=timestamp;
4474 if (elapsed < KEY_DELAY) {
4475 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4476 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4478 if (infoPtr->charCode != charCode) {
4479 infoPtr->charCode=charCode=0;
4482 infoPtr->charCode=charCode;
4483 infoPtr->szSearchParam[0]=charCode;
4484 infoPtr->nSearchParamLength=1;
4485 /* Redundant with the 1 char string */
4489 /* and search from the current position */
4491 if (infoPtr->selectedItem != NULL) {
4492 endidx=infoPtr->selectedItem;
4493 /* if looking for single character match,
4494 * then we must always move forward
4496 if (infoPtr->nSearchParamLength == 1)
4497 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4502 idx=infoPtr->root->firstChild;
4505 /* At the end point, sort out wrapping */
4508 /* If endidx is null, stop at the last item (ie top to bottom) */
4512 /* Otherwise, start again at the very beginning */
4513 idx=infoPtr->root->firstChild;
4515 /* But if we are stopping on the first child, end now! */
4516 if (idx == endidx) break;
4520 ZeroMemory(&item, sizeof(item));
4521 item.mask = TVIF_TEXT;
4523 item.pszText = buffer;
4524 item.cchTextMax = sizeof(buffer);
4525 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4527 /* check for a match */
4528 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4531 } else if ( (charCode != 0) && (nItem == NULL) &&
4532 (nItem != infoPtr->selectedItem) &&
4533 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4534 /* This would work but we must keep looking for a longer match */
4537 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4538 } while (idx != endidx);
4540 if (nItem != NULL) {
4541 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4542 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4549 /* Scrolling ************************************************************/
4552 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4555 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4556 HTREEITEM newFirstVisible = NULL;
4557 int visible_pos = -1;
4559 if (!TREEVIEW_ValidItem(infoPtr, item))
4562 if (!ISVISIBLE(item))
4564 /* Expand parents as necessary. */
4567 /* see if we are trying to ensure that root is visible */
4568 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4569 parent = item->parent;
4571 parent = item; /* this item is the topmost item */
4573 while (parent != infoPtr->root)
4575 if (!(parent->state & TVIS_EXPANDED))
4576 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4578 parent = parent->parent;
4582 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4584 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4585 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4587 if (hasFirstVisible)
4588 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4590 if (visible_pos < 0)
4592 /* item is before the start of the list: put it at the top. */
4593 newFirstVisible = item;
4595 else if (visible_pos >= viscount
4596 /* Sometimes, before we are displayed, GVC is 0, causing us to
4597 * spuriously scroll up. */
4598 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4600 /* item is past the end of the list. */
4601 int scroll = visible_pos - viscount;
4603 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4609 /* Scroll window so item's text is visible as much as possible */
4610 /* Calculation of amount of extra space is taken from EditLabel code */
4612 TEXTMETRICW textMetric;
4613 HDC hdc = GetWindowDC(infoPtr->hwnd);
4615 x = item->textWidth;
4617 GetTextMetricsW(hdc, &textMetric);
4618 ReleaseDC(infoPtr->hwnd, hdc);
4620 x += (textMetric.tmMaxCharWidth * 2);
4621 x = max(x, textMetric.tmMaxCharWidth * 3);
4623 if (item->textOffset < 0)
4624 pos = item->textOffset;
4625 else if (item->textOffset + x > infoPtr->clientWidth)
4627 if (x > infoPtr->clientWidth)
4628 pos = item->textOffset;
4630 pos = item->textOffset + x - infoPtr->clientWidth;
4635 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4638 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4640 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4649 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4650 TREEVIEW_ITEM *newFirstVisible,
4651 BOOL bUpdateScrollPos)
4655 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4657 if (newFirstVisible != NULL)
4659 /* Prevent an empty gap from appearing at the bottom... */
4660 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4661 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4665 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4668 /* ... unless we just don't have enough items. */
4669 if (newFirstVisible == NULL)
4670 newFirstVisible = infoPtr->root->firstChild;
4674 if (infoPtr->firstVisible != newFirstVisible)
4676 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4678 infoPtr->firstVisible = newFirstVisible;
4679 TREEVIEW_Invalidate(infoPtr, NULL);
4683 TREEVIEW_ITEM *item;
4684 int scroll = infoPtr->uItemHeight *
4685 (infoPtr->firstVisible->visibleOrder
4686 - newFirstVisible->visibleOrder);
4688 infoPtr->firstVisible = newFirstVisible;
4690 for (item = infoPtr->root->firstChild; item != NULL;
4691 item = TREEVIEW_GetNextListItem(infoPtr, item))
4693 item->rect.top += scroll;
4694 item->rect.bottom += scroll;
4697 if (bUpdateScrollPos)
4698 SetScrollPos(infoPtr->hwnd, SB_VERT,
4699 newFirstVisible->visibleOrder, TRUE);
4701 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4706 /************************************************************************
4707 * VScroll is always in units of visible items. i.e. we always have a
4708 * visible item aligned to the top of the control. (Unless we have no
4712 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4714 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4715 TREEVIEW_ITEM *newFirstVisible = NULL;
4717 int nScrollCode = LOWORD(wParam);
4719 TRACE("wp %lx\n", wParam);
4721 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4724 if (!oldFirstVisible)
4726 assert(infoPtr->root->firstChild == NULL);
4730 switch (nScrollCode)
4733 newFirstVisible = infoPtr->root->firstChild;
4737 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4741 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4745 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4749 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4750 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4754 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4755 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4759 case SB_THUMBPOSITION:
4760 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4761 infoPtr->root->firstChild,
4762 (LONG)(SHORT)HIWORD(wParam));
4769 if (newFirstVisible != NULL)
4771 if (newFirstVisible != oldFirstVisible)
4772 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4773 nScrollCode != SB_THUMBTRACK);
4774 else if (nScrollCode == SB_THUMBPOSITION)
4775 SetScrollPos(infoPtr->hwnd, SB_VERT,
4776 newFirstVisible->visibleOrder, TRUE);
4783 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4786 int scrollX = infoPtr->scrollX;
4787 int nScrollCode = LOWORD(wParam);
4789 TRACE("wp %lx\n", wParam);
4791 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4794 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4795 /* shall never occur */
4802 switch (nScrollCode)
4805 scrollX -= infoPtr->uItemHeight;
4808 scrollX += infoPtr->uItemHeight;
4811 scrollX -= infoPtr->clientWidth;
4814 scrollX += infoPtr->clientWidth;
4818 case SB_THUMBPOSITION:
4819 scrollX = (int)(SHORT)HIWORD(wParam);
4826 if (scrollX > maxWidth)
4828 else if (scrollX < 0)
4832 if (scrollX != infoPtr->scrollX)
4834 TREEVIEW_ITEM *item;
4835 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4837 for (item = infoPtr->root->firstChild; item != NULL;
4838 item = TREEVIEW_GetNextListItem(infoPtr, item))
4840 item->linesOffset += scroll_pixels;
4841 item->stateOffset += scroll_pixels;
4842 item->imageOffset += scroll_pixels;
4843 item->textOffset += scroll_pixels;
4846 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4847 infoPtr->scrollX = scrollX;
4848 UpdateWindow(infoPtr->hwnd);
4851 if (nScrollCode != SB_THUMBTRACK)
4852 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4858 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4861 UINT pulScrollLines = 3;
4863 if (wParam & (MK_SHIFT | MK_CONTROL))
4864 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4866 if (infoPtr->firstVisible == NULL)
4869 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4871 gcWheelDelta = -(short)HIWORD(wParam);
4872 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4874 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4876 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4877 int maxDy = infoPtr->maxVisibleOrder;
4885 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4890 /* Create/Destroy *******************************************************/
4893 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4896 HBITMAP hbm, hbmOld;
4900 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4902 hdcScreen = GetDC(0);
4904 hdc = CreateCompatibleDC(hdcScreen);
4905 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4906 hbmOld = SelectObject(hdc, hbm);
4908 SetRect(&rc, 0, 0, 48, 16);
4909 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4911 SetRect(&rc, 18, 2, 30, 14);
4912 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4913 DFCS_BUTTONCHECK|DFCS_FLAT);
4915 SetRect(&rc, 34, 2, 46, 14);
4916 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4917 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4919 SelectObject(hdc, hbmOld);
4920 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4921 comctl32_color.clrWindow);
4922 TRACE("checkbox index %d\n", nIndex);
4926 ReleaseDC(0, hdcScreen);
4928 infoPtr->stateImageWidth = 16;
4929 infoPtr->stateImageHeight = 16;
4933 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4936 TREEVIEW_INFO *infoPtr;
4939 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4941 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4943 if (infoPtr == NULL)
4945 ERR("could not allocate info memory!\n");
4949 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4951 infoPtr->hwnd = hwnd;
4952 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4954 infoPtr->uNumItems = 0;
4955 infoPtr->cdmode = 0;
4956 infoPtr->uScrollTime = 300; /* milliseconds */
4957 infoPtr->bRedraw = TRUE;
4959 GetClientRect(hwnd, &rcClient);
4961 /* No scroll bars yet. */
4962 infoPtr->clientWidth = rcClient.right;
4963 infoPtr->clientHeight = rcClient.bottom;
4964 infoPtr->uInternalStatus = 0;
4966 infoPtr->treeWidth = 0;
4967 infoPtr->treeHeight = 0;
4969 infoPtr->uIndent = MINIMUM_INDENT;
4970 infoPtr->selectedItem = NULL;
4971 infoPtr->focusedItem = NULL;
4972 infoPtr->hotItem = NULL;
4973 infoPtr->editItem = NULL;
4974 infoPtr->firstVisible = NULL;
4975 infoPtr->maxVisibleOrder = 0;
4976 infoPtr->dropItem = NULL;
4977 infoPtr->insertMarkItem = NULL;
4978 infoPtr->insertBeforeorAfter = 0;
4981 infoPtr->scrollX = 0;
4983 infoPtr->clrBk = CLR_NONE; /* use system color */
4984 infoPtr->clrText = CLR_NONE; /* use system color */
4985 infoPtr->clrLine = CLR_DEFAULT;
4986 infoPtr->clrInsertMark = CLR_DEFAULT;
4990 infoPtr->hwndEdit = NULL;
4991 infoPtr->wpEditOrig = NULL;
4992 infoPtr->bIgnoreEditKillFocus = FALSE;
4993 infoPtr->bLabelChanged = FALSE;
4995 infoPtr->himlNormal = NULL;
4996 infoPtr->himlState = NULL;
4997 infoPtr->normalImageWidth = 0;
4998 infoPtr->normalImageHeight = 0;
4999 infoPtr->stateImageWidth = 0;
5000 infoPtr->stateImageHeight = 0;
5002 infoPtr->items = DPA_Create(16);
5004 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5005 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5006 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5007 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5008 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5010 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5012 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5013 infoPtr->root->state = TVIS_EXPANDED;
5014 infoPtr->root->iLevel = -1;
5015 infoPtr->root->visibleOrder = -1;
5017 infoPtr->hwndNotify = lpcs->hwndParent;
5018 infoPtr->hwndToolTip = 0;
5020 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5022 /* Determine what type of notify should be issued */
5023 /* sets infoPtr->bNtfUnicode */
5024 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5026 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5027 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5028 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5031 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5032 initialize_checkboxes(infoPtr);
5034 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5035 ShowScrollBar(hwnd, SB_VERT, FALSE);
5036 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5038 OpenThemeData (hwnd, themeClass);
5045 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5049 /* free item data */
5050 TREEVIEW_RemoveTree(infoPtr);
5051 /* root isn't freed with other items */
5052 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5053 DPA_Destroy(infoPtr->items);
5055 /* tool tip is automatically destroyed: we are its owner */
5057 /* Restore original wndproc */
5058 if (infoPtr->hwndEdit)
5059 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5060 (DWORD_PTR)infoPtr->wpEditOrig);
5062 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5064 /* Deassociate treeview from the window before doing anything drastic. */
5065 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5068 DeleteObject(infoPtr->hDefaultFont);
5069 DeleteObject(infoPtr->hBoldFont);
5070 DeleteObject(infoPtr->hUnderlineFont);
5076 /* Miscellaneous Messages ***********************************************/
5079 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5087 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5088 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5089 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5090 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5091 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5092 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5093 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5094 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5095 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5099 if (key >= VK_PRIOR && key <= VK_DOWN)
5101 unsigned char code = scroll[key - VK_PRIOR].code;
5103 (((code & (1 << 7)) == (SB_HORZ << 7))
5105 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5111 /************************************************************************
5114 * VK_UP Move selection to the previous non-hidden item.
5115 * VK_DOWN Move selection to the next non-hidden item.
5116 * VK_HOME Move selection to the first item.
5117 * VK_END Move selection to the last item.
5118 * VK_LEFT If expanded then collapse, otherwise move to parent.
5119 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5121 * VK_SUBTRACT Collapse.
5122 * VK_MULTIPLY Expand all.
5123 * VK_PRIOR Move up GetVisibleCount items.
5124 * VK_NEXT Move down GetVisibleCount items.
5125 * VK_BACK Move to parent.
5126 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5129 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5131 /* If it is non-NULL and different, it will be selected and visible. */
5132 TREEVIEW_ITEM *newSelection = NULL;
5134 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5136 TRACE("%lx\n", wParam);
5138 if (prevItem == NULL)
5141 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5142 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5147 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5149 newSelection = infoPtr->root->firstChild;
5153 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5157 newSelection = infoPtr->root->firstChild;
5161 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5165 if (prevItem->state & TVIS_EXPANDED)
5167 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5169 else if (prevItem->parent != infoPtr->root)
5171 newSelection = prevItem->parent;
5176 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5178 if (!(prevItem->state & TVIS_EXPANDED))
5179 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5182 newSelection = prevItem->firstChild;
5189 TREEVIEW_ExpandAll(infoPtr, prevItem);
5193 if (!(prevItem->state & TVIS_EXPANDED))
5194 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5198 if (prevItem->state & TVIS_EXPANDED)
5199 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5204 = TREEVIEW_GetListItem(infoPtr, prevItem,
5205 -TREEVIEW_GetVisibleCount(infoPtr));
5210 = TREEVIEW_GetListItem(infoPtr, prevItem,
5211 TREEVIEW_GetVisibleCount(infoPtr));
5215 newSelection = prevItem->parent;
5216 if (newSelection == infoPtr->root)
5217 newSelection = NULL;
5221 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5222 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5226 if (newSelection && newSelection != prevItem)
5228 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5231 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5239 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5241 /* remove hot effect from item */
5242 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5243 infoPtr->hotItem = NULL;
5249 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5252 TRACKMOUSEEVENT trackinfo;
5253 TREEVIEW_ITEM * item;
5255 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5257 /* fill in the TRACKMOUSEEVENT struct */
5258 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5259 trackinfo.dwFlags = TME_QUERY;
5260 trackinfo.hwndTrack = infoPtr->hwnd;
5262 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5263 _TrackMouseEvent(&trackinfo);
5265 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5266 if(!(trackinfo.dwFlags & TME_LEAVE))
5268 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5269 trackinfo.hwndTrack = infoPtr->hwnd;
5270 /* do it as fast as possible, minimal systimer latency will be used */
5271 trackinfo.dwHoverTime = 1;
5273 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5274 /* and can properly deactivate the hot item */
5275 _TrackMouseEvent(&trackinfo);
5278 pt.x = (short)LOWORD(lParam);
5279 pt.y = (short)HIWORD(lParam);
5281 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5283 if (item != infoPtr->hotItem)
5285 /* redraw old hot item */
5286 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5287 infoPtr->hotItem = item;
5288 /* redraw new hot item */
5289 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5295 /* Draw themed border */
5296 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5298 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5302 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5303 cyEdge = GetSystemMetrics (SM_CYEDGE);
5306 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5308 GetWindowRect(infoPtr->hwnd, &r);
5310 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5311 r.right - cxEdge, r.bottom - cyEdge);
5312 if (region != (HRGN)1)
5313 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5314 OffsetRect(&r, -r.left, -r.top);
5316 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5317 OffsetRect(&r, -r.left, -r.top);
5319 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5320 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5321 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5322 ReleaseDC(infoPtr->hwnd, dc);
5324 /* Call default proc to get the scrollbars etc. painted */
5325 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5331 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5333 LPNMHDR lpnmh = (LPNMHDR)lParam;
5335 if (lpnmh->code == PGN_CALCSIZE) {
5336 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5338 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5339 lppgc->iWidth = infoPtr->treeWidth;
5340 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5341 infoPtr->treeWidth, infoPtr->clientWidth);
5344 lppgc->iHeight = infoPtr->treeHeight;
5345 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5346 infoPtr->treeHeight, infoPtr->clientHeight);
5350 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5354 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5356 if (wParam == SIZE_RESTORED)
5358 infoPtr->clientWidth = (short)LOWORD(lParam);
5359 infoPtr->clientHeight = (short)HIWORD(lParam);
5361 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5362 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5363 TREEVIEW_UpdateScrollBars(infoPtr);
5367 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5370 TREEVIEW_Invalidate(infoPtr, NULL);
5375 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5377 TRACE("(%lx %lx)\n", wParam, lParam);
5379 if (wParam == GWL_STYLE)
5381 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5383 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5385 if (dwNewStyle & TVS_CHECKBOXES)
5387 initialize_checkboxes(infoPtr);
5388 TRACE("checkboxes enabled\n");
5392 FIXME("tried to disable checkboxes\n");
5396 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5398 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5400 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5401 TRACE("tooltips enabled\n");
5405 DestroyWindow(infoPtr->hwndToolTip);
5406 infoPtr->hwndToolTip = 0;
5407 TRACE("tooltips disabled\n");
5411 infoPtr->dwStyle = dwNewStyle;
5414 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5415 TREEVIEW_UpdateScrollBars(infoPtr);
5416 TREEVIEW_Invalidate(infoPtr, NULL);
5422 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5425 TREEVIEW_ITEM * item;
5429 ScreenToClient(infoPtr->hwnd, &pt);
5431 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5433 memset(&nmmouse, 0, sizeof(nmmouse));
5434 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5435 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5436 nmmouse.hdr.code = NM_SETCURSOR;
5439 nmmouse.dwItemSpec = (DWORD_PTR)item;
5440 nmmouse.dwItemData = item->lParam;
5444 nmmouse.dwHitInfo = lParam;
5445 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, (LPARAM)&nmmouse))
5448 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5450 SetCursor(infoPtr->hcurHand);
5454 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5458 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5462 if (!infoPtr->selectedItem)
5464 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5468 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5469 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5474 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5478 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5479 UpdateWindow(infoPtr->hwnd);
5480 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5484 /* update theme after a WM_THEMECHANGED message */
5485 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5487 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5488 CloseThemeData (theme);
5489 OpenThemeData (infoPtr->hwnd, themeClass);
5494 static LRESULT WINAPI
5495 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5497 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5499 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5501 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5504 if (uMsg == WM_CREATE)
5505 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5512 case TVM_CREATEDRAGIMAGE:
5513 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5515 case TVM_DELETEITEM:
5516 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5518 case TVM_EDITLABELA:
5519 case TVM_EDITLABELW:
5520 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5522 case TVM_ENDEDITLABELNOW:
5523 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5525 case TVM_ENSUREVISIBLE:
5526 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5529 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5531 case TVM_GETBKCOLOR:
5532 return TREEVIEW_GetBkColor(infoPtr);
5535 return TREEVIEW_GetCount(infoPtr);
5537 case TVM_GETEDITCONTROL:
5538 return TREEVIEW_GetEditControl(infoPtr);
5540 case TVM_GETIMAGELIST:
5541 return TREEVIEW_GetImageList(infoPtr, wParam);
5544 return TREEVIEW_GetIndent(infoPtr);
5546 case TVM_GETINSERTMARKCOLOR:
5547 return TREEVIEW_GetInsertMarkColor(infoPtr);
5549 case TVM_GETISEARCHSTRINGA:
5550 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5553 case TVM_GETISEARCHSTRINGW:
5554 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5559 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5560 uMsg == TVM_GETITEMW);
5561 case TVM_GETITEMHEIGHT:
5562 return TREEVIEW_GetItemHeight(infoPtr);
5564 case TVM_GETITEMRECT:
5565 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5567 case TVM_GETITEMSTATE:
5568 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5570 case TVM_GETLINECOLOR:
5571 return TREEVIEW_GetLineColor(infoPtr);
5573 case TVM_GETNEXTITEM:
5574 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5576 case TVM_GETSCROLLTIME:
5577 return TREEVIEW_GetScrollTime(infoPtr);
5579 case TVM_GETTEXTCOLOR:
5580 return TREEVIEW_GetTextColor(infoPtr);
5582 case TVM_GETTOOLTIPS:
5583 return TREEVIEW_GetToolTips(infoPtr);
5585 case TVM_GETUNICODEFORMAT:
5586 return TREEVIEW_GetUnicodeFormat(infoPtr);
5588 case TVM_GETVISIBLECOUNT:
5589 return TREEVIEW_GetVisibleCount(infoPtr);
5592 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5594 case TVM_INSERTITEMA:
5595 case TVM_INSERTITEMW:
5596 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5597 uMsg == TVM_INSERTITEMW);
5598 case TVM_SELECTITEM:
5599 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5601 case TVM_SETBKCOLOR:
5602 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5604 case TVM_SETIMAGELIST:
5605 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5608 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5610 case TVM_SETINSERTMARK:
5611 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5613 case TVM_SETINSERTMARKCOLOR:
5614 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5618 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5619 uMsg == TVM_SETITEMW);
5620 case TVM_SETLINECOLOR:
5621 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5623 case TVM_SETITEMHEIGHT:
5624 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5626 case TVM_SETSCROLLTIME:
5627 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5629 case TVM_SETTEXTCOLOR:
5630 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5632 case TVM_SETTOOLTIPS:
5633 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5635 case TVM_SETUNICODEFORMAT:
5636 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5638 case TVM_SORTCHILDREN:
5639 return TREEVIEW_SortChildren(infoPtr, lParam);
5641 case TVM_SORTCHILDRENCB:
5642 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5645 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5648 return TREEVIEW_Command(infoPtr, wParam, lParam);
5651 return TREEVIEW_Destroy(infoPtr);
5656 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5659 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5662 return TREEVIEW_GetFont(infoPtr);
5665 return TREEVIEW_HScroll(infoPtr, wParam);
5668 return TREEVIEW_KeyDown(infoPtr, wParam);
5671 return TREEVIEW_KillFocus(infoPtr);
5673 case WM_LBUTTONDBLCLK:
5674 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5676 case WM_LBUTTONDOWN:
5677 return TREEVIEW_LButtonDown(infoPtr, lParam);
5679 /* WM_MBUTTONDOWN */
5682 return TREEVIEW_MouseLeave(infoPtr);
5685 return TREEVIEW_MouseMove(infoPtr, lParam);
5687 case WM_NCLBUTTONDOWN:
5688 if (infoPtr->hwndEdit)
5689 SetFocus(infoPtr->hwnd);
5693 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5696 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5698 case WM_NOTIFYFORMAT:
5699 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5701 case WM_PRINTCLIENT:
5702 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5705 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5707 case WM_RBUTTONDOWN:
5708 return TREEVIEW_RButtonDown(infoPtr, lParam);
5711 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5714 return TREEVIEW_SetFocus(infoPtr);
5717 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5720 return TREEVIEW_SetRedraw(infoPtr, wParam);
5723 return TREEVIEW_Size(infoPtr, wParam, lParam);
5725 case WM_STYLECHANGED:
5726 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5728 case WM_SYSCOLORCHANGE:
5729 COMCTL32_RefreshSysColors();
5735 return TREEVIEW_HandleTimer(infoPtr, wParam);
5737 case WM_THEMECHANGED:
5738 return TREEVIEW_ThemeChanged (infoPtr);
5741 return TREEVIEW_VScroll(infoPtr, wParam);
5743 /* WM_WININICHANGE */
5746 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5749 TRACE("drawItem\n");
5753 /* This mostly catches MFC and Delphi messages. :( */
5754 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5755 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5757 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5762 /* Class Registration ***************************************************/
5765 TREEVIEW_Register(void)
5771 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5772 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5773 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5774 wndClass.cbClsExtra = 0;
5775 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5777 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5778 wndClass.hbrBackground = 0;
5779 wndClass.lpszClassName = WC_TREEVIEWW;
5781 RegisterClassW(&wndClass);
5786 TREEVIEW_Unregister(void)
5788 UnregisterClassW(WC_TREEVIEWW, NULL);
5792 /* Tree Verification ****************************************************/
5795 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5797 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5798 TREEVIEW_ITEM *item)
5800 assert(infoPtr != NULL);
5801 assert(item != NULL);
5803 /* both NULL, or both non-null */
5804 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5806 assert(item->firstChild != item);
5807 assert(item->lastChild != item);
5809 if (item->firstChild)
5811 assert(item->firstChild->parent == item);
5812 assert(item->firstChild->prevSibling == NULL);
5815 if (item->lastChild)
5817 assert(item->lastChild->parent == item);
5818 assert(item->lastChild->nextSibling == NULL);
5821 assert(item->nextSibling != item);
5822 if (item->nextSibling)
5824 assert(item->nextSibling->parent == item->parent);
5825 assert(item->nextSibling->prevSibling == item);
5828 assert(item->prevSibling != item);
5829 if (item->prevSibling)
5831 assert(item->prevSibling->parent == item->parent);
5832 assert(item->prevSibling->nextSibling == item);
5837 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5839 assert(item != NULL);
5841 assert(item->parent != NULL);
5842 assert(item->parent != item);
5843 assert(item->iLevel == item->parent->iLevel + 1);
5845 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5847 TREEVIEW_VerifyItemCommon(infoPtr, item);
5849 TREEVIEW_VerifyChildren(infoPtr, item);
5853 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5855 TREEVIEW_ITEM *child;
5856 assert(item != NULL);
5858 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5859 TREEVIEW_VerifyItem(infoPtr, child);
5863 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5865 TREEVIEW_ITEM *root = infoPtr->root;
5867 assert(root != NULL);
5868 assert(root->iLevel == -1);
5869 assert(root->parent == NULL);
5870 assert(root->prevSibling == NULL);
5872 TREEVIEW_VerifyItemCommon(infoPtr, root);
5874 TREEVIEW_VerifyChildren(infoPtr, root);
5878 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5880 if (!TRACE_ON(treeview)) return;
5882 assert(infoPtr != NULL);
5883 TREEVIEW_VerifyRoot(infoPtr);