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, TVIS_EX_FLAT,
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
44 #include "wine/port.h"
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
69 /* internal structures */
71 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
73 HTREEITEM parent; /* handle to parent or 0 if at root */
74 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
75 HTREEITEM firstChild; /* handle to first child or 0 if no child */
87 int iIntegral; /* item height multiplier (1 is normal) */
88 int iLevel; /* indentation level:0=root level */
90 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
96 LONG textWidth; /* horizontal text extent for pszText */
97 LONG visibleOrder; /* visible ordering, 0 is first visible item */
101 typedef struct tagTREEVIEW_INFO
104 HWND hwndNotify; /* Owner window to send notifications to */
107 UINT uInternalStatus;
109 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
110 INT cdmode; /* last custom draw setting */
111 UINT uScrollTime; /* max. time for scrolling in milliseconds */
112 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
114 UINT uItemHeight; /* item height */
117 LONG clientWidth; /* width of control window */
118 LONG clientHeight; /* height of control window */
120 LONG treeWidth; /* width of visible tree items */
121 LONG treeHeight; /* height of visible tree items */
123 UINT uIndent; /* indentation in pixels */
124 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
125 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
126 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
127 HTREEITEM editItem; /* item being edited with builtin edit box */
129 HTREEITEM firstVisible; /* handle to first visible item */
130 LONG maxVisibleOrder;
131 HTREEITEM dropItem; /* handle to item selected by drag cursor */
132 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
133 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
134 HIMAGELIST dragList; /* Bitmap of dragged item */
139 COLORREF clrInsertMark;
143 HFONT hUnderlineFont;
148 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
149 BOOL bIgnoreEditKillFocus;
152 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
153 HIMAGELIST himlNormal;
154 int normalImageHeight;
155 int normalImageWidth;
156 HIMAGELIST himlState;
157 int stateImageHeight;
161 DWORD lastKeyPressTimestamp;
163 INT nSearchParamLength;
164 WCHAR szSearchParam[ MAX_PATH ];
168 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
169 #define KEY_DELAY 450
171 /* bitflags for infoPtr->uInternalStatus */
173 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
174 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
175 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
176 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
177 #define TV_RDRAG 0x10 /* ditto Rbutton */
178 #define TV_RDRAGGING 0x20
180 /* bitflags for infoPtr->timer */
182 #define TV_EDIT_TIMER 2
183 #define TV_EDIT_TIMER_SET 2
185 #define TEXT_CALLBACK_SIZE 260
187 #define TREEVIEW_LEFT_MARGIN 8
189 #define MINIMUM_INDENT 19
191 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
193 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
194 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
195 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
197 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
198 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
199 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
200 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
202 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
205 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
208 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
210 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
211 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
212 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
213 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
214 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
215 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
216 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
218 /* Random Utilities *****************************************************/
219 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
221 /* Returns the treeview private data if hwnd is a treeview.
222 * Otherwise returns an undefined value. */
223 static inline TREEVIEW_INFO *
224 TREEVIEW_GetInfoPtr(HWND hwnd)
226 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
229 /* Don't call this. Nothing wants an item index. */
231 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
233 return DPA_GetPtrIndex(infoPtr->items, handle);
236 /* Checks if item has changed and needs to be redrawn */
237 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
238 const TVITEMEXW *tvChange)
240 /* Number of children has changed */
241 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
244 /* Image has changed and it's not a callback */
245 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
246 tiNew->iImage != I_IMAGECALLBACK)
249 /* Selected image has changed and it's not a callback */
250 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
251 tiNew->iSelectedImage != I_IMAGECALLBACK)
254 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
255 tiNew->iExpandedImage != I_IMAGECALLBACK)
258 /* Text has changed and it's not a callback */
259 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
260 tiNew->pszText != LPSTR_TEXTCALLBACKW)
263 /* Indent has changed */
264 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
267 /* Item state has changed */
268 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
274 /***************************************************************************
275 * This method checks that handle is an item for this tree.
278 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
280 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
282 TRACE("invalid item %p\n", handle);
290 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
294 GetObjectW(hOrigFont, sizeof(font), &font);
295 font.lfWeight = FW_BOLD;
296 return CreateFontIndirectW(&font);
300 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
304 GetObjectW(hOrigFont, sizeof(font), &font);
305 font.lfUnderline = TRUE;
306 return CreateFontIndirectW(&font);
310 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
312 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
313 return infoPtr->hUnderlineFont;
314 if (item->state & TVIS_BOLD)
315 return infoPtr->hBoldFont;
316 return infoPtr->hFont;
319 /* for trace/debugging purposes only */
321 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
323 if (item == NULL) return "<null item>";
324 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
325 if (item->pszText == NULL) return "<null>";
326 return debugstr_w(item->pszText);
329 /* An item is not a child of itself. */
331 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
335 child = child->parent;
336 if (child == parent) return TRUE;
337 } while (child != NULL);
343 /* Tree Traversal *******************************************************/
345 /***************************************************************************
346 * This method returns the last expanded sibling or child child item
349 static TREEVIEW_ITEM *
350 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
352 if (!item) return NULL;
354 while (item->lastChild)
356 if (item->state & TVIS_EXPANDED)
357 item = item->lastChild;
362 if (item == infoPtr->root)
368 /***************************************************************************
369 * This method returns the previous non-hidden item in the list not
370 * considering the tree hierarchy.
372 static TREEVIEW_ITEM *
373 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
375 if (tvItem->prevSibling)
377 /* This item has a prevSibling, get the last item in the sibling's tree. */
378 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
380 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
381 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
387 /* this item does not have a prevSibling, get the parent */
388 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
393 /***************************************************************************
394 * This method returns the next physical item in the treeview not
395 * considering the tree hierarchy.
397 static TREEVIEW_ITEM *
398 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
401 * If this item has children and is expanded, return the first child
403 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
405 return tvItem->firstChild;
410 * try to get the sibling
412 if (tvItem->nextSibling)
413 return tvItem->nextSibling;
416 * Otherwise, get the parent's sibling.
418 while (tvItem->parent)
420 tvItem = tvItem->parent;
422 if (tvItem->nextSibling)
423 return tvItem->nextSibling;
429 /***************************************************************************
430 * This method returns the nth item starting at the given item. It returns
431 * the last item (or first) we we run out of items.
433 * Will scroll backward if count is <0.
434 * forward if count is >0.
436 static TREEVIEW_ITEM *
437 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
440 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
441 TREEVIEW_ITEM *previousItem;
443 assert(item != NULL);
447 next_item = TREEVIEW_GetNextListItem;
452 next_item = TREEVIEW_GetPrevListItem;
460 item = next_item(infoPtr, item);
462 } while (--count && item != NULL);
465 return item ? item : previousItem;
468 /* Notifications ************************************************************/
470 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
472 if (!infoPtr->bNtfUnicode) {
474 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
475 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
476 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
477 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
478 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
479 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
480 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
481 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
482 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
483 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
484 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
485 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
492 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPNMHDR pnmh)
494 TRACE("wParam=%ld, lParam=%p\n", wParam, pnmh);
495 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, (LPARAM)pnmh);
499 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
502 HWND hwnd = infoPtr->hwnd;
505 nmhdr.hwndFrom = hwnd;
506 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
507 nmhdr.code = get_notifycode(infoPtr, code);
509 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, &nmhdr);
513 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
516 tvItem->hItem = item;
517 tvItem->state = item->state;
518 tvItem->stateMask = 0;
519 tvItem->iImage = item->iImage;
520 tvItem->iSelectedImage = item->iSelectedImage;
521 tvItem->cChildren = item->cChildren;
522 tvItem->lParam = item->lParam;
526 if (!infoPtr->bNtfUnicode)
528 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
529 tvItem->pszText = Alloc (tvItem->cchTextMax);
530 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
534 tvItem->cchTextMax = item->cchTextMax;
535 tvItem->pszText = item->pszText;
540 tvItem->cchTextMax = 0;
541 tvItem->pszText = NULL;
546 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
547 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
549 HWND hwnd = infoPtr->hwnd;
553 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
554 code, action, oldItem, newItem);
556 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
558 nmhdr.hdr.hwndFrom = hwnd;
559 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
560 nmhdr.hdr.code = get_notifycode(infoPtr, code);
561 nmhdr.action = action;
564 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
567 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
572 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
573 if (!infoPtr->bNtfUnicode)
575 Free(nmhdr.itemOld.pszText);
576 Free(nmhdr.itemNew.pszText);
582 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
583 HTREEITEM dragItem, POINT pt)
585 HWND hwnd = infoPtr->hwnd;
588 TRACE("code:%d dragitem:%p\n", code, dragItem);
590 nmhdr.hdr.hwndFrom = hwnd;
591 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
592 nmhdr.hdr.code = get_notifycode(infoPtr, code);
594 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
595 nmhdr.itemNew.hItem = dragItem;
596 nmhdr.itemNew.state = dragItem->state;
597 nmhdr.itemNew.lParam = dragItem->lParam;
599 nmhdr.ptDrag.x = pt.x;
600 nmhdr.ptDrag.y = pt.y;
602 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
607 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
610 HWND hwnd = infoPtr->hwnd;
611 NMTVCUSTOMDRAW nmcdhdr;
614 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
616 nmcd = &nmcdhdr.nmcd;
617 nmcd->hdr.hwndFrom = hwnd;
618 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
619 nmcd->hdr.code = NM_CUSTOMDRAW;
620 nmcd->dwDrawStage = dwDrawStage;
623 nmcd->dwItemSpec = 0;
624 nmcd->uItemState = 0;
625 nmcd->lItemlParam = 0;
626 nmcdhdr.clrText = infoPtr->clrText;
627 nmcdhdr.clrTextBk = infoPtr->clrBk;
630 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr.nmcd.hdr);
635 /* FIXME: need to find out when the flags in uItemState need to be set */
638 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
639 TREEVIEW_ITEM *item, UINT uItemDrawState,
640 NMTVCUSTOMDRAW *nmcdhdr)
642 HWND hwnd = infoPtr->hwnd;
645 DWORD_PTR dwItemSpec;
648 dwDrawStage = CDDS_ITEM | uItemDrawState;
649 dwItemSpec = (DWORD_PTR)item;
651 if (item->state & TVIS_SELECTED)
652 uItemState |= CDIS_SELECTED;
653 if (item == infoPtr->selectedItem)
654 uItemState |= CDIS_FOCUS;
655 if (item == infoPtr->hotItem)
656 uItemState |= CDIS_HOT;
658 nmcd = &nmcdhdr->nmcd;
659 nmcd->hdr.hwndFrom = hwnd;
660 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
661 nmcd->hdr.code = NM_CUSTOMDRAW;
662 nmcd->dwDrawStage = dwDrawStage;
664 nmcd->rc = item->rect;
665 nmcd->dwItemSpec = dwItemSpec;
666 nmcd->uItemState = uItemState;
667 nmcd->lItemlParam = item->lParam;
668 nmcdhdr->iLevel = item->iLevel;
670 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
671 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
672 nmcd->uItemState, nmcd->lItemlParam);
674 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr->nmcd.hdr);
678 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
680 HWND hwnd = infoPtr->hwnd;
684 tvdi.hdr.hwndFrom = hwnd;
685 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
686 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
688 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
689 &tvdi.item, editItem);
691 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
693 if (!infoPtr->bNtfUnicode)
694 Free(tvdi.item.pszText);
700 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
703 NMTVDISPINFOEXW callback;
704 HWND hwnd = infoPtr->hwnd;
706 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
707 mask &= item->callbackMask;
709 if (mask == 0) return;
711 callback.hdr.hwndFrom = hwnd;
712 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
713 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
715 /* 'state' always contains valid value, as well as 'lParam'.
716 * All other parameters are uninitialized.
718 callback.item.pszText = item->pszText;
719 callback.item.cchTextMax = item->cchTextMax;
720 callback.item.mask = mask;
721 callback.item.hItem = item;
722 callback.item.state = item->state;
723 callback.item.lParam = item->lParam;
725 /* If text is changed we need to recalculate textWidth */
726 if (mask & TVIF_TEXT)
729 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, &callback.hdr);
730 TRACE("resulting code 0x%08x\n", callback.hdr.code);
732 /* It may have changed due to a call to SetItem. */
733 mask &= item->callbackMask;
735 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
737 /* Instead of copying text into our buffer user specified his own */
738 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
741 int len = MultiByteToWideChar( CP_ACP, 0,
742 (LPSTR)callback.item.pszText, -1,
744 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
745 newText = ReAlloc(item->pszText, buflen);
747 TRACE("returned str %s, len=%d, buflen=%d\n",
748 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
752 item->pszText = newText;
753 MultiByteToWideChar( CP_ACP, 0,
754 (LPSTR)callback.item.pszText, -1,
755 item->pszText, buflen/sizeof(WCHAR));
756 item->cchTextMax = buflen/sizeof(WCHAR);
758 /* If ReAlloc fails we have nothing to do, but keep original text */
761 int len = max(lstrlenW(callback.item.pszText) + 1,
763 LPWSTR newText = ReAlloc(item->pszText, len);
765 TRACE("returned wstr %s, len=%d\n",
766 debugstr_w(callback.item.pszText), len);
770 item->pszText = newText;
771 strcpyW(item->pszText, callback.item.pszText);
772 item->cchTextMax = len;
774 /* If ReAlloc fails we have nothing to do, but keep original text */
777 else if (mask & TVIF_TEXT) {
778 /* User put text into our buffer, that is ok unless A string */
779 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
782 int len = MultiByteToWideChar( CP_ACP, 0,
783 (LPSTR)callback.item.pszText, -1,
785 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
786 newText = Alloc(buflen);
788 TRACE("same buffer str %s, len=%d, buflen=%d\n",
789 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
793 LPWSTR oldText = item->pszText;
794 item->pszText = newText;
795 MultiByteToWideChar( CP_ACP, 0,
796 (LPSTR)callback.item.pszText, -1,
797 item->pszText, buflen/sizeof(WCHAR));
798 item->cchTextMax = buflen/sizeof(WCHAR);
804 if (mask & TVIF_IMAGE)
805 item->iImage = callback.item.iImage;
807 if (mask & TVIF_SELECTEDIMAGE)
808 item->iSelectedImage = callback.item.iSelectedImage;
810 if (mask & TVIF_EXPANDEDIMAGE)
811 item->iExpandedImage = callback.item.iExpandedImage;
813 if (mask & TVIF_CHILDREN)
814 item->cChildren = callback.item.cChildren;
816 if (callback.item.mask & TVIF_STATE)
818 item->state &= ~callback.item.stateMask;
819 item->state |= (callback.item.state & callback.item.stateMask);
822 /* These members are now permanently set. */
823 if (callback.item.mask & TVIF_DI_SETITEM)
824 item->callbackMask &= ~callback.item.mask;
827 /***************************************************************************
828 * This function uses cChildren field to decide whether the item has
830 * Note: if this returns TRUE, the child items may not actually exist,
831 * they could be virtual.
833 * Just use item->firstChild to check for physical children.
836 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
838 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
840 return item->cChildren > 0;
843 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
847 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
849 if (nCommand != NF_REQUERY) return 0;
851 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
852 TRACE("format=%d\n", format);
854 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
856 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
861 /* Item Position ********************************************************/
863 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
865 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
867 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
868 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
871 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
873 item->stateOffset = item->linesOffset + infoPtr->uIndent;
874 item->imageOffset = item->stateOffset
875 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
876 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
880 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
886 /* DRAW's OM docker creates items like this */
887 if (item->pszText == NULL)
899 hdc = GetDC(infoPtr->hwnd);
900 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
903 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
904 item->textWidth = sz.cx;
908 SelectObject(hdc, hOldFont);
914 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
916 item->rect.top = infoPtr->uItemHeight *
917 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
919 item->rect.bottom = item->rect.top
920 + infoPtr->uItemHeight * item->iIntegral - 1;
923 item->rect.right = infoPtr->clientWidth;
926 /* We know that only items after start need their order updated. */
928 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
935 start = infoPtr->root->firstChild;
939 order = start->visibleOrder;
941 for (item = start; item != NULL;
942 item = TREEVIEW_GetNextListItem(infoPtr, item))
944 if (!ISVISIBLE(item) && order > 0)
945 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
946 item->visibleOrder = order;
947 order += item->iIntegral;
950 infoPtr->maxVisibleOrder = order;
952 for (item = start; item != NULL;
953 item = TREEVIEW_GetNextListItem(infoPtr, item))
955 TREEVIEW_ComputeItemRect(infoPtr, item);
960 /* Update metrics of all items in selected subtree.
961 * root must be expanded
964 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
966 TREEVIEW_ITEM *sibling;
970 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
973 root->state &= ~TVIS_EXPANDED;
974 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
975 root->state |= TVIS_EXPANDED;
977 hdc = GetDC(infoPtr->hwnd);
978 hOldFont = SelectObject(hdc, infoPtr->hFont);
980 for (; root != sibling;
981 root = TREEVIEW_GetNextListItem(infoPtr, root))
983 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
985 if (root->callbackMask & TVIF_TEXT)
986 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
988 if (root->textWidth == 0)
990 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
991 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
995 SelectObject(hdc, hOldFont);
996 ReleaseDC(infoPtr->hwnd, hdc);
999 /* Item Allocation **********************************************************/
1001 static TREEVIEW_ITEM *
1002 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1004 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1009 /* I_IMAGENONE would make more sense but this is neither what is
1010 * documented (MSDN doesn't specify) nor what Windows actually does
1011 * (it sets it to zero)... and I can so imagine an application using
1012 * inc/dec to toggle the images. */
1013 newItem->iImage = 0;
1014 newItem->iSelectedImage = 0;
1015 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1017 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1026 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1027 * free item->pszText. */
1029 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1031 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1032 if (infoPtr->selectedItem == item)
1033 infoPtr->selectedItem = NULL;
1034 if (infoPtr->hotItem == item)
1035 infoPtr->hotItem = NULL;
1036 if (infoPtr->focusedItem == item)
1037 infoPtr->focusedItem = NULL;
1038 if (infoPtr->firstVisible == item)
1039 infoPtr->firstVisible = NULL;
1040 if (infoPtr->dropItem == item)
1041 infoPtr->dropItem = NULL;
1042 if (infoPtr->insertMarkItem == item)
1043 infoPtr->insertMarkItem = NULL;
1048 /* Item Insertion *******************************************************/
1050 /***************************************************************************
1051 * This method inserts newItem before sibling as a child of parent.
1052 * sibling can be NULL, but only if parent has no children.
1055 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1056 TREEVIEW_ITEM *parent)
1058 assert(parent != NULL);
1060 if (sibling != NULL)
1062 assert(sibling->parent == parent);
1064 if (sibling->prevSibling != NULL)
1065 sibling->prevSibling->nextSibling = newItem;
1067 newItem->prevSibling = sibling->prevSibling;
1068 sibling->prevSibling = newItem;
1071 newItem->prevSibling = NULL;
1073 newItem->nextSibling = sibling;
1075 if (parent->firstChild == sibling)
1076 parent->firstChild = newItem;
1078 if (parent->lastChild == NULL)
1079 parent->lastChild = newItem;
1082 /***************************************************************************
1083 * This method inserts newItem after sibling as a child of parent.
1084 * sibling can be NULL, but only if parent has no children.
1087 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1088 TREEVIEW_ITEM *parent)
1090 assert(parent != NULL);
1092 if (sibling != NULL)
1094 assert(sibling->parent == parent);
1096 if (sibling->nextSibling != NULL)
1097 sibling->nextSibling->prevSibling = newItem;
1099 newItem->nextSibling = sibling->nextSibling;
1100 sibling->nextSibling = newItem;
1103 newItem->nextSibling = NULL;
1105 newItem->prevSibling = sibling;
1107 if (parent->lastChild == sibling)
1108 parent->lastChild = newItem;
1110 if (parent->firstChild == NULL)
1111 parent->firstChild = newItem;
1115 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1116 const TVITEMEXW *tvItem, BOOL isW)
1118 UINT callbackClear = 0;
1119 UINT callbackSet = 0;
1121 TRACE("item %p\n", item);
1122 /* Do this first in case it fails. */
1123 if (tvItem->mask & TVIF_TEXT)
1125 item->textWidth = 0; /* force width recalculation */
1126 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1131 len = lstrlenW(tvItem->pszText) + 1;
1133 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1135 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1137 if (newText == NULL) return FALSE;
1139 callbackClear |= TVIF_TEXT;
1141 item->pszText = newText;
1142 item->cchTextMax = len;
1144 lstrcpynW(item->pszText, tvItem->pszText, len);
1146 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1147 item->pszText, len);
1149 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1153 callbackSet |= TVIF_TEXT;
1155 item->pszText = ReAlloc(item->pszText,
1156 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1157 item->cchTextMax = TEXT_CALLBACK_SIZE;
1158 TRACE("setting callback, item %p\n", item);
1162 if (tvItem->mask & TVIF_CHILDREN)
1164 item->cChildren = tvItem->cChildren;
1166 if (item->cChildren == I_CHILDRENCALLBACK)
1167 callbackSet |= TVIF_CHILDREN;
1169 callbackClear |= TVIF_CHILDREN;
1172 if (tvItem->mask & TVIF_IMAGE)
1174 item->iImage = tvItem->iImage;
1176 if (item->iImage == I_IMAGECALLBACK)
1177 callbackSet |= TVIF_IMAGE;
1179 callbackClear |= TVIF_IMAGE;
1182 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1184 item->iSelectedImage = tvItem->iSelectedImage;
1186 if (item->iSelectedImage == I_IMAGECALLBACK)
1187 callbackSet |= TVIF_SELECTEDIMAGE;
1189 callbackClear |= TVIF_SELECTEDIMAGE;
1192 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1194 item->iExpandedImage = tvItem->iExpandedImage;
1196 if (item->iExpandedImage == I_IMAGECALLBACK)
1197 callbackSet |= TVIF_EXPANDEDIMAGE;
1199 callbackClear |= TVIF_EXPANDEDIMAGE;
1202 if (tvItem->mask & TVIF_PARAM)
1203 item->lParam = tvItem->lParam;
1205 /* If the application sets TVIF_INTEGRAL without
1206 * supplying a TVITEMEX structure, it's toast. */
1207 if (tvItem->mask & TVIF_INTEGRAL)
1208 item->iIntegral = tvItem->iIntegral;
1210 if (tvItem->mask & TVIF_STATE)
1212 TRACE("prevstate,state,mask:%x,%x,%x\n", item->state, tvItem->state,
1214 item->state &= ~tvItem->stateMask;
1215 item->state |= (tvItem->state & tvItem->stateMask);
1218 if (tvItem->mask & TVIF_STATEEX)
1220 FIXME("New extended state: %x\n", tvItem->uStateEx);
1223 item->callbackMask |= callbackSet;
1224 item->callbackMask &= ~callbackClear;
1229 /* Note that the new item is pre-zeroed. */
1231 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1233 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1234 HTREEITEM insertAfter;
1235 TREEVIEW_ITEM *newItem, *parentItem;
1236 BOOL bTextUpdated = FALSE;
1238 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1240 parentItem = infoPtr->root;
1244 parentItem = ptdi->hParent;
1246 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1248 WARN("invalid parent %p\n", parentItem);
1253 insertAfter = ptdi->hInsertAfter;
1255 /* Validate this now for convenience. */
1256 switch ((DWORD_PTR)insertAfter)
1258 case (DWORD_PTR)TVI_FIRST:
1259 case (DWORD_PTR)TVI_LAST:
1260 case (DWORD_PTR)TVI_SORT:
1264 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1265 insertAfter->parent != parentItem)
1267 WARN("invalid insert after %p\n", insertAfter);
1268 insertAfter = TVI_LAST;
1272 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1273 (tvItem->mask & TVIF_TEXT)
1274 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1275 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1278 newItem = TREEVIEW_AllocateItem(infoPtr);
1279 if (newItem == NULL)
1282 newItem->parent = parentItem;
1283 newItem->iIntegral = 1;
1284 newItem->visibleOrder = -1;
1286 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1289 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1291 infoPtr->uNumItems++;
1293 switch ((DWORD_PTR)insertAfter)
1295 case (DWORD_PTR)TVI_FIRST:
1297 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1298 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1299 if (infoPtr->firstVisible == originalFirst)
1300 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1304 case (DWORD_PTR)TVI_LAST:
1305 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1308 /* hInsertAfter names a specific item we want to insert after */
1310 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1313 case (DWORD_PTR)TVI_SORT:
1315 TREEVIEW_ITEM *aChild;
1316 TREEVIEW_ITEM *previousChild = NULL;
1317 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1318 BOOL bItemInserted = FALSE;
1320 aChild = parentItem->firstChild;
1322 bTextUpdated = TRUE;
1323 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1325 /* Iterate the parent children to see where we fit in */
1326 while (aChild != NULL)
1330 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1331 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1333 if (comp < 0) /* we are smaller than the current one */
1335 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1336 if (infoPtr->firstVisible == originalFirst &&
1337 aChild == originalFirst)
1338 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1339 bItemInserted = TRUE;
1342 else if (comp > 0) /* we are bigger than the current one */
1344 previousChild = aChild;
1346 /* This will help us to exit if there is no more sibling */
1347 aChild = (aChild->nextSibling == 0)
1349 : aChild->nextSibling;
1351 /* Look at the next item */
1357 * An item with this name is already existing, therefore,
1358 * we add after the one we found
1360 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1361 bItemInserted = TRUE;
1367 * we reach the end of the child list and the item has not
1368 * yet been inserted, therefore, insert it after the last child.
1370 if ((!bItemInserted) && (aChild == NULL))
1371 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1378 TRACE("new item %p; parent %p, mask %x\n", newItem,
1379 newItem->parent, tvItem->mask);
1381 newItem->iLevel = newItem->parent->iLevel + 1;
1383 if (newItem->parent->cChildren == 0)
1384 newItem->parent->cChildren = 1;
1386 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1388 if (STATEIMAGEINDEX(newItem->state) == 0)
1389 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1392 if (infoPtr->firstVisible == NULL)
1393 infoPtr->firstVisible = newItem;
1395 TREEVIEW_VerifyTree(infoPtr);
1397 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1399 if (parentItem == infoPtr->root ||
1400 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1402 TREEVIEW_ITEM *item;
1403 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1405 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1406 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1409 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1411 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1412 TREEVIEW_UpdateScrollBars(infoPtr);
1414 * if the item was inserted in a visible part of the tree,
1415 * invalidate it, as well as those after it
1417 for (item = newItem;
1419 item = TREEVIEW_GetNextListItem(infoPtr, item))
1420 TREEVIEW_Invalidate(infoPtr, item);
1424 /* refresh treeview if newItem is the first item inserted under parentItem */
1425 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1427 /* parent got '+' - update it */
1428 TREEVIEW_Invalidate(infoPtr, parentItem);
1432 return (LRESULT)newItem;
1435 /* Item Deletion ************************************************************/
1437 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1440 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1442 TREEVIEW_ITEM *kill = parentItem->firstChild;
1444 while (kill != NULL)
1446 TREEVIEW_ITEM *next = kill->nextSibling;
1448 TREEVIEW_RemoveItem(infoPtr, kill);
1453 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1454 assert(parentItem->firstChild == NULL);
1455 assert(parentItem->lastChild == NULL);
1459 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1461 TREEVIEW_ITEM *parentItem = item->parent;
1463 assert(item != NULL);
1464 assert(item->parent != NULL); /* i.e. it must not be the root */
1466 if (parentItem->firstChild == item)
1467 parentItem->firstChild = item->nextSibling;
1469 if (parentItem->lastChild == item)
1470 parentItem->lastChild = item->prevSibling;
1472 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1473 && parentItem->cChildren > 0)
1474 parentItem->cChildren = 0;
1476 if (item->prevSibling)
1477 item->prevSibling->nextSibling = item->nextSibling;
1479 if (item->nextSibling)
1480 item->nextSibling->prevSibling = item->prevSibling;
1484 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1486 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1488 if (item->firstChild)
1489 TREEVIEW_RemoveAllChildren(infoPtr, item);
1491 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1492 TVIF_HANDLE | TVIF_PARAM, item, 0);
1494 TREEVIEW_UnlinkItem(item);
1496 infoPtr->uNumItems--;
1498 if (item->pszText != LPSTR_TEXTCALLBACKW)
1499 Free(item->pszText);
1501 TREEVIEW_FreeItem(infoPtr, item);
1505 /* Empty out the tree. */
1507 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1509 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1511 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1515 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1517 TREEVIEW_ITEM *newSelection = NULL;
1518 TREEVIEW_ITEM *newFirstVisible = NULL;
1519 TREEVIEW_ITEM *parent, *prev = NULL;
1520 BOOL visible = FALSE;
1522 if (item == TVI_ROOT || !item)
1524 TRACE("TVI_ROOT\n");
1525 parent = infoPtr->root;
1526 newSelection = NULL;
1528 TREEVIEW_RemoveTree(infoPtr);
1532 if (!TREEVIEW_ValidItem(infoPtr, item))
1535 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1536 parent = item->parent;
1538 if (ISVISIBLE(item))
1540 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1544 if (infoPtr->selectedItem != NULL
1545 && (item == infoPtr->selectedItem
1546 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1548 if (item->nextSibling)
1549 newSelection = item->nextSibling;
1550 else if (item->parent != infoPtr->root)
1551 newSelection = item->parent;
1553 newSelection = item->prevSibling;
1554 TRACE("newSelection = %p\n", newSelection);
1557 if (infoPtr->firstVisible == item)
1559 if (item->nextSibling)
1560 newFirstVisible = item->nextSibling;
1561 else if (item->prevSibling)
1562 newFirstVisible = item->prevSibling;
1563 else if (item->parent != infoPtr->root)
1564 newFirstVisible = item->parent;
1565 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1568 newFirstVisible = infoPtr->firstVisible;
1570 TREEVIEW_RemoveItem(infoPtr, item);
1573 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1574 if (!infoPtr->selectedItem && newSelection)
1576 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1577 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1580 /* Validate insertMark dropItem.
1581 * hotItem ??? - used for comparison only.
1583 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1584 infoPtr->insertMarkItem = 0;
1586 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1587 infoPtr->dropItem = 0;
1589 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1590 newFirstVisible = infoPtr->root->firstChild;
1592 TREEVIEW_VerifyTree(infoPtr);
1594 if (!infoPtr->bRedraw) return TRUE;
1598 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1599 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1600 TREEVIEW_UpdateScrollBars(infoPtr);
1601 TREEVIEW_Invalidate(infoPtr, NULL);
1603 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1605 /* parent lost '+/-' - update it */
1606 TREEVIEW_Invalidate(infoPtr, parent);
1613 /* Get/Set Messages *********************************************************/
1615 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1617 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1619 if (infoPtr->bRedraw)
1621 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1622 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1623 TREEVIEW_UpdateScrollBars(infoPtr);
1624 TREEVIEW_Invalidate(infoPtr, NULL);
1630 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1633 return infoPtr->uIndent;
1637 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1641 if (newIndent < MINIMUM_INDENT)
1642 newIndent = MINIMUM_INDENT;
1644 if (infoPtr->uIndent != newIndent)
1646 infoPtr->uIndent = newIndent;
1647 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1648 TREEVIEW_UpdateScrollBars(infoPtr);
1649 TREEVIEW_Invalidate(infoPtr, NULL);
1657 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1660 return (LRESULT)infoPtr->hwndToolTip;
1664 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1669 prevToolTip = infoPtr->hwndToolTip;
1670 infoPtr->hwndToolTip = hwndTT;
1672 return (LRESULT)prevToolTip;
1676 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1678 BOOL rc = infoPtr->bNtfUnicode;
1679 infoPtr->bNtfUnicode = fUnicode;
1684 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1686 return infoPtr->bNtfUnicode;
1690 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1692 return infoPtr->uScrollTime;
1696 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1698 UINT uOldScrollTime = infoPtr->uScrollTime;
1700 infoPtr->uScrollTime = min(uScrollTime, 100);
1702 return uOldScrollTime;
1707 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1714 return (LRESULT)infoPtr->himlNormal;
1717 return (LRESULT)infoPtr->himlState;
1724 #define TVHEIGHT_MIN 16
1725 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1727 /* Compute the natural height for items. */
1729 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1733 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1736 /* Height is the maximum of:
1737 * 16 (a hack because our fonts are tiny), and
1738 * The text height + border & margin, and
1739 * The size of the normal image list
1741 GetTextMetricsW(hdc, &tm);
1742 SelectObject(hdc, hOldFont);
1745 height = TVHEIGHT_MIN;
1746 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1747 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1748 if (height < infoPtr->normalImageHeight)
1749 height = infoPtr->normalImageHeight;
1751 /* Round down, unless we support odd ("non even") heights. */
1752 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1759 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1761 HIMAGELIST himlOld = 0;
1762 int oldWidth = infoPtr->normalImageWidth;
1763 int oldHeight = infoPtr->normalImageHeight;
1766 TRACE("%lx,%p\n", wParam, himlNew);
1771 himlOld = infoPtr->himlNormal;
1772 infoPtr->himlNormal = himlNew;
1774 if (himlNew != NULL)
1775 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1776 &infoPtr->normalImageHeight);
1779 infoPtr->normalImageWidth = 0;
1780 infoPtr->normalImageHeight = 0;
1786 himlOld = infoPtr->himlState;
1787 infoPtr->himlState = himlNew;
1789 if (himlNew != NULL)
1790 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1791 &infoPtr->stateImageHeight);
1794 infoPtr->stateImageWidth = 0;
1795 infoPtr->stateImageHeight = 0;
1801 if (oldWidth != infoPtr->normalImageWidth ||
1802 oldHeight != infoPtr->normalImageHeight)
1804 BOOL bRecalcVisible = FALSE;
1806 if (oldHeight != infoPtr->normalImageHeight &&
1807 !infoPtr->bHeightSet)
1809 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1810 bRecalcVisible = TRUE;
1813 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1814 infoPtr->normalImageWidth != infoPtr->uIndent)
1816 infoPtr->uIndent = infoPtr->normalImageWidth;
1817 bRecalcVisible = TRUE;
1821 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1823 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1824 TREEVIEW_UpdateScrollBars(infoPtr);
1827 TREEVIEW_Invalidate(infoPtr, NULL);
1829 return (LRESULT)himlOld;
1833 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1835 INT prevHeight = infoPtr->uItemHeight;
1837 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1838 if (newHeight == -1)
1840 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1841 infoPtr->bHeightSet = FALSE;
1845 if (newHeight == 0) newHeight = 1;
1846 infoPtr->uItemHeight = newHeight;
1847 infoPtr->bHeightSet = TRUE;
1850 /* Round down, unless we support odd ("non even") heights. */
1851 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1853 infoPtr->uItemHeight &= ~1;
1854 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1857 if (infoPtr->uItemHeight != prevHeight)
1859 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1860 TREEVIEW_UpdateScrollBars(infoPtr);
1861 TREEVIEW_Invalidate(infoPtr, NULL);
1868 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1871 return infoPtr->uItemHeight;
1876 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1878 TRACE("%p\n", infoPtr->hFont);
1879 return (LRESULT)infoPtr->hFont;
1884 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1888 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1894 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1896 UINT uHeight = infoPtr->uItemHeight;
1898 TRACE("%p %i\n", hFont, bRedraw);
1900 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1902 DeleteObject(infoPtr->hBoldFont);
1903 DeleteObject(infoPtr->hUnderlineFont);
1904 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1905 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1907 if (!infoPtr->bHeightSet)
1908 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1910 if (uHeight != infoPtr->uItemHeight)
1911 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1913 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1915 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1916 TREEVIEW_UpdateScrollBars(infoPtr);
1919 TREEVIEW_Invalidate(infoPtr, NULL);
1926 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1929 return (LRESULT)infoPtr->clrLine;
1933 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1935 COLORREF prevColor = infoPtr->clrLine;
1938 infoPtr->clrLine = color;
1939 return (LRESULT)prevColor;
1944 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1947 return (LRESULT)infoPtr->clrText;
1951 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1953 COLORREF prevColor = infoPtr->clrText;
1956 infoPtr->clrText = color;
1958 if (infoPtr->clrText != prevColor)
1959 TREEVIEW_Invalidate(infoPtr, NULL);
1961 return (LRESULT)prevColor;
1966 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1969 return (LRESULT)infoPtr->clrBk;
1973 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1975 COLORREF prevColor = infoPtr->clrBk;
1978 infoPtr->clrBk = newColor;
1980 if (newColor != prevColor)
1981 TREEVIEW_Invalidate(infoPtr, NULL);
1983 return (LRESULT)prevColor;
1988 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1991 return (LRESULT)infoPtr->clrInsertMark;
1995 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1997 COLORREF prevColor = infoPtr->clrInsertMark;
1999 TRACE("%x\n", color);
2000 infoPtr->clrInsertMark = color;
2002 return (LRESULT)prevColor;
2007 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2009 TRACE("%d %p\n", wParam, item);
2011 if (!TREEVIEW_ValidItem(infoPtr, item))
2014 infoPtr->insertBeforeorAfter = wParam;
2015 infoPtr->insertMarkItem = item;
2017 TREEVIEW_Invalidate(infoPtr, NULL);
2023 /************************************************************************
2024 * Some serious braindamage here. lParam is a pointer to both the
2025 * input HTREEITEM and the output RECT.
2028 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2030 TREEVIEW_ITEM *item;
2031 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2039 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2043 * If wParam is TRUE return the text size otherwise return
2044 * the whole item size
2048 /* Windows does not send TVN_GETDISPINFO here. */
2050 lpRect->top = item->rect.top;
2051 lpRect->bottom = item->rect.bottom;
2053 lpRect->left = item->textOffset;
2054 if (!item->textWidth)
2055 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2057 lpRect->right = item->textOffset + item->textWidth + 4;
2061 *lpRect = item->rect;
2064 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2069 static inline LRESULT
2070 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2072 /* Surprise! This does not take integral height into account. */
2073 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2074 return infoPtr->clientHeight / infoPtr->uItemHeight;
2079 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2081 TREEVIEW_ITEM *item = tvItem->hItem;
2083 if (!TREEVIEW_ValidItem(infoPtr, item))
2086 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2088 if (tvItem->mask & TVIF_CHILDREN)
2090 if (item->cChildren==I_CHILDRENCALLBACK)
2091 FIXME("I_CHILDRENCALLBACK not supported\n");
2092 tvItem->cChildren = item->cChildren;
2095 if (tvItem->mask & TVIF_HANDLE)
2096 tvItem->hItem = item;
2098 if (tvItem->mask & TVIF_IMAGE)
2099 tvItem->iImage = item->iImage;
2101 if (tvItem->mask & TVIF_INTEGRAL)
2102 tvItem->iIntegral = item->iIntegral;
2104 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2105 tvItem->lParam = item->lParam;
2107 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2108 tvItem->iSelectedImage = item->iSelectedImage;
2110 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2111 tvItem->iExpandedImage = item->iExpandedImage;
2113 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2114 tvItem->state = item->state;
2116 if (tvItem->mask & TVIF_TEXT)
2118 if (item->pszText == NULL)
2120 if (tvItem->cchTextMax > 0)
2121 tvItem->pszText[0] = '\0';
2125 if (item->pszText == LPSTR_TEXTCALLBACKW)
2127 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2128 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2132 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2137 if (item->pszText == LPSTR_TEXTCALLBACKW)
2139 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2140 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2144 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2145 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2150 if (tvItem->mask & TVIF_STATEEX)
2152 FIXME("Extended item state not supported, returning 0.\n");
2153 tvItem->uStateEx = 0;
2156 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2157 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2162 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2163 * which is wrong. */
2165 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2167 TREEVIEW_ITEM *item;
2168 TREEVIEW_ITEM originalItem;
2170 item = tvItem->hItem;
2172 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2175 if (!TREEVIEW_ValidItem(infoPtr, item))
2178 /* store the original item values */
2179 originalItem = *item;
2181 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2184 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2185 if ((tvItem->mask & TVIF_TEXT
2186 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2189 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2190 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2193 if (tvItem->mask != 0 && ISVISIBLE(item))
2195 /* The refresh updates everything, but we can't wait until then. */
2196 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2198 /* if any of the item's values changed and it's not a callback, redraw the item */
2199 if (item_changed(&originalItem, item, tvItem))
2201 if (tvItem->mask & TVIF_INTEGRAL)
2203 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2204 TREEVIEW_UpdateScrollBars(infoPtr);
2206 TREEVIEW_Invalidate(infoPtr, NULL);
2210 TREEVIEW_UpdateScrollBars(infoPtr);
2211 TREEVIEW_Invalidate(infoPtr, item);
2220 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2224 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2227 return (item->state & mask);
2231 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2233 TREEVIEW_ITEM *retval;
2237 /* handle all the global data here */
2240 case TVGN_CHILD: /* Special case: child of 0 is root */
2245 retval = infoPtr->root->firstChild;
2249 retval = infoPtr->selectedItem;
2252 case TVGN_FIRSTVISIBLE:
2253 retval = infoPtr->firstVisible;
2256 case TVGN_DROPHILITE:
2257 retval = infoPtr->dropItem;
2260 case TVGN_LASTVISIBLE:
2261 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2267 TRACE("flags:%x, returns %p\n", which, retval);
2268 return (LRESULT)retval;
2271 if (item == TVI_ROOT) item = infoPtr->root;
2273 if (!TREEVIEW_ValidItem(infoPtr, item))
2279 retval = item->nextSibling;
2282 retval = item->prevSibling;
2285 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2288 retval = item->firstChild;
2290 case TVGN_NEXTVISIBLE:
2291 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2293 case TVGN_PREVIOUSVISIBLE:
2294 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2297 TRACE("Unknown msg %x,item %p\n", which, item);
2301 TRACE("flags:%x, item %p;returns %p\n", which, item, retval);
2302 return (LRESULT)retval;
2307 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2309 TRACE(" %d\n", infoPtr->uNumItems);
2310 return (LRESULT)infoPtr->uNumItems;
2314 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2316 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2318 static const unsigned int state_table[] = { 0, 2, 1 };
2322 state = STATEIMAGEINDEX(item->state);
2323 TRACE("state:%x\n", state);
2324 item->state &= ~TVIS_STATEIMAGEMASK;
2327 state = state_table[state];
2329 item->state |= INDEXTOSTATEIMAGEMASK(state);
2331 TRACE("state:%x\n", state);
2332 TREEVIEW_Invalidate(infoPtr, item);
2337 /* Painting *************************************************************/
2339 /* Draw the lines and expand button for an item. Also draws one section
2340 * of the line from item's parent to item's parent's next sibling. */
2342 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2344 LONG centerx, centery;
2345 BOOL lar = ((infoPtr->dwStyle
2346 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2349 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2351 if (!lar && item->iLevel == 0)
2354 hbr = CreateSolidBrush(clrBk);
2355 hbrOld = SelectObject(hdc, hbr);
2357 centerx = (item->linesOffset + item->stateOffset) / 2;
2358 centery = (item->rect.top + item->rect.bottom) / 2;
2360 if (infoPtr->dwStyle & TVS_HASLINES)
2362 HPEN hOldPen, hNewPen;
2366 /* Get a dotted grey pen */
2367 lb.lbStyle = BS_SOLID;
2368 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2369 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2370 hOldPen = SelectObject(hdc, hNewPen);
2372 /* Make sure the center is on a dot (using +2 instead
2373 * of +1 gives us pixel-by-pixel compat with native) */
2374 centery = (centery + 2) & ~1;
2376 MoveToEx(hdc, item->stateOffset, centery, NULL);
2377 LineTo(hdc, centerx - 1, centery);
2379 if (item->prevSibling || item->parent != infoPtr->root)
2381 MoveToEx(hdc, centerx, item->rect.top, NULL);
2382 LineTo(hdc, centerx, centery);
2385 if (item->nextSibling)
2387 MoveToEx(hdc, centerx, centery, NULL);
2388 LineTo(hdc, centerx, item->rect.bottom + 1);
2391 /* Draw the line from our parent to its next sibling. */
2392 parent = item->parent;
2393 while (parent != infoPtr->root)
2395 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2397 if (parent->nextSibling
2398 /* skip top-levels unless TVS_LINESATROOT */
2399 && parent->stateOffset > parent->linesOffset)
2401 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2402 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2405 parent = parent->parent;
2408 SelectObject(hdc, hOldPen);
2409 DeleteObject(hNewPen);
2413 * Display the (+/-) signs
2416 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2418 if (item->cChildren)
2420 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2423 RECT glyphRect = item->rect;
2424 glyphRect.left = item->linesOffset;
2425 glyphRect.right = item->stateOffset;
2426 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2427 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2432 LONG height = item->rect.bottom - item->rect.top;
2433 LONG width = item->stateOffset - item->linesOffset;
2434 LONG rectsize = min(height, width) / 4;
2435 /* plussize = ceil(rectsize * 3/4) */
2436 LONG plussize = (rectsize + 1) * 3 / 4;
2438 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2439 HPEN old_pen = SelectObject(hdc, new_pen);
2441 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2442 centerx + rectsize + 2, centery + rectsize + 2);
2444 SelectObject(hdc, old_pen);
2445 DeleteObject(new_pen);
2447 /* draw +/- signs with current text color */
2448 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2449 old_pen = SelectObject(hdc, new_pen);
2451 if (height < 18 || width < 18)
2453 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2454 LineTo(hdc, centerx + plussize, centery);
2456 if (!(item->state & TVIS_EXPANDED))
2458 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2459 LineTo(hdc, centerx, centery + plussize);
2464 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2465 centerx + plussize, centery + 2);
2467 if (!(item->state & TVIS_EXPANDED))
2469 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2470 centerx + 2, centery + plussize);
2471 SetPixel(hdc, centerx - 1, centery, clrBk);
2472 SetPixel(hdc, centerx + 1, centery, clrBk);
2476 SelectObject(hdc, old_pen);
2477 DeleteObject(new_pen);
2481 SelectObject(hdc, hbrOld);
2486 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2490 COLORREF oldTextColor, oldTextBkColor;
2492 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2493 NMTVCUSTOMDRAW nmcdhdr;
2495 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2497 /* - If item is drop target or it is selected and window is in focus -
2498 * use blue background (COLOR_HIGHLIGHT).
2499 * - If item is selected, window is not in focus, but it has style
2500 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2501 * - Otherwise - use background color
2503 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2504 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2505 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2507 if ((item->state & TVIS_DROPHILITED) || inFocus)
2509 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2510 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2514 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2515 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2520 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2521 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2522 nmcdhdr.clrText = comctl32_color.clrHighlight;
2524 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2527 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2529 /* The custom draw handler can query the text rectangle,
2531 /* should already be known, set to 0 when changed */
2532 if (!item->textWidth)
2533 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2537 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2539 cditem = TREEVIEW_SendCustomDrawItemNotify
2540 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2541 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2543 if (cditem & CDRF_SKIPDEFAULT)
2545 SelectObject(hdc, hOldFont);
2550 if (cditem & CDRF_NEWFONT)
2551 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2553 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2555 /* Set colors. Custom draw handler can change these so we do this after it. */
2556 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2557 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2559 centery = (item->rect.top + item->rect.bottom) / 2;
2562 * Display the images associated with this item
2567 /* State images are displayed to the left of the Normal image
2568 * image number is in state; zero should be `display no image'.
2570 imageIndex = STATEIMAGEINDEX(item->state);
2572 if (infoPtr->himlState && imageIndex)
2574 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2576 centery - infoPtr->stateImageHeight / 2,
2580 /* Now, draw the normal image; can be either selected,
2581 * non-selected or expanded image.
2584 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2586 /* The item is currently selected */
2587 imageIndex = item->iSelectedImage;
2589 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2591 /* The item is currently not selected but expanded */
2592 imageIndex = item->iExpandedImage;
2596 /* The item is not selected and not expanded */
2597 imageIndex = item->iImage;
2600 if (infoPtr->himlNormal)
2602 int ovlIdx = item->state & TVIS_OVERLAYMASK;
2604 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2606 centery - infoPtr->normalImageHeight / 2,
2607 ILD_NORMAL | ovlIdx);
2613 * Display the text associated with this item
2616 /* Don't paint item's text if it's being edited */
2617 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2623 rcText.top = item->rect.top;
2624 rcText.bottom = item->rect.bottom;
2625 rcText.left = item->textOffset;
2626 rcText.right = rcText.left + item->textWidth + 4;
2628 TRACE("drawing text %s at (%s)\n",
2629 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2632 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2633 ETO_CLIPPED | ETO_OPAQUE,
2636 lstrlenW(item->pszText),
2639 /* Draw the box around the selected item */
2640 if ((item == infoPtr->selectedItem) && inFocus)
2642 DrawFocusRect(hdc,&rcText);
2648 /* Draw insertion mark if necessary */
2650 if (infoPtr->insertMarkItem)
2651 TRACE("item:%d,mark:%p\n",
2652 TREEVIEW_GetItemIndex(infoPtr, item),
2653 infoPtr->insertMarkItem);
2655 if (item == infoPtr->insertMarkItem)
2657 HPEN hNewPen, hOldPen;
2661 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2662 hOldPen = SelectObject(hdc, hNewPen);
2664 if (infoPtr->insertBeforeorAfter)
2665 offset = item->rect.bottom - 1;
2667 offset = item->rect.top + 1;
2669 left = item->textOffset - 2;
2670 right = item->textOffset + item->textWidth + 2;
2672 MoveToEx(hdc, left, offset - 3, NULL);
2673 LineTo(hdc, left, offset + 4);
2675 MoveToEx(hdc, left, offset, NULL);
2676 LineTo(hdc, right + 1, offset);
2678 MoveToEx(hdc, right, offset + 3, NULL);
2679 LineTo(hdc, right, offset - 4);
2681 SelectObject(hdc, hOldPen);
2682 DeleteObject(hNewPen);
2685 if (cditem & CDRF_NOTIFYPOSTPAINT)
2687 cditem = TREEVIEW_SendCustomDrawItemNotify
2688 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2689 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2692 /* Restore the hdc state */
2693 SetTextColor(hdc, oldTextColor);
2694 SetBkColor(hdc, oldTextBkColor);
2695 SelectObject(hdc, hOldFont);
2698 /* Computes treeHeight and treeWidth and updates the scroll bars.
2701 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2703 TREEVIEW_ITEM *item;
2704 HWND hwnd = infoPtr->hwnd;
2708 LONG scrollX = infoPtr->scrollX;
2710 infoPtr->treeWidth = 0;
2711 infoPtr->treeHeight = 0;
2713 /* We iterate through all visible items in order to get the tree height
2715 item = infoPtr->root->firstChild;
2717 while (item != NULL)
2719 if (ISVISIBLE(item))
2721 /* actually we draw text at textOffset + 2 */
2722 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2723 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2725 /* This is scroll-adjusted, but we fix this below. */
2726 infoPtr->treeHeight = item->rect.bottom;
2729 item = TREEVIEW_GetNextListItem(infoPtr, item);
2732 /* Fix the scroll adjusted treeHeight and treeWidth. */
2733 if (infoPtr->root->firstChild)
2734 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2736 infoPtr->treeWidth += infoPtr->scrollX;
2738 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2740 /* Adding one scroll bar may take up enough space that it forces us
2741 * to add the other as well. */
2742 if (infoPtr->treeHeight > infoPtr->clientHeight)
2746 if (infoPtr->treeWidth
2747 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2750 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2753 if (!vert && horz && infoPtr->treeHeight
2754 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2757 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2759 si.cbSize = sizeof(SCROLLINFO);
2760 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2765 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2766 if ( si.nPage && NULL != infoPtr->firstVisible)
2768 si.nPos = infoPtr->firstVisible->visibleOrder;
2769 si.nMax = infoPtr->maxVisibleOrder - 1;
2771 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2773 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2774 ShowScrollBar(hwnd, SB_VERT, TRUE);
2775 infoPtr->uInternalStatus |= TV_VSCROLL;
2779 if (infoPtr->uInternalStatus & TV_VSCROLL)
2780 ShowScrollBar(hwnd, SB_VERT, FALSE);
2781 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2786 if (infoPtr->uInternalStatus & TV_VSCROLL)
2787 ShowScrollBar(hwnd, SB_VERT, FALSE);
2788 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2793 si.nPage = infoPtr->clientWidth;
2794 si.nPos = infoPtr->scrollX;
2795 si.nMax = infoPtr->treeWidth - 1;
2797 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2799 si.nPos = si.nMax - max( si.nPage-1, 0 );
2803 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2804 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2805 infoPtr->uInternalStatus |= TV_HSCROLL;
2807 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2808 TREEVIEW_HScroll(infoPtr,
2809 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2813 if (infoPtr->uInternalStatus & TV_HSCROLL)
2814 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2815 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2818 if (infoPtr->scrollX != 0)
2820 TREEVIEW_HScroll(infoPtr,
2821 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2826 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2830 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2833 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2835 hBrush = CreateSolidBrush(clrBk);
2836 FillRect(hdc, rc, hBrush);
2837 DeleteObject(hBrush);
2840 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2842 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2846 TRACE("%p\n", infoPtr);
2848 GetClientRect(infoPtr->hwnd, &rect);
2849 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2855 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2857 HWND hwnd = infoPtr->hwnd;
2859 TREEVIEW_ITEM *item;
2861 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2863 TRACE("empty window\n");
2867 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2870 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2872 ReleaseDC(hwnd, hdc);
2876 for (item = infoPtr->root->firstChild;
2878 item = TREEVIEW_GetNextListItem(infoPtr, item))
2880 if (ISVISIBLE(item))
2882 /* Avoid unneeded calculations */
2883 if (item->rect.top > rect.bottom)
2885 if (item->rect.bottom < rect.top)
2888 TREEVIEW_DrawItem(infoPtr, hdc, item);
2892 TREEVIEW_UpdateScrollBars(infoPtr);
2894 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2896 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2900 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2902 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2906 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2909 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2911 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2915 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2921 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2926 GetClientRect(infoPtr->hwnd, &rc);
2927 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2931 hdc = BeginPaint(infoPtr->hwnd, &ps);
2934 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2937 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2938 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2941 EndPaint(infoPtr->hwnd, &ps);
2947 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2949 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2951 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2954 if (options & PRF_ERASEBKGND)
2955 TREEVIEW_EraseBackground(infoPtr, hdc);
2957 if (options & PRF_CLIENT)
2960 GetClientRect(infoPtr->hwnd, &rc);
2961 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2967 /* Sorting **************************************************************/
2969 /***************************************************************************
2970 * Forward the DPA local callback to the treeview owner callback
2973 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2974 const TVSORTCB *pCallBackSort)
2976 /* Forward the call to the client-defined callback */
2977 return pCallBackSort->lpfnCompare(first->lParam,
2979 pCallBackSort->lParam);
2982 /***************************************************************************
2983 * Treeview native sort routine: sort on item text.
2986 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2987 const TREEVIEW_INFO *infoPtr)
2989 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2990 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2992 if(first->pszText && second->pszText)
2993 return lstrcmpiW(first->pszText, second->pszText);
2994 else if(first->pszText)
2996 else if(second->pszText)
3002 /* Returns the number of physical children belonging to item. */
3004 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3009 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3015 /* Returns a DPA containing a pointer to each physical child of item in
3016 * sibling order. If item has no children, an empty DPA is returned. */
3018 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3020 HTREEITEM child = item->firstChild;
3022 HDPA list = DPA_Create(8);
3023 if (list == 0) return NULL;
3025 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3027 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3037 /***************************************************************************
3038 * Setup the treeview structure with regards of the sort method
3039 * and sort the children of the TV item specified in lParam
3040 * fRecurse: currently unused. Should be zero.
3041 * parent: if pSort!=NULL, should equal pSort->hParent.
3042 * otherwise, item which child items are to be sorted.
3043 * pSort: sort method info. if NULL, sort on item text.
3044 * if non-NULL, sort on item's lParam content, and let the
3045 * application decide what that means. See also TVM_SORTCHILDRENCB.
3049 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3053 PFNDPACOMPARE pfnCompare;
3056 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3057 if (parent == TVI_ROOT || parent == NULL)
3058 parent = infoPtr->root;
3060 /* Check for a valid handle to the parent item */
3061 if (!TREEVIEW_ValidItem(infoPtr, parent))
3063 ERR("invalid item hParent=%p\n", parent);
3069 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3070 lpCompare = (LPARAM)pSort;
3074 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3075 lpCompare = (LPARAM)infoPtr;
3078 cChildren = TREEVIEW_CountChildren(parent);
3080 /* Make sure there is something to sort */
3083 /* TREEVIEW_ITEM rechaining */
3086 HTREEITEM nextItem = 0;
3087 HTREEITEM prevItem = 0;
3089 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3091 if (sortList == NULL)
3094 /* let DPA sort the list */
3095 DPA_Sort(sortList, pfnCompare, lpCompare);
3097 /* The order of DPA entries has been changed, so fixup the
3098 * nextSibling and prevSibling pointers. */
3100 item = DPA_GetPtr(sortList, count++);
3101 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3103 /* link the two current item together */
3104 item->nextSibling = nextItem;
3105 nextItem->prevSibling = item;
3107 if (prevItem == NULL)
3109 /* this is the first item, update the parent */
3110 parent->firstChild = item;
3111 item->prevSibling = NULL;
3115 /* fix the back chaining */
3116 item->prevSibling = prevItem;
3119 /* get ready for the next one */
3124 /* the last item is pointed to by item and never has a sibling */
3125 item->nextSibling = NULL;
3126 parent->lastChild = item;
3128 DPA_Destroy(sortList);
3130 TREEVIEW_VerifyTree(infoPtr);
3132 if (parent->state & TVIS_EXPANDED)
3134 int visOrder = infoPtr->firstVisible->visibleOrder;
3136 if (parent == infoPtr->root)
3137 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3139 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3141 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3143 TREEVIEW_ITEM *item;
3145 for (item = infoPtr->root->firstChild; item != NULL;
3146 item = TREEVIEW_GetNextListItem(infoPtr, item))
3148 if (item->visibleOrder == visOrder)
3152 if (!item) item = parent->firstChild;
3153 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3156 TREEVIEW_Invalidate(infoPtr, NULL);
3165 /***************************************************************************
3166 * Setup the treeview structure with regards of the sort method
3167 * and sort the children of the TV item specified in lParam
3170 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3172 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3176 /***************************************************************************
3177 * Sort the children of the TV item specified in lParam.
3180 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3182 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3186 /* Expansion/Collapse ***************************************************/
3189 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3192 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3193 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3194 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3199 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3202 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3203 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3204 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3209 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3210 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3212 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3213 BOOL bRemoveChildren, BOOL bUser)
3215 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3216 BOOL bSetSelection, bSetFirstVisible;
3218 LONG scrollDist = 0;
3219 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3221 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3223 if (!(item->state & TVIS_EXPANDED))
3226 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3227 TREEVIEW_SendExpanding(infoPtr, item, action);
3229 if (item->firstChild == NULL)
3232 item->state &= ~TVIS_EXPANDED;
3234 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3235 TREEVIEW_SendExpanded(infoPtr, item, action);
3237 bSetSelection = (infoPtr->selectedItem != NULL
3238 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3240 bSetFirstVisible = (infoPtr->firstVisible != NULL
3241 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3246 if (tmpItem->nextSibling)
3248 nextItem = tmpItem->nextSibling;
3251 tmpItem = tmpItem->parent;
3255 scrollDist = nextItem->rect.top;
3257 if (bRemoveChildren)
3259 INT old_cChildren = item->cChildren;
3260 TRACE("TVE_COLLAPSERESET\n");
3261 item->state &= ~TVIS_EXPANDEDONCE;
3262 TREEVIEW_RemoveAllChildren(infoPtr, item);
3263 item->cChildren = old_cChildren;
3266 if (item->firstChild)
3268 TREEVIEW_ITEM *i, *sibling;
3270 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3272 for (i = item->firstChild; i != sibling;
3273 i = TREEVIEW_GetNextListItem(infoPtr, i))
3275 i->visibleOrder = -1;
3279 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3282 scrollDist = -(scrollDist - nextItem->rect.top);
3286 /* Don't call DoSelectItem, it sends notifications. */
3287 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3288 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3289 item->state |= TVIS_SELECTED;
3290 infoPtr->selectedItem = item;
3293 TREEVIEW_UpdateScrollBars(infoPtr);
3295 scrollRect.left = 0;
3296 scrollRect.right = infoPtr->clientWidth;
3297 scrollRect.bottom = infoPtr->clientHeight;
3301 scrollRect.top = nextItem->rect.top;
3303 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3304 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3305 TREEVIEW_Invalidate(infoPtr, item);
3307 scrollRect.top = item->rect.top;
3308 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3311 TREEVIEW_SetFirstVisible(infoPtr,
3312 bSetFirstVisible ? item : infoPtr->firstVisible,
3319 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3320 BOOL partial, BOOL user)
3323 LONG orgNextTop = 0;
3325 TREEVIEW_ITEM *nextItem, *tmpItem;
3326 BOOL sendsNotifications;
3328 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, item, partial, user);
3330 if (item->state & TVIS_EXPANDED)
3333 tmpItem = item; nextItem = NULL;
3336 if (tmpItem->nextSibling)
3338 nextItem = tmpItem->nextSibling;
3341 tmpItem = tmpItem->parent;
3345 orgNextTop = nextItem->rect.top;
3347 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3349 sendsNotifications = user || ((item->cChildren != 0) &&
3350 !(item->state & TVIS_EXPANDEDONCE));
3351 if (sendsNotifications)
3353 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3355 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3359 if (!item->firstChild)
3362 item->state |= TVIS_EXPANDED;
3365 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3367 if (ISVISIBLE(item))
3369 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3370 TREEVIEW_UpdateSubTree(infoPtr, item);
3371 TREEVIEW_UpdateScrollBars(infoPtr);
3373 scrollRect.left = 0;
3374 scrollRect.bottom = infoPtr->treeHeight;
3375 scrollRect.right = infoPtr->clientWidth;
3378 scrollDist = nextItem->rect.top - orgNextTop;
3379 scrollRect.top = orgNextTop;
3381 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3382 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3383 TREEVIEW_Invalidate (infoPtr, item);
3385 scrollRect.top = item->rect.top;
3386 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3389 /* Scroll up so that as many children as possible are visible.
3390 * This fails when expanding causes an HScroll bar to appear, but we
3391 * don't know that yet, so the last item is obscured. */
3392 if (item->firstChild != NULL)
3394 int nChildren = item->lastChild->visibleOrder
3395 - item->firstChild->visibleOrder + 1;
3397 int visible_pos = item->visibleOrder
3398 - infoPtr->firstVisible->visibleOrder;
3400 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3402 if (visible_pos > 0 && nChildren > rows_below)
3404 int scroll = nChildren - rows_below;
3406 if (scroll > visible_pos)
3407 scroll = visible_pos;
3411 TREEVIEW_ITEM *newFirstVisible
3412 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3416 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3422 if (sendsNotifications) {
3423 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3424 item->state |= TVIS_EXPANDEDONCE;
3430 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3431 to mouse messages and TVM_SELECTITEM.
3433 selection - previously selected item, used to collapse a part of a tree
3434 item - new selected item
3436 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3437 HTREEITEM selection, HTREEITEM item)
3439 TREEVIEW_ITEM *SelItem;
3441 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3443 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3446 * Close the previous selection all the way to the root
3447 * as long as the new selection is not a child
3449 if(selection && (selection != item))
3451 BOOL closeit = TRUE;
3454 /* determine if the hitItem is a child of the currently selected item */
3455 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3456 (SelItem->parent != infoPtr->root))
3458 closeit = (SelItem != selection);
3459 SelItem = SelItem->parent;
3464 if(TREEVIEW_ValidItem(infoPtr, selection))
3465 SelItem = selection;
3467 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3468 SelItem->parent != infoPtr->root)
3470 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3471 SelItem = SelItem->parent;
3477 * Expand the current item
3479 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3483 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL bUser)
3487 if (item->state & TVIS_EXPANDED)
3488 return TREEVIEW_Collapse(infoPtr, item, FALSE, bUser);
3490 return TREEVIEW_Expand(infoPtr, item, FALSE, bUser);
3494 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3496 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3498 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3500 if (TREEVIEW_HasChildren(infoPtr, item))
3501 TREEVIEW_ExpandAll(infoPtr, item);
3505 /* Note:If the specified item is the child of a collapsed parent item,
3506 the parent's list of child items is (recursively) expanded to reveal the
3507 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3508 know if it also applies here.
3512 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3514 if (!TREEVIEW_ValidItem(infoPtr, item))
3517 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3518 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3521 switch (flag & TVE_TOGGLE)
3524 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3528 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3532 return TREEVIEW_Toggle(infoPtr, item, TRUE);
3539 /* Hit-Testing **********************************************************/
3541 static TREEVIEW_ITEM *
3542 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3544 TREEVIEW_ITEM *item;
3547 if (!infoPtr->firstVisible)
3550 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3552 for (item = infoPtr->firstVisible; item != NULL;
3553 item = TREEVIEW_GetNextListItem(infoPtr, item))
3555 if (row >= item->visibleOrder
3556 && row < item->visibleOrder + item->iIntegral)
3564 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3566 TREEVIEW_ITEM *item;
3572 GetClientRect(infoPtr->hwnd, &rect);
3579 status |= TVHT_TOLEFT;
3581 else if (x > rect.right)
3583 status |= TVHT_TORIGHT;
3588 status |= TVHT_ABOVE;
3590 else if (y > rect.bottom)
3592 status |= TVHT_BELOW;
3597 lpht->flags = status;
3601 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3604 lpht->flags = TVHT_NOWHERE;
3608 if (x >= item->textOffset + item->textWidth)
3610 lpht->flags = TVHT_ONITEMRIGHT;
3612 else if (x >= item->textOffset)
3614 lpht->flags = TVHT_ONITEMLABEL;
3616 else if (x >= item->imageOffset)
3618 lpht->flags = TVHT_ONITEMICON;
3620 else if (x >= item->stateOffset)
3622 lpht->flags = TVHT_ONITEMSTATEICON;
3624 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3626 lpht->flags = TVHT_ONITEMBUTTON;
3630 lpht->flags = TVHT_ONITEMINDENT;
3634 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3636 return (LRESULT)item;
3639 /* Item Label Editing ***************************************************/
3642 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3644 return (LRESULT)infoPtr->hwndEdit;
3647 static LRESULT CALLBACK
3648 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3650 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3651 BOOL bCancel = FALSE;
3657 TRACE("WM_PAINT start\n");
3658 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3660 TRACE("WM_PAINT done\n");
3664 if (infoPtr->bIgnoreEditKillFocus)
3670 WNDPROC editProc = infoPtr->wpEditOrig;
3671 infoPtr->wpEditOrig = 0;
3672 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3673 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3677 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3680 if (wParam == VK_ESCAPE)
3685 else if (wParam == VK_RETURN)
3692 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3695 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3696 /* eg. Using a messagebox */
3698 infoPtr->bIgnoreEditKillFocus = TRUE;
3699 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3700 infoPtr->bIgnoreEditKillFocus = FALSE;
3706 /* should handle edit control messages here */
3709 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3711 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3713 switch (HIWORD(wParam))
3718 * Adjust the edit window size
3721 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3722 HDC hdc = GetDC(infoPtr->hwndEdit);
3724 HFONT hFont, hOldFont = 0;
3726 TRACE("edit=%p\n", infoPtr->hwndEdit);
3728 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3730 infoPtr->bLabelChanged = TRUE;
3732 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3734 /* Select font to get the right dimension of the string */
3735 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3739 hOldFont = SelectObject(hdc, hFont);
3742 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3744 TEXTMETRICW textMetric;
3746 /* Add Extra spacing for the next character */
3747 GetTextMetricsW(hdc, &textMetric);
3748 sz.cx += (textMetric.tmMaxCharWidth * 2);
3750 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3752 infoPtr->clientWidth - editItem->textOffset + 2);
3754 SetWindowPos(infoPtr->hwndEdit,
3759 editItem->rect.bottom - editItem->rect.top + 3,
3760 SWP_NOMOVE | SWP_DRAWFRAME);
3765 SelectObject(hdc, hOldFont);
3768 ReleaseDC(infoPtr->hwnd, hdc);
3772 /* apparently we should respect passed handle value */
3773 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3775 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3779 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3786 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3788 HWND hwnd = infoPtr->hwnd;
3791 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3794 TEXTMETRICW textMetric;
3796 TRACE("%p %p\n", hwnd, hItem);
3797 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3800 if (infoPtr->hwndEdit)
3801 return infoPtr->hwndEdit;
3803 infoPtr->bLabelChanged = FALSE;
3805 /* make edit item visible */
3806 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3808 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3811 /* Select the font to get appropriate metric dimensions */
3812 if (infoPtr->hFont != 0)
3814 hOldFont = SelectObject(hdc, infoPtr->hFont);
3817 /* Get string length in pixels */
3819 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3822 GetTextExtentPoint32A(hdc, "", 0, &sz);
3824 /* Add Extra spacing for the next character */
3825 GetTextMetricsW(hdc, &textMetric);
3826 sz.cx += (textMetric.tmMaxCharWidth * 2);
3828 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3829 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3831 if (infoPtr->hFont != 0)
3833 SelectObject(hdc, hOldFont);
3836 ReleaseDC(hwnd, hdc);
3838 infoPtr->editItem = hItem;
3840 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3843 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3844 WS_CLIPSIBLINGS | ES_WANTRETURN |
3845 ES_LEFT, hItem->textOffset - 2,
3846 hItem->rect.top - 1, sz.cx + 3,
3847 hItem->rect.bottom -
3848 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3849 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3851 infoPtr->hwndEdit = hwndEdit;
3853 /* Get a 2D border. */
3854 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3855 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3856 SetWindowLongW(hwndEdit, GWL_STYLE,
3857 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3859 SendMessageW(hwndEdit, WM_SETFONT,
3860 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3862 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3864 TREEVIEW_Edit_SubclassProc);
3866 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3868 DestroyWindow(hwndEdit);
3869 infoPtr->hwndEdit = 0;
3870 infoPtr->editItem = NULL;
3875 SetWindowTextW(hwndEdit, hItem->pszText);
3878 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3879 ShowWindow(hwndEdit, SW_SHOW);
3886 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3888 HWND hwnd = infoPtr->hwnd;
3889 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3892 WCHAR tmpText[1024] = { '\0' };
3893 WCHAR *newText = tmpText;
3896 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3898 tvdi.hdr.hwndFrom = hwnd;
3899 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3900 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3902 tvdi.item.hItem = editedItem;
3903 tvdi.item.state = editedItem->state;
3904 tvdi.item.lParam = editedItem->lParam;
3908 if (!infoPtr->bNtfUnicode)
3909 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3911 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3913 if (iLength >= 1023)
3915 ERR("Insufficient space to retrieve new item label\n");
3918 tvdi.item.mask = TVIF_TEXT;
3919 tvdi.item.pszText = tmpText;
3920 tvdi.item.cchTextMax = iLength + 1;
3924 tvdi.item.pszText = NULL;
3925 tvdi.item.cchTextMax = 0;
3928 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
3930 if (!bCancel && bCommit) /* Apply the changes */
3932 if (!infoPtr->bNtfUnicode)
3934 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3935 newText = Alloc(len * sizeof(WCHAR));
3936 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3940 if (strcmpW(newText, editedItem->pszText) != 0)
3942 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3945 ERR("OutOfMemory, cannot allocate space for label\n");
3946 if(newText != tmpText) Free(newText);
3947 DestroyWindow(infoPtr->hwndEdit);
3948 infoPtr->hwndEdit = 0;
3949 infoPtr->editItem = NULL;
3954 editedItem->pszText = ptr;
3955 editedItem->cchTextMax = iLength + 1;
3956 strcpyW(editedItem->pszText, newText);
3957 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3960 if(newText != tmpText) Free(newText);
3963 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3964 DestroyWindow(infoPtr->hwndEdit);
3965 infoPtr->hwndEdit = 0;
3966 infoPtr->editItem = NULL;
3971 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3973 if (wParam != TV_EDIT_TIMER)
3975 ERR("got unknown timer\n");
3979 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3980 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3982 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3988 /* Mouse Tracking/Drag **************************************************/
3990 /***************************************************************************
3991 * This is quite unusual piece of code, but that's how it's implemented in
3995 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3997 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3998 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4002 r.top = pt.y - cyDrag;
4003 r.left = pt.x - cxDrag;
4004 r.bottom = pt.y + cyDrag;
4005 r.right = pt.x + cxDrag;
4007 SetCapture(infoPtr->hwnd);
4011 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4013 if (msg.message == WM_MOUSEMOVE)
4015 pt.x = (short)LOWORD(msg.lParam);
4016 pt.y = (short)HIWORD(msg.lParam);
4017 if (PtInRect(&r, pt))
4025 else if (msg.message >= WM_LBUTTONDOWN &&
4026 msg.message <= WM_RBUTTONDBLCLK)
4028 if (msg.message == WM_RBUTTONUP)
4029 TREEVIEW_RButtonUp(infoPtr, &pt);
4033 DispatchMessageW(&msg);
4036 if (GetCapture() != infoPtr->hwnd)
4046 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4048 TREEVIEW_ITEM *item;
4052 SetFocus(infoPtr->hwnd);
4054 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4056 /* If there is pending 'edit label' event - kill it now */
4057 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4060 hit.pt.x = (short)LOWORD(lParam);
4061 hit.pt.y = (short)HIWORD(lParam);
4063 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4066 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4068 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4072 case TVHT_ONITEMRIGHT:
4073 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4076 case TVHT_ONITEMINDENT:
4077 if (!(infoPtr->dwStyle & TVS_HASLINES))
4083 int level = hit.pt.x / infoPtr->uIndent;
4084 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4086 while (item->iLevel > level)
4088 item = item->parent;
4094 case TVHT_ONITEMLABEL:
4095 case TVHT_ONITEMICON:
4096 case TVHT_ONITEMBUTTON:
4097 TREEVIEW_Toggle(infoPtr, item, TRUE);
4100 case TVHT_ONITEMSTATEICON:
4101 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4102 TREEVIEW_ToggleItemState(infoPtr, item);
4104 TREEVIEW_Toggle(infoPtr, item, TRUE);
4113 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4115 HWND hwnd = infoPtr->hwnd;
4117 BOOL bTrack, bDoLabelEdit;
4119 /* If Edit control is active - kill it and return.
4120 * The best way to do it is to set focus to itself.
4121 * Edit control subclassed procedure will automatically call
4124 if (infoPtr->hwndEdit)
4130 ht.pt.x = (short)LOWORD(lParam);
4131 ht.pt.y = (short)HIWORD(lParam);
4133 TREEVIEW_HitTest(infoPtr, &ht);
4134 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4136 /* update focusedItem and redraw both items */
4137 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4139 infoPtr->focusedItem = ht.hItem;
4140 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4141 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4144 bTrack = (ht.flags & TVHT_ONITEM)
4145 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4148 * If the style allows editing and the node is already selected
4149 * and the click occurred on the item label...
4151 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4152 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4154 /* Send NM_CLICK right away */
4156 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4159 if (ht.flags & TVHT_ONITEMBUTTON)
4161 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4165 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4166 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4168 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4169 infoPtr->dropItem = ht.hItem;
4171 /* clean up focusedItem as we dragged and won't select this item */
4172 if(infoPtr->focusedItem)
4174 /* refresh the item that was focused */
4175 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4176 infoPtr->focusedItem = NULL;
4178 /* refresh the selected item to return the filled background */
4179 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4186 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4191 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4192 KillTimer(hwnd, TV_EDIT_TIMER);
4194 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4195 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4197 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4199 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4201 /* Select the current item */
4202 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4203 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4205 else if (ht.flags & TVHT_ONITEMSTATEICON)
4207 /* TVS_CHECKBOXES requires us to toggle the current state */
4208 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4209 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4219 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4223 if (infoPtr->hwndEdit)
4225 SetFocus(infoPtr->hwnd);
4229 ht.pt.x = (short)LOWORD(lParam);
4230 ht.pt.y = (short)HIWORD(lParam);
4232 TREEVIEW_HitTest(infoPtr, &ht);
4234 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4238 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4239 infoPtr->dropItem = ht.hItem;
4244 SetFocus(infoPtr->hwnd);
4245 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4252 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4258 TREEVIEW_HitTest(infoPtr, &ht);
4262 /* Change to screen coordinate for WM_CONTEXTMENU */
4263 ClientToScreen(infoPtr->hwnd, &ht.pt);
4265 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4266 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4267 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4274 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4276 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4280 HBITMAP hbmp, hOldbmp;
4287 if (!(infoPtr->himlNormal))
4290 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4293 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4295 hwtop = GetDesktopWindow();
4296 htopdc = GetDC(hwtop);
4297 hdc = CreateCompatibleDC(htopdc);
4299 hOldFont = SelectObject(hdc, infoPtr->hFont);
4301 if (dragItem->pszText)
4302 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4305 GetTextExtentPoint32A(hdc, "", 0, &size);
4307 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4308 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4309 hOldbmp = SelectObject(hdc, hbmp);
4311 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4316 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4317 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4321 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4322 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4325 /* draw item text */
4327 SetRect(&rc, cx, 0, size.cx, size.cy);
4329 if (dragItem->pszText)
4330 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4333 SelectObject(hdc, hOldFont);
4334 SelectObject(hdc, hOldbmp);
4336 ImageList_Add(infoPtr->dragList, hbmp, 0);
4340 ReleaseDC(hwtop, htopdc);
4342 return (LRESULT)infoPtr->dragList;
4345 /* Selection ************************************************************/
4348 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4351 TREEVIEW_ITEM *prevSelect;
4353 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4355 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4356 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4357 newSelect ? newSelect->state : 0);
4359 /* reset and redraw focusedItem if focusedItem was set so we don't */
4360 /* have to worry about the previously focused item when we set a new one */
4361 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4362 infoPtr->focusedItem = NULL;
4366 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4367 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4370 prevSelect = infoPtr->selectedItem;
4372 if (prevSelect == newSelect) {
4373 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4377 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4380 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4386 prevSelect->state &= ~TVIS_SELECTED;
4388 newSelect->state |= TVIS_SELECTED;
4390 infoPtr->selectedItem = newSelect;
4392 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4394 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4395 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4397 TREEVIEW_SendTreeviewNotify(infoPtr,
4400 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4405 case TVGN_DROPHILITE:
4406 prevSelect = infoPtr->dropItem;
4409 prevSelect->state &= ~TVIS_DROPHILITED;
4411 infoPtr->dropItem = newSelect;
4414 newSelect->state |= TVIS_DROPHILITED;
4416 TREEVIEW_Invalidate(infoPtr, prevSelect);
4417 TREEVIEW_Invalidate(infoPtr, newSelect);
4420 case TVGN_FIRSTVISIBLE:
4421 if (newSelect != NULL)
4423 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4424 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4425 TREEVIEW_Invalidate(infoPtr, NULL);
4430 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4434 /* FIXME: handle NM_KILLFOCUS etc */
4436 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4438 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4440 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4443 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4445 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4448 TREEVIEW_SingleExpand(infoPtr, selection, item);
4453 /*************************************************************************
4454 * TREEVIEW_ProcessLetterKeys
4456 * Processes keyboard messages generated by pressing the letter keys
4458 * What this does is perform a case insensitive search from the
4459 * current position with the following quirks:
4460 * - If two chars or more are pressed in quick succession we search
4461 * for the corresponding string (e.g. 'abc').
4462 * - If there is a delay we wipe away the current search string and
4463 * restart with just that char.
4464 * - If the user keeps pressing the same character, whether slowly or
4465 * fast, so that the search string is entirely composed of this
4466 * character ('aaaaa' for instance), then we search for first item
4467 * that starting with that character.
4468 * - If the user types the above character in quick succession, then
4469 * we must also search for the corresponding string ('aaaaa'), and
4470 * go to that string if there is a match.
4478 * - The current implementation has a list of characters it will
4479 * accept and it ignores everything else. In particular it will
4480 * ignore accentuated characters which seems to match what
4481 * Windows does. But I'm not sure it makes sense to follow
4483 * - We don't sound a beep when the search fails.
4484 * - The search should start from the focused item, not from the selected
4485 * item. One reason for this is to allow for multiple selections in trees.
4486 * But currently infoPtr->focusedItem does not seem very usable.
4490 * TREEVIEW_ProcessLetterKeys
4492 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4495 HTREEITEM endidx,idx;
4497 WCHAR buffer[MAX_PATH];
4498 DWORD timestamp,elapsed;
4500 /* simple parameter checking */
4501 if (!charCode || !keyData) return 0;
4503 /* only allow the valid WM_CHARs through */
4504 if (!isalnum(charCode) &&
4505 charCode != '.' && charCode != '`' && charCode != '!' &&
4506 charCode != '@' && charCode != '#' && charCode != '$' &&
4507 charCode != '%' && charCode != '^' && charCode != '&' &&
4508 charCode != '*' && charCode != '(' && charCode != ')' &&
4509 charCode != '-' && charCode != '_' && charCode != '+' &&
4510 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4511 charCode != '}' && charCode != '[' && charCode != '{' &&
4512 charCode != '/' && charCode != '?' && charCode != '>' &&
4513 charCode != '<' && charCode != ',' && charCode != '~')
4516 /* compute how much time elapsed since last keypress */
4517 timestamp = GetTickCount();
4518 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4519 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4521 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4524 /* update the search parameters */
4525 infoPtr->lastKeyPressTimestamp=timestamp;
4526 if (elapsed < KEY_DELAY) {
4527 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4528 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4530 if (infoPtr->charCode != charCode) {
4531 infoPtr->charCode=charCode=0;
4534 infoPtr->charCode=charCode;
4535 infoPtr->szSearchParam[0]=charCode;
4536 infoPtr->nSearchParamLength=1;
4537 /* Redundant with the 1 char string */
4541 /* and search from the current position */
4543 if (infoPtr->selectedItem != NULL) {
4544 endidx=infoPtr->selectedItem;
4545 /* if looking for single character match,
4546 * then we must always move forward
4548 if (infoPtr->nSearchParamLength == 1)
4549 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4554 idx=infoPtr->root->firstChild;
4557 /* At the end point, sort out wrapping */
4560 /* If endidx is null, stop at the last item (ie top to bottom) */
4564 /* Otherwise, start again at the very beginning */
4565 idx=infoPtr->root->firstChild;
4567 /* But if we are stopping on the first child, end now! */
4568 if (idx == endidx) break;
4572 ZeroMemory(&item, sizeof(item));
4573 item.mask = TVIF_TEXT;
4575 item.pszText = buffer;
4576 item.cchTextMax = sizeof(buffer);
4577 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4579 /* check for a match */
4580 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4583 } else if ( (charCode != 0) && (nItem == NULL) &&
4584 (nItem != infoPtr->selectedItem) &&
4585 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4586 /* This would work but we must keep looking for a longer match */
4589 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4590 } while (idx != endidx);
4592 if (nItem != NULL) {
4593 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4594 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4601 /* Scrolling ************************************************************/
4604 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4607 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4608 HTREEITEM newFirstVisible = NULL;
4609 int visible_pos = -1;
4611 if (!TREEVIEW_ValidItem(infoPtr, item))
4614 if (!ISVISIBLE(item))
4616 /* Expand parents as necessary. */
4619 /* see if we are trying to ensure that root is visible */
4620 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4621 parent = item->parent;
4623 parent = item; /* this item is the topmost item */
4625 while (parent != infoPtr->root)
4627 if (!(parent->state & TVIS_EXPANDED))
4628 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4630 parent = parent->parent;
4634 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4636 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4637 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4639 if (hasFirstVisible)
4640 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4642 if (visible_pos < 0)
4644 /* item is before the start of the list: put it at the top. */
4645 newFirstVisible = item;
4647 else if (visible_pos >= viscount
4648 /* Sometimes, before we are displayed, GVC is 0, causing us to
4649 * spuriously scroll up. */
4650 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4652 /* item is past the end of the list. */
4653 int scroll = visible_pos - viscount;
4655 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4661 /* Scroll window so item's text is visible as much as possible */
4662 /* Calculation of amount of extra space is taken from EditLabel code */
4664 TEXTMETRICW textMetric;
4665 HDC hdc = GetWindowDC(infoPtr->hwnd);
4667 x = item->textWidth;
4669 GetTextMetricsW(hdc, &textMetric);
4670 ReleaseDC(infoPtr->hwnd, hdc);
4672 x += (textMetric.tmMaxCharWidth * 2);
4673 x = max(x, textMetric.tmMaxCharWidth * 3);
4675 if (item->textOffset < 0)
4676 pos = item->textOffset;
4677 else if (item->textOffset + x > infoPtr->clientWidth)
4679 if (x > infoPtr->clientWidth)
4680 pos = item->textOffset;
4682 pos = item->textOffset + x - infoPtr->clientWidth;
4687 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4690 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4692 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4701 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4702 TREEVIEW_ITEM *newFirstVisible,
4703 BOOL bUpdateScrollPos)
4707 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4709 if (newFirstVisible != NULL)
4711 /* Prevent an empty gap from appearing at the bottom... */
4712 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4713 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4717 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4720 /* ... unless we just don't have enough items. */
4721 if (newFirstVisible == NULL)
4722 newFirstVisible = infoPtr->root->firstChild;
4726 if (infoPtr->firstVisible != newFirstVisible)
4728 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4730 infoPtr->firstVisible = newFirstVisible;
4731 TREEVIEW_Invalidate(infoPtr, NULL);
4735 TREEVIEW_ITEM *item;
4736 int scroll = infoPtr->uItemHeight *
4737 (infoPtr->firstVisible->visibleOrder
4738 - newFirstVisible->visibleOrder);
4740 infoPtr->firstVisible = newFirstVisible;
4742 for (item = infoPtr->root->firstChild; item != NULL;
4743 item = TREEVIEW_GetNextListItem(infoPtr, item))
4745 item->rect.top += scroll;
4746 item->rect.bottom += scroll;
4749 if (bUpdateScrollPos)
4750 SetScrollPos(infoPtr->hwnd, SB_VERT,
4751 newFirstVisible->visibleOrder, TRUE);
4753 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4758 /************************************************************************
4759 * VScroll is always in units of visible items. i.e. we always have a
4760 * visible item aligned to the top of the control. (Unless we have no
4764 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4766 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4767 TREEVIEW_ITEM *newFirstVisible = NULL;
4769 int nScrollCode = LOWORD(wParam);
4771 TRACE("wp %lx\n", wParam);
4773 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4776 if (!oldFirstVisible)
4778 assert(infoPtr->root->firstChild == NULL);
4782 switch (nScrollCode)
4785 newFirstVisible = infoPtr->root->firstChild;
4789 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4793 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4797 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4801 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4802 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4806 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4807 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4811 case SB_THUMBPOSITION:
4812 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4813 infoPtr->root->firstChild,
4814 (LONG)(SHORT)HIWORD(wParam));
4821 if (newFirstVisible != NULL)
4823 if (newFirstVisible != oldFirstVisible)
4824 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4825 nScrollCode != SB_THUMBTRACK);
4826 else if (nScrollCode == SB_THUMBPOSITION)
4827 SetScrollPos(infoPtr->hwnd, SB_VERT,
4828 newFirstVisible->visibleOrder, TRUE);
4835 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4838 int scrollX = infoPtr->scrollX;
4839 int nScrollCode = LOWORD(wParam);
4841 TRACE("wp %lx\n", wParam);
4843 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4846 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4847 /* shall never occur */
4854 switch (nScrollCode)
4857 scrollX -= infoPtr->uItemHeight;
4860 scrollX += infoPtr->uItemHeight;
4863 scrollX -= infoPtr->clientWidth;
4866 scrollX += infoPtr->clientWidth;
4870 case SB_THUMBPOSITION:
4871 scrollX = (int)(SHORT)HIWORD(wParam);
4878 if (scrollX > maxWidth)
4880 else if (scrollX < 0)
4884 if (scrollX != infoPtr->scrollX)
4886 TREEVIEW_ITEM *item;
4887 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4889 for (item = infoPtr->root->firstChild; item != NULL;
4890 item = TREEVIEW_GetNextListItem(infoPtr, item))
4892 item->linesOffset += scroll_pixels;
4893 item->stateOffset += scroll_pixels;
4894 item->imageOffset += scroll_pixels;
4895 item->textOffset += scroll_pixels;
4898 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4899 infoPtr->scrollX = scrollX;
4900 UpdateWindow(infoPtr->hwnd);
4903 if (nScrollCode != SB_THUMBTRACK)
4904 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4910 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4913 UINT pulScrollLines = 3;
4915 if (wParam & (MK_SHIFT | MK_CONTROL))
4916 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4918 if (infoPtr->firstVisible == NULL)
4921 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4923 gcWheelDelta = -(short)HIWORD(wParam);
4924 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4926 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4928 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4929 int maxDy = infoPtr->maxVisibleOrder;
4937 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4942 /* Create/Destroy *******************************************************/
4945 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4948 HBITMAP hbm, hbmOld;
4952 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4954 hdcScreen = GetDC(0);
4956 hdc = CreateCompatibleDC(hdcScreen);
4957 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4958 hbmOld = SelectObject(hdc, hbm);
4960 SetRect(&rc, 0, 0, 48, 16);
4961 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4963 SetRect(&rc, 18, 2, 30, 14);
4964 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4965 DFCS_BUTTONCHECK|DFCS_FLAT);
4967 SetRect(&rc, 34, 2, 46, 14);
4968 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4969 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4971 SelectObject(hdc, hbmOld);
4972 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4973 comctl32_color.clrWindow);
4974 TRACE("checkbox index %d\n", nIndex);
4978 ReleaseDC(0, hdcScreen);
4980 infoPtr->stateImageWidth = 16;
4981 infoPtr->stateImageHeight = 16;
4985 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4988 TREEVIEW_INFO *infoPtr;
4991 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4993 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4995 if (infoPtr == NULL)
4997 ERR("could not allocate info memory!\n");
5001 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5003 infoPtr->hwnd = hwnd;
5004 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5006 infoPtr->uNumItems = 0;
5007 infoPtr->cdmode = 0;
5008 infoPtr->uScrollTime = 300; /* milliseconds */
5009 infoPtr->bRedraw = TRUE;
5011 GetClientRect(hwnd, &rcClient);
5013 /* No scroll bars yet. */
5014 infoPtr->clientWidth = rcClient.right;
5015 infoPtr->clientHeight = rcClient.bottom;
5016 infoPtr->uInternalStatus = 0;
5018 infoPtr->treeWidth = 0;
5019 infoPtr->treeHeight = 0;
5021 infoPtr->uIndent = MINIMUM_INDENT;
5022 infoPtr->selectedItem = NULL;
5023 infoPtr->focusedItem = NULL;
5024 infoPtr->hotItem = NULL;
5025 infoPtr->editItem = NULL;
5026 infoPtr->firstVisible = NULL;
5027 infoPtr->maxVisibleOrder = 0;
5028 infoPtr->dropItem = NULL;
5029 infoPtr->insertMarkItem = NULL;
5030 infoPtr->insertBeforeorAfter = 0;
5033 infoPtr->scrollX = 0;
5035 infoPtr->clrBk = CLR_NONE; /* use system color */
5036 infoPtr->clrText = CLR_NONE; /* use system color */
5037 infoPtr->clrLine = CLR_DEFAULT;
5038 infoPtr->clrInsertMark = CLR_DEFAULT;
5042 infoPtr->hwndEdit = NULL;
5043 infoPtr->wpEditOrig = NULL;
5044 infoPtr->bIgnoreEditKillFocus = FALSE;
5045 infoPtr->bLabelChanged = FALSE;
5047 infoPtr->himlNormal = NULL;
5048 infoPtr->himlState = NULL;
5049 infoPtr->normalImageWidth = 0;
5050 infoPtr->normalImageHeight = 0;
5051 infoPtr->stateImageWidth = 0;
5052 infoPtr->stateImageHeight = 0;
5054 infoPtr->items = DPA_Create(16);
5056 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5057 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5058 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5059 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5060 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5062 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5064 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5065 infoPtr->root->state = TVIS_EXPANDED;
5066 infoPtr->root->iLevel = -1;
5067 infoPtr->root->visibleOrder = -1;
5069 infoPtr->hwndNotify = lpcs->hwndParent;
5070 infoPtr->hwndToolTip = 0;
5072 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5074 /* Determine what type of notify should be issued */
5075 /* sets infoPtr->bNtfUnicode */
5076 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5078 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5079 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5080 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5083 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5084 initialize_checkboxes(infoPtr);
5086 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5087 ShowScrollBar(hwnd, SB_VERT, FALSE);
5088 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5090 OpenThemeData (hwnd, themeClass);
5097 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5101 /* free item data */
5102 TREEVIEW_RemoveTree(infoPtr);
5103 /* root isn't freed with other items */
5104 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5105 DPA_Destroy(infoPtr->items);
5107 /* tool tip is automatically destroyed: we are its owner */
5109 /* Restore original wndproc */
5110 if (infoPtr->hwndEdit)
5111 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5112 (DWORD_PTR)infoPtr->wpEditOrig);
5114 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5116 /* Deassociate treeview from the window before doing anything drastic. */
5117 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5120 DeleteObject(infoPtr->hDefaultFont);
5121 DeleteObject(infoPtr->hBoldFont);
5122 DeleteObject(infoPtr->hUnderlineFont);
5128 /* Miscellaneous Messages ***********************************************/
5131 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5139 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5140 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5141 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5142 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5143 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5144 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5145 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5146 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5147 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5151 if (key >= VK_PRIOR && key <= VK_DOWN)
5153 unsigned char code = scroll[key - VK_PRIOR].code;
5155 (((code & (1 << 7)) == (SB_HORZ << 7))
5157 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5163 /************************************************************************
5166 * VK_UP Move selection to the previous non-hidden item.
5167 * VK_DOWN Move selection to the next non-hidden item.
5168 * VK_HOME Move selection to the first item.
5169 * VK_END Move selection to the last item.
5170 * VK_LEFT If expanded then collapse, otherwise move to parent.
5171 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5173 * VK_SUBTRACT Collapse.
5174 * VK_MULTIPLY Expand all.
5175 * VK_PRIOR Move up GetVisibleCount items.
5176 * VK_NEXT Move down GetVisibleCount items.
5177 * VK_BACK Move to parent.
5178 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5181 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5183 /* If it is non-NULL and different, it will be selected and visible. */
5184 TREEVIEW_ITEM *newSelection = NULL;
5186 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5188 TRACE("%lx\n", wParam);
5190 if (prevItem == NULL)
5193 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5194 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5199 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5201 newSelection = infoPtr->root->firstChild;
5205 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5209 newSelection = infoPtr->root->firstChild;
5213 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5217 if (prevItem->state & TVIS_EXPANDED)
5219 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5221 else if (prevItem->parent != infoPtr->root)
5223 newSelection = prevItem->parent;
5228 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5230 if (!(prevItem->state & TVIS_EXPANDED))
5231 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5234 newSelection = prevItem->firstChild;
5241 TREEVIEW_ExpandAll(infoPtr, prevItem);
5245 if (!(prevItem->state & TVIS_EXPANDED))
5246 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5250 if (prevItem->state & TVIS_EXPANDED)
5251 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5256 = TREEVIEW_GetListItem(infoPtr, prevItem,
5257 -TREEVIEW_GetVisibleCount(infoPtr));
5262 = TREEVIEW_GetListItem(infoPtr, prevItem,
5263 TREEVIEW_GetVisibleCount(infoPtr));
5267 newSelection = prevItem->parent;
5268 if (newSelection == infoPtr->root)
5269 newSelection = NULL;
5273 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5274 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5278 if (newSelection && newSelection != prevItem)
5280 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5283 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5291 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5293 /* remove hot effect from item */
5294 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5295 infoPtr->hotItem = NULL;
5301 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5304 TRACKMOUSEEVENT trackinfo;
5305 TREEVIEW_ITEM * item;
5307 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5309 /* fill in the TRACKMOUSEEVENT struct */
5310 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5311 trackinfo.dwFlags = TME_QUERY;
5312 trackinfo.hwndTrack = infoPtr->hwnd;
5314 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5315 _TrackMouseEvent(&trackinfo);
5317 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5318 if(!(trackinfo.dwFlags & TME_LEAVE))
5320 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5321 trackinfo.hwndTrack = infoPtr->hwnd;
5322 /* do it as fast as possible, minimal systimer latency will be used */
5323 trackinfo.dwHoverTime = 1;
5325 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5326 /* and can properly deactivate the hot item */
5327 _TrackMouseEvent(&trackinfo);
5330 pt.x = (short)LOWORD(lParam);
5331 pt.y = (short)HIWORD(lParam);
5333 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5335 if (item != infoPtr->hotItem)
5337 /* redraw old hot item */
5338 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5339 infoPtr->hotItem = item;
5340 /* redraw new hot item */
5341 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5347 /* Draw themed border */
5348 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5350 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5354 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5355 cyEdge = GetSystemMetrics (SM_CYEDGE);
5358 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5360 GetWindowRect(infoPtr->hwnd, &r);
5362 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5363 r.right - cxEdge, r.bottom - cyEdge);
5364 if (region != (HRGN)1)
5365 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5366 OffsetRect(&r, -r.left, -r.top);
5368 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5369 OffsetRect(&r, -r.left, -r.top);
5371 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5372 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5373 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5374 ReleaseDC(infoPtr->hwnd, dc);
5376 /* Call default proc to get the scrollbars etc. painted */
5377 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5383 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5385 LPNMHDR lpnmh = (LPNMHDR)lParam;
5387 if (lpnmh->code == PGN_CALCSIZE) {
5388 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5390 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5391 lppgc->iWidth = infoPtr->treeWidth;
5392 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5393 infoPtr->treeWidth, infoPtr->clientWidth);
5396 lppgc->iHeight = infoPtr->treeHeight;
5397 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5398 infoPtr->treeHeight, infoPtr->clientHeight);
5402 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5406 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5408 if (wParam == SIZE_RESTORED)
5410 infoPtr->clientWidth = (short)LOWORD(lParam);
5411 infoPtr->clientHeight = (short)HIWORD(lParam);
5413 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5414 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5415 TREEVIEW_UpdateScrollBars(infoPtr);
5419 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5422 TREEVIEW_Invalidate(infoPtr, NULL);
5426 static void TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5428 TREEVIEW_ITEM *child = item->firstChild;
5430 item->state &= ~TVIS_STATEIMAGEMASK;
5431 item->state |= INDEXTOSTATEIMAGEMASK(1);
5435 TREEVIEW_ITEM *next = child->nextSibling;
5436 TREEVIEW_ResetImageStateIndex(infoPtr, child);
5442 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5444 TRACE("(%lx %lx)\n", wParam, lParam);
5446 if (wParam == GWL_STYLE)
5448 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5450 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5452 if (dwNewStyle & TVS_CHECKBOXES)
5454 initialize_checkboxes(infoPtr);
5455 TRACE("checkboxes enabled\n");
5457 /* set all items to state image index 1 */
5458 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5462 FIXME("tried to disable checkboxes\n");
5466 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5468 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5470 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5471 TRACE("tooltips enabled\n");
5475 DestroyWindow(infoPtr->hwndToolTip);
5476 infoPtr->hwndToolTip = 0;
5477 TRACE("tooltips disabled\n");
5481 infoPtr->dwStyle = dwNewStyle;
5484 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5485 TREEVIEW_UpdateScrollBars(infoPtr);
5486 TREEVIEW_Invalidate(infoPtr, NULL);
5492 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5495 TREEVIEW_ITEM * item;
5499 ScreenToClient(infoPtr->hwnd, &pt);
5501 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5503 memset(&nmmouse, 0, sizeof(nmmouse));
5504 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5505 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5506 nmmouse.hdr.code = NM_SETCURSOR;
5509 nmmouse.dwItemSpec = (DWORD_PTR)item;
5510 nmmouse.dwItemData = item->lParam;
5514 nmmouse.dwHitInfo = lParam;
5515 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, &nmmouse.hdr))
5518 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5520 SetCursor(infoPtr->hcurHand);
5524 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5528 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5532 if (!infoPtr->selectedItem)
5534 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5538 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5539 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5544 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5548 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5549 UpdateWindow(infoPtr->hwnd);
5550 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5554 /* update theme after a WM_THEMECHANGED message */
5555 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5557 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5558 CloseThemeData (theme);
5559 OpenThemeData (infoPtr->hwnd, themeClass);
5564 static LRESULT WINAPI
5565 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5567 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5569 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5571 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5574 if (uMsg == WM_CREATE)
5575 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5582 case TVM_CREATEDRAGIMAGE:
5583 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5585 case TVM_DELETEITEM:
5586 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5588 case TVM_EDITLABELA:
5589 case TVM_EDITLABELW:
5590 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5592 case TVM_ENDEDITLABELNOW:
5593 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5595 case TVM_ENSUREVISIBLE:
5596 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5599 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5601 case TVM_GETBKCOLOR:
5602 return TREEVIEW_GetBkColor(infoPtr);
5605 return TREEVIEW_GetCount(infoPtr);
5607 case TVM_GETEDITCONTROL:
5608 return TREEVIEW_GetEditControl(infoPtr);
5610 case TVM_GETIMAGELIST:
5611 return TREEVIEW_GetImageList(infoPtr, wParam);
5614 return TREEVIEW_GetIndent(infoPtr);
5616 case TVM_GETINSERTMARKCOLOR:
5617 return TREEVIEW_GetInsertMarkColor(infoPtr);
5619 case TVM_GETISEARCHSTRINGA:
5620 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5623 case TVM_GETISEARCHSTRINGW:
5624 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5629 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5630 uMsg == TVM_GETITEMW);
5631 case TVM_GETITEMHEIGHT:
5632 return TREEVIEW_GetItemHeight(infoPtr);
5634 case TVM_GETITEMRECT:
5635 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5637 case TVM_GETITEMSTATE:
5638 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5640 case TVM_GETLINECOLOR:
5641 return TREEVIEW_GetLineColor(infoPtr);
5643 case TVM_GETNEXTITEM:
5644 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5646 case TVM_GETSCROLLTIME:
5647 return TREEVIEW_GetScrollTime(infoPtr);
5649 case TVM_GETTEXTCOLOR:
5650 return TREEVIEW_GetTextColor(infoPtr);
5652 case TVM_GETTOOLTIPS:
5653 return TREEVIEW_GetToolTips(infoPtr);
5655 case TVM_GETUNICODEFORMAT:
5656 return TREEVIEW_GetUnicodeFormat(infoPtr);
5658 case TVM_GETVISIBLECOUNT:
5659 return TREEVIEW_GetVisibleCount(infoPtr);
5662 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5664 case TVM_INSERTITEMA:
5665 case TVM_INSERTITEMW:
5666 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5667 uMsg == TVM_INSERTITEMW);
5668 case TVM_SELECTITEM:
5669 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5671 case TVM_SETBKCOLOR:
5672 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5674 case TVM_SETIMAGELIST:
5675 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5678 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5680 case TVM_SETINSERTMARK:
5681 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5683 case TVM_SETINSERTMARKCOLOR:
5684 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5688 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5689 uMsg == TVM_SETITEMW);
5690 case TVM_SETLINECOLOR:
5691 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5693 case TVM_SETITEMHEIGHT:
5694 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5696 case TVM_SETSCROLLTIME:
5697 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5699 case TVM_SETTEXTCOLOR:
5700 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5702 case TVM_SETTOOLTIPS:
5703 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5705 case TVM_SETUNICODEFORMAT:
5706 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5708 case TVM_SORTCHILDREN:
5709 return TREEVIEW_SortChildren(infoPtr, lParam);
5711 case TVM_SORTCHILDRENCB:
5712 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5715 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5718 return TREEVIEW_Command(infoPtr, wParam, lParam);
5721 return TREEVIEW_Destroy(infoPtr);
5726 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5729 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5732 return TREEVIEW_GetFont(infoPtr);
5735 return TREEVIEW_HScroll(infoPtr, wParam);
5738 return TREEVIEW_KeyDown(infoPtr, wParam);
5741 return TREEVIEW_KillFocus(infoPtr);
5743 case WM_LBUTTONDBLCLK:
5744 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5746 case WM_LBUTTONDOWN:
5747 return TREEVIEW_LButtonDown(infoPtr, lParam);
5749 /* WM_MBUTTONDOWN */
5752 return TREEVIEW_MouseLeave(infoPtr);
5755 return TREEVIEW_MouseMove(infoPtr, lParam);
5757 case WM_NCLBUTTONDOWN:
5758 if (infoPtr->hwndEdit)
5759 SetFocus(infoPtr->hwnd);
5763 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5766 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5768 case WM_NOTIFYFORMAT:
5769 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5771 case WM_PRINTCLIENT:
5772 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5775 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5777 case WM_RBUTTONDOWN:
5778 return TREEVIEW_RButtonDown(infoPtr, lParam);
5781 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5784 return TREEVIEW_SetFocus(infoPtr);
5787 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5790 return TREEVIEW_SetRedraw(infoPtr, wParam);
5793 return TREEVIEW_Size(infoPtr, wParam, lParam);
5795 case WM_STYLECHANGED:
5796 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5798 case WM_SYSCOLORCHANGE:
5799 COMCTL32_RefreshSysColors();
5805 return TREEVIEW_HandleTimer(infoPtr, wParam);
5807 case WM_THEMECHANGED:
5808 return TREEVIEW_ThemeChanged (infoPtr);
5811 return TREEVIEW_VScroll(infoPtr, wParam);
5813 /* WM_WININICHANGE */
5816 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5819 TRACE("drawItem\n");
5823 /* This mostly catches MFC and Delphi messages. :( */
5824 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5825 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5827 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5832 /* Class Registration ***************************************************/
5835 TREEVIEW_Register(void)
5841 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5842 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5843 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5844 wndClass.cbClsExtra = 0;
5845 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5847 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5848 wndClass.hbrBackground = 0;
5849 wndClass.lpszClassName = WC_TREEVIEWW;
5851 RegisterClassW(&wndClass);
5856 TREEVIEW_Unregister(void)
5858 UnregisterClassW(WC_TREEVIEWW, NULL);
5862 /* Tree Verification ****************************************************/
5865 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5867 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5868 const TREEVIEW_ITEM *item)
5870 assert(infoPtr != NULL);
5871 assert(item != NULL);
5873 /* both NULL, or both non-null */
5874 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5876 assert(item->firstChild != item);
5877 assert(item->lastChild != item);
5879 if (item->firstChild)
5881 assert(item->firstChild->parent == item);
5882 assert(item->firstChild->prevSibling == NULL);
5885 if (item->lastChild)
5887 assert(item->lastChild->parent == item);
5888 assert(item->lastChild->nextSibling == NULL);
5891 assert(item->nextSibling != item);
5892 if (item->nextSibling)
5894 assert(item->nextSibling->parent == item->parent);
5895 assert(item->nextSibling->prevSibling == item);
5898 assert(item->prevSibling != item);
5899 if (item->prevSibling)
5901 assert(item->prevSibling->parent == item->parent);
5902 assert(item->prevSibling->nextSibling == item);
5907 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5909 assert(item != NULL);
5911 assert(item->parent != NULL);
5912 assert(item->parent != item);
5913 assert(item->iLevel == item->parent->iLevel + 1);
5915 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5917 TREEVIEW_VerifyItemCommon(infoPtr, item);
5919 TREEVIEW_VerifyChildren(infoPtr, item);
5923 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5925 const TREEVIEW_ITEM *child;
5926 assert(item != NULL);
5928 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5929 TREEVIEW_VerifyItem(infoPtr, child);
5933 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5935 TREEVIEW_ITEM *root = infoPtr->root;
5937 assert(root != NULL);
5938 assert(root->iLevel == -1);
5939 assert(root->parent == NULL);
5940 assert(root->prevSibling == NULL);
5942 TREEVIEW_VerifyItemCommon(infoPtr, root);
5944 TREEVIEW_VerifyChildren(infoPtr, root);
5948 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5950 if (!TRACE_ON(treeview)) return;
5952 assert(infoPtr != NULL);
5953 TREEVIEW_VerifyRoot(infoPtr);