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: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
32 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
38 * Make the insertion mark look right.
39 * Scroll (instead of repaint) as much as possible.
43 #include "wine/port.h"
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
63 #include "wine/unicode.h"
64 #include "wine/debug.h"
66 /* internal structures */
68 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
79 int iIntegral; /* item height multiplier (1 is normal) */
80 int iLevel; /* indentation level:0=root level */
81 HTREEITEM parent; /* handle to parent or 0 if at root */
82 HTREEITEM firstChild; /* handle to first child or 0 if no child */
84 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
85 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
91 LONG textWidth; /* horizontal text extent for pszText */
92 LONG visibleOrder; /* visible ordering, 0 is first visible item */
96 typedef struct tagTREEVIEW_INFO
99 HWND hwndNotify; /* Owner window to send notifications to */
102 UINT uInternalStatus;
104 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
105 INT cdmode; /* last custom draw setting */
106 UINT uScrollTime; /* max. time for scrolling in milliseconds */
107 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
109 UINT uItemHeight; /* item height */
112 LONG clientWidth; /* width of control window */
113 LONG clientHeight; /* height of control window */
115 LONG treeWidth; /* width of visible tree items */
116 LONG treeHeight; /* height of visible tree items */
118 UINT uIndent; /* indentation in pixels */
119 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
120 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
121 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
122 HTREEITEM editItem; /* item being edited with builtin edit box */
124 HTREEITEM firstVisible; /* handle to first visible item */
125 LONG maxVisibleOrder;
126 HTREEITEM dropItem; /* handle to item selected by drag cursor */
127 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
128 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
129 HIMAGELIST dragList; /* Bitmap of dragged item */
134 COLORREF clrInsertMark;
138 HFONT hUnderlineFont;
143 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
144 BOOL bIgnoreEditKillFocus;
147 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
148 HIMAGELIST himlNormal;
149 int normalImageHeight;
150 int normalImageWidth;
151 HIMAGELIST himlState;
152 int stateImageHeight;
156 DWORD lastKeyPressTimestamp; /* Added */
157 WPARAM charCode; /* Added */
158 INT nSearchParamLength; /* Added */
159 WCHAR szSearchParam[ MAX_PATH ]; /* Added */
163 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
164 #define KEY_DELAY 450
166 /* bitflags for infoPtr->uInternalStatus */
168 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
169 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
170 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
171 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
172 #define TV_RDRAG 0x10 /* ditto Rbutton */
173 #define TV_RDRAGGING 0x20
175 /* bitflags for infoPtr->timer */
177 #define TV_EDIT_TIMER 2
178 #define TV_EDIT_TIMER_SET 2
181 VOID TREEVIEW_Register (VOID);
182 VOID TREEVIEW_Unregister (VOID);
185 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
188 #define TEXT_CALLBACK_SIZE 260
190 #define TREEVIEW_LEFT_MARGIN 8
192 #define MINIMUM_INDENT 19
194 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
196 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
197 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
198 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
201 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
204 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
207 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
209 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
210 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
211 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
212 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
213 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
214 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
215 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
216 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND wParam, UINT lParam);
219 /* Random Utilities *****************************************************/
223 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
228 /* The definition is at the end of the file. */
229 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
232 /* Returns the treeview private data if hwnd is a treeview.
233 * Otherwise returns an undefined value. */
234 static TREEVIEW_INFO *
235 TREEVIEW_GetInfoPtr(HWND hwnd)
237 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
240 /* Don't call this. Nothing wants an item index. */
242 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
244 return DPA_GetPtrIndex(infoPtr->items, handle);
247 /* Checks if item has changed and needs to be redrawn */
248 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
249 const TVITEMEXW *tvChange)
251 /* Number of children has changed */
252 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
255 /* Image has changed and it's not a callback */
256 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
257 tiNew->iImage != I_IMAGECALLBACK)
260 /* Selected image has changed and it's not a callback */
261 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
262 tiNew->iSelectedImage != I_IMAGECALLBACK)
265 /* Text has changed and it's not a callback */
266 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
267 tiNew->pszText != LPSTR_TEXTCALLBACKW)
270 /* Indent has changed */
271 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
274 /* Item state has changed */
275 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
281 /***************************************************************************
282 * This method checks that handle is an item for this tree.
285 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
287 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
289 TRACE("invalid item %p\n", handle);
297 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
301 GetObjectW(hOrigFont, sizeof(font), &font);
302 font.lfWeight = FW_BOLD;
303 return CreateFontIndirectW(&font);
307 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
311 GetObjectW(hOrigFont, sizeof(font), &font);
312 font.lfUnderline = TRUE;
313 return CreateFontIndirectW(&font);
317 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
319 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
320 return infoPtr->hUnderlineFont;
321 if (item->state & TVIS_BOLD)
322 return infoPtr->hBoldFont;
323 return infoPtr->hFont;
326 /* for trace/debugging purposes only */
328 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
330 if (item == NULL) return "<null item>";
331 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
332 if (item->pszText == NULL) return "<null>";
333 return debugstr_w(item->pszText);
336 /* An item is not a child of itself. */
338 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
342 child = child->parent;
343 if (child == parent) return TRUE;
344 } while (child != NULL);
350 /* Tree Traversal *******************************************************/
352 /***************************************************************************
353 * This method returns the last expanded sibling or child child item
356 static TREEVIEW_ITEM *
357 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
362 while (wineItem->lastChild)
364 if (wineItem->state & TVIS_EXPANDED)
365 wineItem = wineItem->lastChild;
370 if (wineItem == infoPtr->root)
376 /***************************************************************************
377 * This method returns the previous non-hidden item in the list not
378 * considering the tree hierarchy.
380 static TREEVIEW_ITEM *
381 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
383 if (tvItem->prevSibling)
385 /* This item has a prevSibling, get the last item in the sibling's tree. */
386 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
388 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
389 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
395 /* this item does not have a prevSibling, get the parent */
396 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
401 /***************************************************************************
402 * This method returns the next physical item in the treeview not
403 * considering the tree hierarchy.
405 static TREEVIEW_ITEM *
406 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
409 * If this item has children and is expanded, return the first child
411 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
413 return tvItem->firstChild;
418 * try to get the sibling
420 if (tvItem->nextSibling)
421 return tvItem->nextSibling;
424 * Otherwise, get the parent's sibling.
426 while (tvItem->parent)
428 tvItem = tvItem->parent;
430 if (tvItem->nextSibling)
431 return tvItem->nextSibling;
437 /***************************************************************************
438 * This method returns the nth item starting at the given item. It returns
439 * the last item (or first) we we run out of items.
441 * Will scroll backward if count is <0.
442 * forward if count is >0.
444 static TREEVIEW_ITEM *
445 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
448 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
449 TREEVIEW_ITEM *previousItem;
451 assert(wineItem != NULL);
455 next_item = TREEVIEW_GetNextListItem;
460 next_item = TREEVIEW_GetPrevListItem;
467 previousItem = wineItem;
468 wineItem = next_item(infoPtr, wineItem);
470 } while (--count && wineItem != NULL);
473 return wineItem ? wineItem : previousItem;
476 /* Notifications ************************************************************/
478 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
480 if (!infoPtr->bNtfUnicode) {
482 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
483 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
484 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
485 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
486 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
487 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
488 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
489 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
490 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
491 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
492 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
493 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
500 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
502 TRACE("wParam=%ld, lParam=%ld\n", wParam, lParam);
503 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
507 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
510 HWND hwnd = infoPtr->hwnd;
513 nmhdr.hwndFrom = hwnd;
514 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
515 nmhdr.code = get_notifycode(infoPtr, code);
517 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, (LPARAM)&nmhdr);
521 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
524 tvItem->hItem = item;
525 tvItem->state = item->state;
526 tvItem->stateMask = 0;
527 tvItem->iImage = item->iImage;
528 tvItem->iSelectedImage = item->iSelectedImage;
529 tvItem->cChildren = item->cChildren;
530 tvItem->lParam = item->lParam;
534 if (!infoPtr->bNtfUnicode)
536 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
537 tvItem->pszText = Alloc (tvItem->cchTextMax);
538 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
542 tvItem->cchTextMax = item->cchTextMax;
543 tvItem->pszText = item->pszText;
548 tvItem->cchTextMax = 0;
549 tvItem->pszText = NULL;
554 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
555 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
557 HWND hwnd = infoPtr->hwnd;
561 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
562 code, action, oldItem, newItem);
564 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
566 nmhdr.hdr.hwndFrom = hwnd;
567 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
568 nmhdr.hdr.code = get_notifycode(infoPtr, code);
569 nmhdr.action = action;
572 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
575 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
580 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
581 if (!infoPtr->bNtfUnicode)
583 Free(nmhdr.itemOld.pszText);
584 Free(nmhdr.itemNew.pszText);
590 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
591 HTREEITEM dragItem, POINT pt)
593 HWND hwnd = infoPtr->hwnd;
596 TRACE("code:%d dragitem:%p\n", code, dragItem);
598 nmhdr.hdr.hwndFrom = hwnd;
599 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
600 nmhdr.hdr.code = get_notifycode(infoPtr, code);
602 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
603 nmhdr.itemNew.hItem = dragItem;
604 nmhdr.itemNew.state = dragItem->state;
605 nmhdr.itemNew.lParam = dragItem->lParam;
607 nmhdr.ptDrag.x = pt.x;
608 nmhdr.ptDrag.y = pt.y;
610 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
615 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
618 HWND hwnd = infoPtr->hwnd;
619 NMTVCUSTOMDRAW nmcdhdr;
622 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
624 nmcd = &nmcdhdr.nmcd;
625 nmcd->hdr.hwndFrom = hwnd;
626 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
627 nmcd->hdr.code = NM_CUSTOMDRAW;
628 nmcd->dwDrawStage = dwDrawStage;
631 nmcd->dwItemSpec = 0;
632 nmcd->uItemState = 0;
633 nmcd->lItemlParam = 0;
634 nmcdhdr.clrText = infoPtr->clrText;
635 nmcdhdr.clrTextBk = infoPtr->clrBk;
638 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)&nmcdhdr);
643 /* FIXME: need to find out when the flags in uItemState need to be set */
646 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
647 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
648 NMTVCUSTOMDRAW *nmcdhdr)
650 HWND hwnd = infoPtr->hwnd;
653 DWORD_PTR dwItemSpec;
657 dwDrawStage = CDDS_ITEM | uItemDrawState;
658 dwItemSpec = (DWORD_PTR)wineItem;
660 if (wineItem->state & TVIS_SELECTED)
661 uItemState |= CDIS_SELECTED;
662 if (wineItem == infoPtr->selectedItem)
663 uItemState |= CDIS_FOCUS;
664 if (wineItem == infoPtr->hotItem)
665 uItemState |= CDIS_HOT;
667 nmcd = &nmcdhdr->nmcd;
668 nmcd->hdr.hwndFrom = hwnd;
669 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
670 nmcd->hdr.code = NM_CUSTOMDRAW;
671 nmcd->dwDrawStage = dwDrawStage;
673 nmcd->rc = wineItem->rect;
674 nmcd->dwItemSpec = dwItemSpec;
675 nmcd->uItemState = uItemState;
676 nmcd->lItemlParam = wineItem->lParam;
677 nmcdhdr->iLevel = wineItem->iLevel;
679 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
680 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
681 nmcd->uItemState, nmcd->lItemlParam);
683 retval = TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)nmcdhdr);
689 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
691 HWND hwnd = infoPtr->hwnd;
695 tvdi.hdr.hwndFrom = hwnd;
696 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
697 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
699 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
700 &tvdi.item, editItem);
702 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
704 if (!infoPtr->bNtfUnicode)
705 Free(tvdi.item.pszText);
711 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
714 NMTVDISPINFOEXW callback;
715 HWND hwnd = infoPtr->hwnd;
717 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
718 mask &= wineItem->callbackMask;
720 if (mask == 0) return;
722 callback.hdr.hwndFrom = hwnd;
723 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
724 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
726 /* 'state' always contains valid value, as well as 'lParam'.
727 * All other parameters are uninitialized.
729 callback.item.pszText = wineItem->pszText;
730 callback.item.cchTextMax = wineItem->cchTextMax;
731 callback.item.mask = mask;
732 callback.item.hItem = wineItem;
733 callback.item.state = wineItem->state;
734 callback.item.lParam = wineItem->lParam;
736 /* If text is changed we need to recalculate textWidth */
737 if (mask & TVIF_TEXT)
738 wineItem->textWidth = 0;
740 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, (LPARAM)&callback);
742 /* It may have changed due to a call to SetItem. */
743 mask &= wineItem->callbackMask;
745 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
747 /* Instead of copying text into our buffer user specified its own */
748 if (!infoPtr->bNtfUnicode) {
751 int len = MultiByteToWideChar( CP_ACP, 0,
752 (LPSTR)callback.item.pszText, -1,
754 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
755 newText = ReAlloc(wineItem->pszText, buflen);
757 TRACE("returned str %s, len=%d, buflen=%d\n",
758 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
762 wineItem->pszText = newText;
763 MultiByteToWideChar( CP_ACP, 0,
764 (LPSTR)callback.item.pszText, -1,
765 wineItem->pszText, buflen/sizeof(WCHAR));
766 wineItem->cchTextMax = buflen/sizeof(WCHAR);
768 /* If ReAlloc fails we have nothing to do, but keep original text */
771 int len = max(lstrlenW(callback.item.pszText) + 1,
773 LPWSTR newText = ReAlloc(wineItem->pszText, len);
775 TRACE("returned wstr %s, len=%d\n",
776 debugstr_w(callback.item.pszText), len);
780 wineItem->pszText = newText;
781 strcpyW(wineItem->pszText, callback.item.pszText);
782 wineItem->cchTextMax = len;
784 /* If ReAlloc fails we have nothing to do, but keep original text */
787 else if (mask & TVIF_TEXT) {
788 /* User put text into our buffer, that is ok unless A string */
789 if (!infoPtr->bNtfUnicode) {
791 LPWSTR oldText = NULL;
793 int len = MultiByteToWideChar( CP_ACP, 0,
794 (LPSTR)callback.item.pszText, -1,
796 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
797 newText = Alloc(buflen);
799 TRACE("same buffer str %s, len=%d, buflen=%d\n",
800 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
804 oldText = wineItem->pszText;
805 wineItem->pszText = newText;
806 MultiByteToWideChar( CP_ACP, 0,
807 (LPSTR)callback.item.pszText, -1,
808 wineItem->pszText, buflen/sizeof(WCHAR));
809 wineItem->cchTextMax = buflen/sizeof(WCHAR);
815 if (mask & TVIF_IMAGE)
816 wineItem->iImage = callback.item.iImage;
818 if (mask & TVIF_SELECTEDIMAGE)
819 wineItem->iSelectedImage = callback.item.iSelectedImage;
821 if (mask & TVIF_CHILDREN)
822 wineItem->cChildren = callback.item.cChildren;
824 /* These members are now permanently set. */
825 if (callback.item.mask & TVIF_DI_SETITEM)
826 wineItem->callbackMask &= ~callback.item.mask;
829 /***************************************************************************
830 * This function uses cChildren field to decide whether the item has
832 * Note: if this returns TRUE, the child items may not actually exist,
833 * they could be virtual.
835 * Just use wineItem->firstChild to check for physical children.
838 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
840 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
842 return wineItem->cChildren > 0;
846 /* Item Position ********************************************************/
848 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
850 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
852 /* Same effect, different optimisation. */
854 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
855 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
857 BOOL lar = ((infoPtr->dwStyle
858 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
862 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
864 item->stateOffset = item->linesOffset + infoPtr->uIndent;
865 item->imageOffset = item->stateOffset
866 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
867 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
871 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
877 /* DRAW's OM docker creates items like this */
878 if (item->pszText == NULL)
890 hdc = GetDC(infoPtr->hwnd);
891 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
894 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
895 item->textWidth = sz.cx;
899 SelectObject(hdc, hOldFont);
905 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
907 item->rect.top = infoPtr->uItemHeight *
908 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
910 item->rect.bottom = item->rect.top
911 + infoPtr->uItemHeight * item->iIntegral - 1;
914 item->rect.right = infoPtr->clientWidth;
917 /* We know that only items after start need their order updated. */
919 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
926 start = infoPtr->root->firstChild;
930 order = start->visibleOrder;
932 for (item = start; item != NULL;
933 item = TREEVIEW_GetNextListItem(infoPtr, item))
935 if (!ISVISIBLE(item) && order > 0)
936 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
937 item->visibleOrder = order;
938 order += item->iIntegral;
941 infoPtr->maxVisibleOrder = order;
943 for (item = start; item != NULL;
944 item = TREEVIEW_GetNextListItem(infoPtr, item))
946 TREEVIEW_ComputeItemRect(infoPtr, item);
951 /* Update metrics of all items in selected subtree.
952 * root must be expanded
955 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
957 TREEVIEW_ITEM *sibling;
961 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
964 root->state &= ~TVIS_EXPANDED;
965 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
966 root->state |= TVIS_EXPANDED;
968 hdc = GetDC(infoPtr->hwnd);
969 hOldFont = SelectObject(hdc, infoPtr->hFont);
971 for (; root != sibling;
972 root = TREEVIEW_GetNextListItem(infoPtr, root))
974 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
976 if (root->callbackMask & TVIF_TEXT)
977 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
979 if (root->textWidth == 0)
981 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
982 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
986 SelectObject(hdc, hOldFont);
987 ReleaseDC(infoPtr->hwnd, hdc);
990 /* Item Allocation **********************************************************/
992 static TREEVIEW_ITEM *
993 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
995 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1000 /* I_IMAGENONE would make more sense but this is neither what is
1001 * documented (MSDN doesn't specify) nor what Windows actually does
1002 * (it sets it to zero)... and I can so imagine an application using
1003 * inc/dec to toggle the images. */
1004 newItem->iImage = 0;
1005 newItem->iSelectedImage = 0;
1007 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1016 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1017 * free item->pszText. */
1019 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1021 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1022 if (infoPtr->selectedItem == item)
1023 infoPtr->selectedItem = NULL;
1024 if (infoPtr->hotItem == item)
1025 infoPtr->hotItem = NULL;
1026 if (infoPtr->focusedItem == item)
1027 infoPtr->focusedItem = NULL;
1028 if (infoPtr->firstVisible == item)
1029 infoPtr->firstVisible = NULL;
1030 if (infoPtr->dropItem == item)
1031 infoPtr->dropItem = NULL;
1032 if (infoPtr->insertMarkItem == item)
1033 infoPtr->insertMarkItem = NULL;
1038 /* Item Insertion *******************************************************/
1040 /***************************************************************************
1041 * This method inserts newItem before sibling as a child of parent.
1042 * sibling can be NULL, but only if parent has no children.
1045 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1046 TREEVIEW_ITEM *parent)
1048 assert(parent != NULL);
1050 if (sibling != NULL)
1052 assert(sibling->parent == parent);
1054 if (sibling->prevSibling != NULL)
1055 sibling->prevSibling->nextSibling = newItem;
1057 newItem->prevSibling = sibling->prevSibling;
1058 sibling->prevSibling = newItem;
1061 newItem->prevSibling = NULL;
1063 newItem->nextSibling = sibling;
1065 if (parent->firstChild == sibling)
1066 parent->firstChild = newItem;
1068 if (parent->lastChild == NULL)
1069 parent->lastChild = newItem;
1072 /***************************************************************************
1073 * This method inserts newItem after sibling as a child of parent.
1074 * sibling can be NULL, but only if parent has no children.
1077 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1078 TREEVIEW_ITEM *parent)
1080 assert(parent != NULL);
1082 if (sibling != NULL)
1084 assert(sibling->parent == parent);
1086 if (sibling->nextSibling != NULL)
1087 sibling->nextSibling->prevSibling = newItem;
1089 newItem->nextSibling = sibling->nextSibling;
1090 sibling->nextSibling = newItem;
1093 newItem->nextSibling = NULL;
1095 newItem->prevSibling = sibling;
1097 if (parent->lastChild == sibling)
1098 parent->lastChild = newItem;
1100 if (parent->firstChild == NULL)
1101 parent->firstChild = newItem;
1105 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1106 const TVITEMEXW *tvItem, BOOL isW)
1108 UINT callbackClear = 0;
1109 UINT callbackSet = 0;
1111 TRACE("item %p\n", wineItem);
1112 /* Do this first in case it fails. */
1113 if (tvItem->mask & TVIF_TEXT)
1115 wineItem->textWidth = 0; /* force width recalculation */
1116 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1121 len = lstrlenW(tvItem->pszText) + 1;
1123 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1125 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1127 if (newText == NULL) return FALSE;
1129 callbackClear |= TVIF_TEXT;
1131 wineItem->pszText = newText;
1132 wineItem->cchTextMax = len;
1134 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1136 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1137 wineItem->pszText, len);
1139 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1143 callbackSet |= TVIF_TEXT;
1145 wineItem->pszText = ReAlloc(wineItem->pszText,
1146 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1147 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1148 TRACE("setting callback, item %p\n", wineItem);
1152 if (tvItem->mask & TVIF_CHILDREN)
1154 wineItem->cChildren = tvItem->cChildren;
1156 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1157 callbackSet |= TVIF_CHILDREN;
1159 callbackClear |= TVIF_CHILDREN;
1162 if (tvItem->mask & TVIF_IMAGE)
1164 wineItem->iImage = tvItem->iImage;
1166 if (wineItem->iImage == I_IMAGECALLBACK)
1167 callbackSet |= TVIF_IMAGE;
1169 callbackClear |= TVIF_IMAGE;
1172 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1174 wineItem->iSelectedImage = tvItem->iSelectedImage;
1176 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1177 callbackSet |= TVIF_SELECTEDIMAGE;
1179 callbackClear |= TVIF_SELECTEDIMAGE;
1182 if (tvItem->mask & TVIF_PARAM)
1183 wineItem->lParam = tvItem->lParam;
1185 /* If the application sets TVIF_INTEGRAL without
1186 * supplying a TVITEMEX structure, it's toast. */
1187 if (tvItem->mask & TVIF_INTEGRAL)
1188 wineItem->iIntegral = tvItem->iIntegral;
1190 if (tvItem->mask & TVIF_STATE)
1192 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1194 wineItem->state &= ~tvItem->stateMask;
1195 wineItem->state |= (tvItem->state & tvItem->stateMask);
1198 wineItem->callbackMask |= callbackSet;
1199 wineItem->callbackMask &= ~callbackClear;
1204 /* Note that the new item is pre-zeroed. */
1206 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1208 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1209 HTREEITEM insertAfter;
1210 TREEVIEW_ITEM *newItem, *parentItem;
1211 BOOL bTextUpdated = FALSE;
1213 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1215 parentItem = infoPtr->root;
1219 parentItem = ptdi->hParent;
1221 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1223 WARN("invalid parent %p\n", parentItem);
1228 insertAfter = ptdi->hInsertAfter;
1230 /* Validate this now for convenience. */
1231 switch ((DWORD_PTR)insertAfter)
1233 case (DWORD_PTR)TVI_FIRST:
1234 case (DWORD_PTR)TVI_LAST:
1235 case (DWORD_PTR)TVI_SORT:
1239 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1240 insertAfter->parent != parentItem)
1242 WARN("invalid insert after %p\n", insertAfter);
1243 insertAfter = TVI_LAST;
1247 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1248 (tvItem->mask & TVIF_TEXT)
1249 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1250 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1253 newItem = TREEVIEW_AllocateItem(infoPtr);
1254 if (newItem == NULL)
1257 newItem->parent = parentItem;
1258 newItem->iIntegral = 1;
1259 newItem->visibleOrder = -1;
1261 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1264 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1266 infoPtr->uNumItems++;
1268 switch ((DWORD_PTR)insertAfter)
1270 case (DWORD_PTR)TVI_FIRST:
1272 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1273 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1274 if (infoPtr->firstVisible == originalFirst)
1275 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1279 case (DWORD_PTR)TVI_LAST:
1280 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1283 /* hInsertAfter names a specific item we want to insert after */
1285 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1288 case (DWORD_PTR)TVI_SORT:
1290 TREEVIEW_ITEM *aChild;
1291 TREEVIEW_ITEM *previousChild = NULL;
1292 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1293 BOOL bItemInserted = FALSE;
1295 aChild = parentItem->firstChild;
1297 bTextUpdated = TRUE;
1298 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1300 /* Iterate the parent children to see where we fit in */
1301 while (aChild != NULL)
1305 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1306 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1308 if (comp < 0) /* we are smaller than the current one */
1310 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1311 if (infoPtr->firstVisible == originalFirst &&
1312 aChild == originalFirst)
1313 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1314 bItemInserted = TRUE;
1317 else if (comp > 0) /* we are bigger than the current one */
1319 previousChild = aChild;
1321 /* This will help us to exit if there is no more sibling */
1322 aChild = (aChild->nextSibling == 0)
1324 : aChild->nextSibling;
1326 /* Look at the next item */
1332 * An item with this name is already existing, therefore,
1333 * we add after the one we found
1335 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1336 bItemInserted = TRUE;
1342 * we reach the end of the child list and the item has not
1343 * yet been inserted, therefore, insert it after the last child.
1345 if ((!bItemInserted) && (aChild == NULL))
1346 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1353 TRACE("new item %p; parent %p, mask %x\n", newItem,
1354 newItem->parent, tvItem->mask);
1356 newItem->iLevel = newItem->parent->iLevel + 1;
1358 if (newItem->parent->cChildren == 0)
1359 newItem->parent->cChildren = 1;
1361 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1363 if (STATEIMAGEINDEX(newItem->state) == 0)
1364 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1367 if (infoPtr->firstVisible == NULL)
1368 infoPtr->firstVisible = newItem;
1370 TREEVIEW_VerifyTree(infoPtr);
1372 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1374 if (parentItem == infoPtr->root ||
1375 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1377 TREEVIEW_ITEM *item;
1378 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1380 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1381 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1384 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1386 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1387 TREEVIEW_UpdateScrollBars(infoPtr);
1389 * if the item was inserted in a visible part of the tree,
1390 * invalidate it, as well as those after it
1392 for (item = newItem;
1394 item = TREEVIEW_GetNextListItem(infoPtr, item))
1395 TREEVIEW_Invalidate(infoPtr, item);
1399 /* refresh treeview if newItem is the first item inserted under parentItem */
1400 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1402 /* parent got '+' - update it */
1403 TREEVIEW_Invalidate(infoPtr, parentItem);
1407 return (LRESULT)newItem;
1410 /* Item Deletion ************************************************************/
1412 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1415 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1417 TREEVIEW_ITEM *kill = parentItem->firstChild;
1419 while (kill != NULL)
1421 TREEVIEW_ITEM *next = kill->nextSibling;
1423 TREEVIEW_RemoveItem(infoPtr, kill);
1428 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1429 assert(parentItem->firstChild == NULL);
1430 assert(parentItem->lastChild == NULL);
1434 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1436 TREEVIEW_ITEM *parentItem = item->parent;
1438 assert(item != NULL);
1439 assert(item->parent != NULL); /* i.e. it must not be the root */
1441 if (parentItem->firstChild == item)
1442 parentItem->firstChild = item->nextSibling;
1444 if (parentItem->lastChild == item)
1445 parentItem->lastChild = item->prevSibling;
1447 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1448 && parentItem->cChildren > 0)
1449 parentItem->cChildren = 0;
1451 if (item->prevSibling)
1452 item->prevSibling->nextSibling = item->nextSibling;
1454 if (item->nextSibling)
1455 item->nextSibling->prevSibling = item->prevSibling;
1459 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1461 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1463 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1464 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1466 if (wineItem->firstChild)
1467 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1469 TREEVIEW_UnlinkItem(wineItem);
1471 infoPtr->uNumItems--;
1473 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1474 Free(wineItem->pszText);
1476 TREEVIEW_FreeItem(infoPtr, wineItem);
1480 /* Empty out the tree. */
1482 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1484 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1486 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1490 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1492 TREEVIEW_ITEM *newSelection = NULL;
1493 TREEVIEW_ITEM *newFirstVisible = NULL;
1494 TREEVIEW_ITEM *parent, *prev = NULL;
1495 BOOL visible = FALSE;
1497 if (wineItem == TVI_ROOT)
1499 TRACE("TVI_ROOT\n");
1500 parent = infoPtr->root;
1501 newSelection = NULL;
1503 TREEVIEW_RemoveTree(infoPtr);
1507 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1510 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1511 parent = wineItem->parent;
1513 if (ISVISIBLE(wineItem))
1515 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1519 if (infoPtr->selectedItem != NULL
1520 && (wineItem == infoPtr->selectedItem
1521 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1523 if (wineItem->nextSibling)
1524 newSelection = wineItem->nextSibling;
1525 else if (wineItem->parent != infoPtr->root)
1526 newSelection = wineItem->parent;
1528 newSelection = wineItem->prevSibling;
1529 TRACE("newSelection = %p\n", newSelection);
1532 if (infoPtr->firstVisible == wineItem)
1534 if (wineItem->nextSibling)
1535 newFirstVisible = wineItem->nextSibling;
1536 else if (wineItem->prevSibling)
1537 newFirstVisible = wineItem->prevSibling;
1538 else if (wineItem->parent != infoPtr->root)
1539 newFirstVisible = wineItem->parent;
1540 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1543 newFirstVisible = infoPtr->firstVisible;
1545 TREEVIEW_RemoveItem(infoPtr, wineItem);
1548 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1549 if (!infoPtr->selectedItem && newSelection)
1551 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1552 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1555 /* Validate insertMark dropItem.
1556 * hotItem ??? - used for comparison only.
1558 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1559 infoPtr->insertMarkItem = 0;
1561 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1562 infoPtr->dropItem = 0;
1564 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1565 newFirstVisible = infoPtr->root->firstChild;
1567 TREEVIEW_VerifyTree(infoPtr);
1569 if (!infoPtr->bRedraw) return TRUE;
1573 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1574 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1575 TREEVIEW_UpdateScrollBars(infoPtr);
1576 TREEVIEW_Invalidate(infoPtr, NULL);
1578 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1580 /* parent lost '+/-' - update it */
1581 TREEVIEW_Invalidate(infoPtr, parent);
1588 /* Get/Set Messages *********************************************************/
1590 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1592 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1594 if (infoPtr->bRedraw)
1596 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1597 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1598 TREEVIEW_UpdateScrollBars(infoPtr);
1599 TREEVIEW_Invalidate(infoPtr, NULL);
1605 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1608 return infoPtr->uIndent;
1612 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1616 if (newIndent < MINIMUM_INDENT)
1617 newIndent = MINIMUM_INDENT;
1619 if (infoPtr->uIndent != newIndent)
1621 infoPtr->uIndent = newIndent;
1622 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1623 TREEVIEW_UpdateScrollBars(infoPtr);
1624 TREEVIEW_Invalidate(infoPtr, NULL);
1632 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1635 return (LRESULT)infoPtr->hwndToolTip;
1639 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1644 prevToolTip = infoPtr->hwndToolTip;
1645 infoPtr->hwndToolTip = hwndTT;
1647 return (LRESULT)prevToolTip;
1651 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1653 BOOL rc = infoPtr->bNtfUnicode;
1654 infoPtr->bNtfUnicode = fUnicode;
1659 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1661 return infoPtr->bNtfUnicode;
1665 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1667 return infoPtr->uScrollTime;
1671 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1673 UINT uOldScrollTime = infoPtr->uScrollTime;
1675 infoPtr->uScrollTime = min(uScrollTime, 100);
1677 return uOldScrollTime;
1682 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1688 case (WPARAM)TVSIL_NORMAL:
1689 return (LRESULT)infoPtr->himlNormal;
1691 case (WPARAM)TVSIL_STATE:
1692 return (LRESULT)infoPtr->himlState;
1699 #define TVHEIGHT_MIN 16
1700 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1702 /* Compute the natural height for items. */
1704 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1708 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1711 /* Height is the maximum of:
1712 * 16 (a hack because our fonts are tiny), and
1713 * The text height + border & margin, and
1714 * The size of the normal image list
1716 GetTextMetricsW(hdc, &tm);
1717 SelectObject(hdc, hOldFont);
1720 height = TVHEIGHT_MIN;
1721 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1722 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1723 if (height < infoPtr->normalImageHeight)
1724 height = infoPtr->normalImageHeight;
1726 /* Round down, unless we support odd ("non even") heights. */
1727 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1734 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1736 HIMAGELIST himlOld = 0;
1737 int oldWidth = infoPtr->normalImageWidth;
1738 int oldHeight = infoPtr->normalImageHeight;
1741 TRACE("%lx,%p\n", wParam, himlNew);
1745 case (WPARAM)TVSIL_NORMAL:
1746 himlOld = infoPtr->himlNormal;
1747 infoPtr->himlNormal = himlNew;
1749 if (himlNew != NULL)
1750 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1751 &infoPtr->normalImageHeight);
1754 infoPtr->normalImageWidth = 0;
1755 infoPtr->normalImageHeight = 0;
1760 case (WPARAM)TVSIL_STATE:
1761 himlOld = infoPtr->himlState;
1762 infoPtr->himlState = himlNew;
1764 if (himlNew != NULL)
1765 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1766 &infoPtr->stateImageHeight);
1769 infoPtr->stateImageWidth = 0;
1770 infoPtr->stateImageHeight = 0;
1776 if (oldWidth != infoPtr->normalImageWidth ||
1777 oldHeight != infoPtr->normalImageHeight)
1779 BOOL bRecalcVisible = FALSE;
1781 if (oldHeight != infoPtr->normalImageHeight &&
1782 !infoPtr->bHeightSet)
1784 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1785 bRecalcVisible = TRUE;
1788 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1789 infoPtr->normalImageWidth != infoPtr->uIndent)
1791 infoPtr->uIndent = infoPtr->normalImageWidth;
1792 bRecalcVisible = TRUE;
1796 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1798 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1799 TREEVIEW_UpdateScrollBars(infoPtr);
1802 TREEVIEW_Invalidate(infoPtr, NULL);
1804 return (LRESULT)himlOld;
1808 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1810 INT prevHeight = infoPtr->uItemHeight;
1812 TRACE("%d\n", newHeight);
1813 if (newHeight == -1)
1815 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1816 infoPtr->bHeightSet = FALSE;
1820 infoPtr->uItemHeight = newHeight;
1821 infoPtr->bHeightSet = TRUE;
1824 /* Round down, unless we support odd ("non even") heights. */
1825 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1826 infoPtr->uItemHeight &= ~1;
1828 if (infoPtr->uItemHeight != prevHeight)
1830 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1831 TREEVIEW_UpdateScrollBars(infoPtr);
1832 TREEVIEW_Invalidate(infoPtr, NULL);
1839 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1842 return infoPtr->uItemHeight;
1847 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1849 TRACE("%p\n", infoPtr->hFont);
1850 return (LRESULT)infoPtr->hFont;
1855 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1859 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1865 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1867 UINT uHeight = infoPtr->uItemHeight;
1869 TRACE("%p %i\n", hFont, bRedraw);
1871 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1873 DeleteObject(infoPtr->hBoldFont);
1874 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1875 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1877 if (!infoPtr->bHeightSet)
1878 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1880 if (uHeight != infoPtr->uItemHeight)
1881 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1883 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1885 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1886 TREEVIEW_UpdateScrollBars(infoPtr);
1889 TREEVIEW_Invalidate(infoPtr, NULL);
1896 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1899 return (LRESULT)infoPtr->clrLine;
1903 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1905 COLORREF prevColor = infoPtr->clrLine;
1908 infoPtr->clrLine = color;
1909 return (LRESULT)prevColor;
1914 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1917 return (LRESULT)infoPtr->clrText;
1921 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1923 COLORREF prevColor = infoPtr->clrText;
1926 infoPtr->clrText = color;
1928 if (infoPtr->clrText != prevColor)
1929 TREEVIEW_Invalidate(infoPtr, NULL);
1931 return (LRESULT)prevColor;
1936 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1939 return (LRESULT)infoPtr->clrBk;
1943 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1945 COLORREF prevColor = infoPtr->clrBk;
1948 infoPtr->clrBk = newColor;
1950 if (newColor != prevColor)
1951 TREEVIEW_Invalidate(infoPtr, NULL);
1953 return (LRESULT)prevColor;
1958 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1961 return (LRESULT)infoPtr->clrInsertMark;
1965 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1967 COLORREF prevColor = infoPtr->clrInsertMark;
1969 TRACE("%x\n", color);
1970 infoPtr->clrInsertMark = color;
1972 return (LRESULT)prevColor;
1977 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1979 TRACE("%d %p\n", wParam, item);
1981 if (!TREEVIEW_ValidItem(infoPtr, item))
1984 infoPtr->insertBeforeorAfter = wParam;
1985 infoPtr->insertMarkItem = item;
1987 TREEVIEW_Invalidate(infoPtr, NULL);
1993 /************************************************************************
1994 * Some serious braindamage here. lParam is a pointer to both the
1995 * input HTREEITEM and the output RECT.
1998 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2000 TREEVIEW_ITEM *wineItem;
2001 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2005 * validate parameters
2011 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2015 * If wParam is TRUE return the text size otherwise return
2016 * the whole item size
2020 /* Windows does not send TVN_GETDISPINFO here. */
2022 lpRect->top = wineItem->rect.top;
2023 lpRect->bottom = wineItem->rect.bottom;
2025 lpRect->left = wineItem->textOffset;
2026 if (!wineItem->textWidth)
2027 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2029 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2033 *lpRect = wineItem->rect;
2036 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2041 static inline LRESULT
2042 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2044 /* Surprise! This does not take integral height into account. */
2045 return infoPtr->clientHeight / infoPtr->uItemHeight;
2050 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2052 TREEVIEW_ITEM *wineItem;
2054 wineItem = tvItem->hItem;
2055 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2058 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2060 if (tvItem->mask & TVIF_CHILDREN)
2062 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2063 FIXME("I_CHILDRENCALLBACK not supported\n");
2064 tvItem->cChildren = wineItem->cChildren;
2067 if (tvItem->mask & TVIF_HANDLE)
2068 tvItem->hItem = wineItem;
2070 if (tvItem->mask & TVIF_IMAGE)
2071 tvItem->iImage = wineItem->iImage;
2073 if (tvItem->mask & TVIF_INTEGRAL)
2074 tvItem->iIntegral = wineItem->iIntegral;
2076 /* undocumented: windows ignores TVIF_PARAM and
2077 * * always sets lParam
2079 tvItem->lParam = wineItem->lParam;
2081 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2082 tvItem->iSelectedImage = wineItem->iSelectedImage;
2084 if (tvItem->mask & TVIF_STATE)
2085 /* Careful here - Windows ignores the stateMask when you get the state
2086 That contradicts the documentation, but makes more common sense, masking
2087 retrieval in this way seems overkill */
2088 tvItem->state = wineItem->state;
2090 if (tvItem->mask & TVIF_TEXT)
2092 if (wineItem->pszText == NULL)
2094 if (tvItem->cchTextMax > 0)
2095 tvItem->pszText[0] = '\0';
2099 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2101 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2102 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2106 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2111 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2113 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2114 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2118 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2119 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2123 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2124 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2129 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2130 * which is wrong. */
2132 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2134 TREEVIEW_ITEM *wineItem;
2135 TREEVIEW_ITEM originalItem;
2137 wineItem = tvItem->hItem;
2139 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2142 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2145 /* store the original item values */
2146 originalItem = *wineItem;
2148 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2151 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2152 if ((tvItem->mask & TVIF_TEXT
2153 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2154 && ISVISIBLE(wineItem))
2156 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2157 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2160 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2162 /* The refresh updates everything, but we can't wait until then. */
2163 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2165 /* if any of the item's values changed and it's not a callback, redraw the item */
2166 if (item_changed(&originalItem, wineItem, tvItem))
2168 if (tvItem->mask & TVIF_INTEGRAL)
2170 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2171 TREEVIEW_UpdateScrollBars(infoPtr);
2173 TREEVIEW_Invalidate(infoPtr, NULL);
2177 TREEVIEW_UpdateScrollBars(infoPtr);
2178 TREEVIEW_Invalidate(infoPtr, wineItem);
2187 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2191 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2194 return (wineItem->state & mask);
2198 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2200 TREEVIEW_ITEM *retval;
2204 /* handle all the global data here */
2207 case TVGN_CHILD: /* Special case: child of 0 is root */
2212 retval = infoPtr->root->firstChild;
2216 retval = infoPtr->selectedItem;
2219 case TVGN_FIRSTVISIBLE:
2220 retval = infoPtr->firstVisible;
2223 case TVGN_DROPHILITE:
2224 retval = infoPtr->dropItem;
2227 case TVGN_LASTVISIBLE:
2228 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2234 TRACE("flags:%x, returns %p\n", which, retval);
2235 return (LRESULT)retval;
2238 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2240 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2246 retval = wineItem->nextSibling;
2249 retval = wineItem->prevSibling;
2252 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2255 retval = wineItem->firstChild;
2257 case TVGN_NEXTVISIBLE:
2258 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2260 case TVGN_PREVIOUSVISIBLE:
2261 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2264 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2268 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2269 return (LRESULT)retval;
2274 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2276 TRACE(" %d\n", infoPtr->uNumItems);
2277 return (LRESULT)infoPtr->uNumItems;
2281 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2283 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2285 static const unsigned int state_table[] = { 0, 2, 1 };
2289 state = STATEIMAGEINDEX(item->state);
2290 TRACE("state:%x\n", state);
2291 item->state &= ~TVIS_STATEIMAGEMASK;
2294 state = state_table[state];
2296 item->state |= INDEXTOSTATEIMAGEMASK(state);
2298 TRACE("state:%x\n", state);
2299 TREEVIEW_Invalidate(infoPtr, item);
2304 /* Painting *************************************************************/
2306 /* Draw the lines and expand button for an item. Also draws one section
2307 * of the line from item's parent to item's parent's next sibling. */
2309 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2311 LONG centerx, centery;
2312 BOOL lar = ((infoPtr->dwStyle
2313 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2316 COLORREF clrBk = infoPtr->clrBk == -1 ? comctl32_color.clrWindow:
2319 if (!lar && item->iLevel == 0)
2322 hbr = CreateSolidBrush(clrBk);
2323 hbrOld = SelectObject(hdc, hbr);
2325 centerx = (item->linesOffset + item->stateOffset) / 2;
2326 centery = (item->rect.top + item->rect.bottom) / 2;
2328 if (infoPtr->dwStyle & TVS_HASLINES)
2330 HPEN hOldPen, hNewPen;
2334 /* Get a dotted grey pen */
2335 lb.lbStyle = BS_SOLID;
2336 lb.lbColor = infoPtr->clrLine;
2337 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2338 hOldPen = SelectObject(hdc, hNewPen);
2340 /* Make sure the center is on a dot (using +2 instead
2341 * of +1 gives us pixel-by-pixel compat with native) */
2342 centery = (centery + 2) & ~1;
2344 MoveToEx(hdc, item->stateOffset, centery, NULL);
2345 LineTo(hdc, centerx - 1, centery);
2347 if (item->prevSibling || item->parent != infoPtr->root)
2349 MoveToEx(hdc, centerx, item->rect.top, NULL);
2350 LineTo(hdc, centerx, centery);
2353 if (item->nextSibling)
2355 MoveToEx(hdc, centerx, centery, NULL);
2356 LineTo(hdc, centerx, item->rect.bottom + 1);
2359 /* Draw the line from our parent to its next sibling. */
2360 parent = item->parent;
2361 while (parent != infoPtr->root)
2363 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2365 if (parent->nextSibling
2366 /* skip top-levels unless TVS_LINESATROOT */
2367 && parent->stateOffset > parent->linesOffset)
2369 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2370 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2373 parent = parent->parent;
2376 SelectObject(hdc, hOldPen);
2377 DeleteObject(hNewPen);
2381 * Display the (+/-) signs
2384 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2386 if (item->cChildren)
2388 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2391 RECT glyphRect = item->rect;
2392 glyphRect.left = item->linesOffset;
2393 glyphRect.right = item->stateOffset;
2394 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2395 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2400 LONG height = item->rect.bottom - item->rect.top;
2401 LONG width = item->stateOffset - item->linesOffset;
2402 LONG rectsize = min(height, width) / 4;
2403 /* plussize = ceil(rectsize * 3/4) */
2404 LONG plussize = (rectsize + 1) * 3 / 4;
2406 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2407 HPEN hOldPen = SelectObject(hdc, hNewPen);
2409 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2410 centerx + rectsize + 2, centery + rectsize + 2);
2412 SelectObject(hdc, hOldPen);
2413 DeleteObject(hNewPen);
2415 if (height < 18 || width < 18)
2417 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2418 LineTo(hdc, centerx + plussize, centery);
2420 if (!(item->state & TVIS_EXPANDED))
2422 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2423 LineTo(hdc, centerx, centery + plussize);
2428 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2429 centerx + plussize, centery + 2);
2431 if (!(item->state & TVIS_EXPANDED))
2433 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2434 centerx + 2, centery + plussize);
2435 SetPixel(hdc, centerx - 1, centery, clrBk);
2436 SetPixel(hdc, centerx + 1, centery, clrBk);
2442 SelectObject(hdc, hbrOld);
2447 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2451 COLORREF oldTextColor, oldTextBkColor;
2453 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2454 NMTVCUSTOMDRAW nmcdhdr;
2456 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2458 /* - If item is drop target or it is selected and window is in focus -
2459 * use blue background (COLOR_HIGHLIGHT).
2460 * - If item is selected, window is not in focus, but it has style
2461 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2462 * - Otherwise - use background color
2464 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2465 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2466 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2468 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2470 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2471 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2475 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2476 if (infoPtr->clrText == -1)
2477 nmcdhdr.clrText = comctl32_color.clrWindowText;
2479 nmcdhdr.clrText = infoPtr->clrText;
2484 nmcdhdr.clrTextBk = infoPtr->clrBk == -1 ? comctl32_color.clrWindow:
2486 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2487 nmcdhdr.clrText = comctl32_color.clrHighlight;
2488 else if (infoPtr->clrText == -1)
2489 nmcdhdr.clrText = comctl32_color.clrWindowText;
2491 nmcdhdr.clrText = infoPtr->clrText;
2494 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2496 /* The custom draw handler can query the text rectangle,
2498 /* should already be known, set to 0 when changed */
2499 if (!wineItem->textWidth)
2500 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2504 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2506 cditem = TREEVIEW_SendCustomDrawItemNotify
2507 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2508 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2510 if (cditem & CDRF_SKIPDEFAULT)
2512 SelectObject(hdc, hOldFont);
2517 if (cditem & CDRF_NEWFONT)
2518 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2520 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2522 /* Set colors. Custom draw handler can change these so we do this after it. */
2523 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2524 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2526 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2529 * Display the images associated with this item
2534 /* State images are displayed to the left of the Normal image
2535 * image number is in state; zero should be `display no image'.
2537 imageIndex = STATEIMAGEINDEX(wineItem->state);
2539 if (infoPtr->himlState && imageIndex)
2541 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2542 wineItem->stateOffset,
2543 centery - infoPtr->stateImageHeight / 2,
2547 /* Now, draw the normal image; can be either selected or
2548 * non-selected image.
2551 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2553 /* The item is currently selected */
2554 imageIndex = wineItem->iSelectedImage;
2558 /* The item is not selected */
2559 imageIndex = wineItem->iImage;
2562 if (infoPtr->himlNormal)
2564 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2566 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2567 wineItem->imageOffset,
2568 centery - infoPtr->normalImageHeight / 2,
2569 ILD_NORMAL | ovlIdx);
2575 * Display the text associated with this item
2578 /* Don't paint item's text if it's being edited */
2579 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2581 if (wineItem->pszText)
2585 rcText.top = wineItem->rect.top;
2586 rcText.bottom = wineItem->rect.bottom;
2587 rcText.left = wineItem->textOffset;
2588 rcText.right = rcText.left + wineItem->textWidth + 4;
2590 TRACE("drawing text %s at (%s)\n",
2591 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2594 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2595 ETO_CLIPPED | ETO_OPAQUE,
2598 lstrlenW(wineItem->pszText),
2601 /* Draw the box around the selected item */
2602 if ((wineItem == infoPtr->selectedItem) && inFocus)
2604 DrawFocusRect(hdc,&rcText);
2610 /* Draw insertion mark if necessary */
2612 if (infoPtr->insertMarkItem)
2613 TRACE("item:%d,mark:%p\n",
2614 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2615 infoPtr->insertMarkItem);
2617 if (wineItem == infoPtr->insertMarkItem)
2619 HPEN hNewPen, hOldPen;
2623 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2624 hOldPen = SelectObject(hdc, hNewPen);
2626 if (infoPtr->insertBeforeorAfter)
2627 offset = wineItem->rect.bottom - 1;
2629 offset = wineItem->rect.top + 1;
2631 left = wineItem->textOffset - 2;
2632 right = wineItem->textOffset + wineItem->textWidth + 2;
2634 MoveToEx(hdc, left, offset - 3, NULL);
2635 LineTo(hdc, left, offset + 4);
2637 MoveToEx(hdc, left, offset, NULL);
2638 LineTo(hdc, right + 1, offset);
2640 MoveToEx(hdc, right, offset + 3, NULL);
2641 LineTo(hdc, right, offset - 4);
2643 SelectObject(hdc, hOldPen);
2644 DeleteObject(hNewPen);
2647 if (cditem & CDRF_NOTIFYPOSTPAINT)
2649 cditem = TREEVIEW_SendCustomDrawItemNotify
2650 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2651 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2654 /* Restore the hdc state */
2655 SetTextColor(hdc, oldTextColor);
2656 SetBkColor(hdc, oldTextBkColor);
2657 SelectObject(hdc, hOldFont);
2660 /* Computes treeHeight and treeWidth and updates the scroll bars.
2663 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2665 TREEVIEW_ITEM *wineItem;
2666 HWND hwnd = infoPtr->hwnd;
2670 LONG scrollX = infoPtr->scrollX;
2672 infoPtr->treeWidth = 0;
2673 infoPtr->treeHeight = 0;
2675 /* We iterate through all visible items in order to get the tree height
2677 wineItem = infoPtr->root->firstChild;
2679 while (wineItem != NULL)
2681 if (ISVISIBLE(wineItem))
2683 /* actually we draw text at textOffset + 2 */
2684 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2685 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2687 /* This is scroll-adjusted, but we fix this below. */
2688 infoPtr->treeHeight = wineItem->rect.bottom;
2691 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2694 /* Fix the scroll adjusted treeHeight and treeWidth. */
2695 if (infoPtr->root->firstChild)
2696 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2698 infoPtr->treeWidth += infoPtr->scrollX;
2700 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2702 /* Adding one scroll bar may take up enough space that it forces us
2703 * to add the other as well. */
2704 if (infoPtr->treeHeight > infoPtr->clientHeight)
2708 if (infoPtr->treeWidth
2709 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2712 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2715 if (!vert && horz && infoPtr->treeHeight
2716 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2719 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2721 si.cbSize = sizeof(SCROLLINFO);
2722 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2727 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2728 if ( si.nPage && NULL != infoPtr->firstVisible)
2730 si.nPos = infoPtr->firstVisible->visibleOrder;
2731 si.nMax = infoPtr->maxVisibleOrder - 1;
2733 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2735 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2736 ShowScrollBar(hwnd, SB_VERT, TRUE);
2737 infoPtr->uInternalStatus |= TV_VSCROLL;
2741 if (infoPtr->uInternalStatus & TV_VSCROLL)
2742 ShowScrollBar(hwnd, SB_VERT, FALSE);
2743 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2748 if (infoPtr->uInternalStatus & TV_VSCROLL)
2749 ShowScrollBar(hwnd, SB_VERT, FALSE);
2750 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2755 si.nPage = infoPtr->clientWidth;
2756 si.nPos = infoPtr->scrollX;
2757 si.nMax = infoPtr->treeWidth - 1;
2759 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2761 si.nPos = si.nMax - max( si.nPage-1, 0 );
2765 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2766 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2767 infoPtr->uInternalStatus |= TV_HSCROLL;
2769 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2770 TREEVIEW_HScroll(infoPtr,
2771 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2775 if (infoPtr->uInternalStatus & TV_HSCROLL)
2776 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2777 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2780 if (infoPtr->scrollX != 0)
2782 TREEVIEW_HScroll(infoPtr,
2783 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2788 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2792 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2795 COLORREF clrBk = infoPtr->clrBk == -1 ? comctl32_color.clrWindow:
2797 hBrush = CreateSolidBrush(clrBk);
2798 FillRect(hdc, rc, hBrush);
2799 DeleteObject(hBrush);
2802 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2804 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2808 TRACE("%p\n", infoPtr);
2810 GetClientRect(infoPtr->hwnd, &rect);
2811 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2817 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2819 HWND hwnd = infoPtr->hwnd;
2821 TREEVIEW_ITEM *wineItem;
2823 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2825 TRACE("empty window\n");
2829 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2832 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2834 ReleaseDC(hwnd, hdc);
2838 for (wineItem = infoPtr->root->firstChild;
2840 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2842 if (ISVISIBLE(wineItem))
2844 /* Avoid unneeded calculations */
2845 if (wineItem->rect.top > rect.bottom)
2847 if (wineItem->rect.bottom < rect.top)
2850 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2854 TREEVIEW_UpdateScrollBars(infoPtr);
2856 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2858 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2862 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2865 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2867 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2871 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2882 GetClientRect(infoPtr->hwnd, &rc);
2886 hdc = BeginPaint(infoPtr->hwnd, &ps);
2889 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2892 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2893 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2896 EndPaint(infoPtr->hwnd, &ps);
2902 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2904 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2906 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2909 if (options & PRF_ERASEBKGND)
2910 TREEVIEW_EraseBackground(infoPtr, hdc);
2912 if (options & PRF_CLIENT)
2915 GetClientRect(infoPtr->hwnd, &rc);
2916 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2922 /* Sorting **************************************************************/
2924 /***************************************************************************
2925 * Forward the DPA local callback to the treeview owner callback
2928 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2929 const TVSORTCB *pCallBackSort)
2931 /* Forward the call to the client-defined callback */
2932 return pCallBackSort->lpfnCompare(first->lParam,
2934 pCallBackSort->lParam);
2937 /***************************************************************************
2938 * Treeview native sort routine: sort on item text.
2941 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2942 const TREEVIEW_INFO *infoPtr)
2944 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2945 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2947 if(first->pszText && second->pszText)
2948 return lstrcmpiW(first->pszText, second->pszText);
2949 else if(first->pszText)
2951 else if(second->pszText)
2957 /* Returns the number of physical children belonging to item. */
2959 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
2964 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2970 /* Returns a DPA containing a pointer to each physical child of item in
2971 * sibling order. If item has no children, an empty DPA is returned. */
2973 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
2975 HTREEITEM child = item->firstChild;
2977 HDPA list = DPA_Create(8);
2978 if (list == 0) return NULL;
2980 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2982 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2992 /***************************************************************************
2993 * Setup the treeview structure with regards of the sort method
2994 * and sort the children of the TV item specified in lParam
2995 * fRecurse: currently unused. Should be zero.
2996 * parent: if pSort!=NULL, should equal pSort->hParent.
2997 * otherwise, item which child items are to be sorted.
2998 * pSort: sort method info. if NULL, sort on item text.
2999 * if non-NULL, sort on item's lParam content, and let the
3000 * application decide what that means. See also TVM_SORTCHILDRENCB.
3004 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3008 PFNDPACOMPARE pfnCompare;
3011 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3012 if (parent == TVI_ROOT || parent == NULL)
3013 parent = infoPtr->root;
3015 /* Check for a valid handle to the parent item */
3016 if (!TREEVIEW_ValidItem(infoPtr, parent))
3018 ERR("invalid item hParent=%p\n", parent);
3024 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3025 lpCompare = (LPARAM)pSort;
3029 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3030 lpCompare = (LPARAM)infoPtr;
3033 cChildren = TREEVIEW_CountChildren(parent);
3035 /* Make sure there is something to sort */
3038 /* TREEVIEW_ITEM rechaining */
3041 HTREEITEM nextItem = 0;
3042 HTREEITEM prevItem = 0;
3044 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3046 if (sortList == NULL)
3049 /* let DPA sort the list */
3050 DPA_Sort(sortList, pfnCompare, lpCompare);
3052 /* The order of DPA entries has been changed, so fixup the
3053 * nextSibling and prevSibling pointers. */
3055 item = DPA_GetPtr(sortList, count++);
3056 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3058 /* link the two current item together */
3059 item->nextSibling = nextItem;
3060 nextItem->prevSibling = item;
3062 if (prevItem == NULL)
3064 /* this is the first item, update the parent */
3065 parent->firstChild = item;
3066 item->prevSibling = NULL;
3070 /* fix the back chaining */
3071 item->prevSibling = prevItem;
3074 /* get ready for the next one */
3079 /* the last item is pointed to by item and never has a sibling */
3080 item->nextSibling = NULL;
3081 parent->lastChild = item;
3083 DPA_Destroy(sortList);
3085 TREEVIEW_VerifyTree(infoPtr);
3087 if (parent->state & TVIS_EXPANDED)
3089 int visOrder = infoPtr->firstVisible->visibleOrder;
3091 if (parent == infoPtr->root)
3092 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3094 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3096 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3098 TREEVIEW_ITEM *item;
3100 for (item = infoPtr->root->firstChild; item != NULL;
3101 item = TREEVIEW_GetNextListItem(infoPtr, item))
3103 if (item->visibleOrder == visOrder)
3107 if (!item) item = parent->firstChild;
3108 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3111 TREEVIEW_Invalidate(infoPtr, NULL);
3120 /***************************************************************************
3121 * Setup the treeview structure with regards of the sort method
3122 * and sort the children of the TV item specified in lParam
3125 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3127 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3131 /***************************************************************************
3132 * Sort the children of the TV item specified in lParam.
3135 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3137 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3141 /* Expansion/Collapse ***************************************************/
3144 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3147 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3148 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3149 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3154 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3157 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3158 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3159 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3164 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3165 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3167 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3168 BOOL bRemoveChildren, BOOL bUser)
3170 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3171 BOOL bSetSelection, bSetFirstVisible;
3173 LONG scrollDist = 0;
3174 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3176 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3178 if (!(wineItem->state & TVIS_EXPANDED))
3181 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3182 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3184 if (wineItem->firstChild == NULL)
3187 wineItem->state &= ~TVIS_EXPANDED;
3189 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3190 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3192 bSetSelection = (infoPtr->selectedItem != NULL
3193 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3195 bSetFirstVisible = (infoPtr->firstVisible != NULL
3196 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3201 if (tmpItem->nextSibling)
3203 nextItem = tmpItem->nextSibling;
3206 tmpItem = tmpItem->parent;
3210 scrollDist = nextItem->rect.top;
3212 if (bRemoveChildren)
3214 INT old_cChildren = wineItem->cChildren;
3215 TRACE("TVE_COLLAPSERESET\n");
3216 wineItem->state &= ~TVIS_EXPANDEDONCE;
3217 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3218 wineItem->cChildren = old_cChildren;
3221 if (wineItem->firstChild)
3223 TREEVIEW_ITEM *item, *sibling;
3225 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3227 for (item = wineItem->firstChild; item != sibling;
3228 item = TREEVIEW_GetNextListItem(infoPtr, item))
3230 item->visibleOrder = -1;
3234 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3237 scrollDist = -(scrollDist - nextItem->rect.top);
3241 /* Don't call DoSelectItem, it sends notifications. */
3242 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3243 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3244 wineItem->state |= TVIS_SELECTED;
3245 infoPtr->selectedItem = wineItem;
3248 TREEVIEW_UpdateScrollBars(infoPtr);
3250 scrollRect.left = 0;
3251 scrollRect.right = infoPtr->clientWidth;
3252 scrollRect.bottom = infoPtr->clientHeight;
3256 scrollRect.top = nextItem->rect.top;
3258 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3259 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3260 TREEVIEW_Invalidate(infoPtr, wineItem);
3262 scrollRect.top = wineItem->rect.top;
3263 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3266 TREEVIEW_SetFirstVisible(infoPtr,
3267 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3274 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3275 BOOL bExpandPartial, BOOL bUser)
3278 LONG orgNextTop = 0;
3280 TREEVIEW_ITEM *nextItem, *tmpItem;
3284 if (wineItem->state & TVIS_EXPANDED)
3287 tmpItem = wineItem; nextItem = NULL;
3290 if (tmpItem->nextSibling)
3292 nextItem = tmpItem->nextSibling;
3295 tmpItem = tmpItem->parent;
3299 orgNextTop = nextItem->rect.top;
3301 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3303 if (bUser || ((wineItem->cChildren != 0) &&
3304 !(wineItem->state & TVIS_EXPANDEDONCE)))
3306 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3308 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3312 if (!wineItem->firstChild)
3315 wineItem->state |= TVIS_EXPANDED;
3316 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3317 wineItem->state |= TVIS_EXPANDEDONCE;
3321 if (!wineItem->firstChild)
3324 /* this item has already been expanded */
3325 wineItem->state |= TVIS_EXPANDED;
3329 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3331 if (ISVISIBLE(wineItem))
3333 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3334 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3335 TREEVIEW_UpdateScrollBars(infoPtr);
3337 scrollRect.left = 0;
3338 scrollRect.bottom = infoPtr->treeHeight;
3339 scrollRect.right = infoPtr->clientWidth;
3342 scrollDist = nextItem->rect.top - orgNextTop;
3343 scrollRect.top = orgNextTop;
3345 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3346 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3347 TREEVIEW_Invalidate (infoPtr, wineItem);
3349 scrollRect.top = wineItem->rect.top;
3350 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3353 /* Scroll up so that as many children as possible are visible.
3354 * This fails when expanding causes an HScroll bar to appear, but we
3355 * don't know that yet, so the last item is obscured. */
3356 if (wineItem->firstChild != NULL)
3358 int nChildren = wineItem->lastChild->visibleOrder
3359 - wineItem->firstChild->visibleOrder + 1;
3361 int visible_pos = wineItem->visibleOrder
3362 - infoPtr->firstVisible->visibleOrder;
3364 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3366 if (visible_pos > 0 && nChildren > rows_below)
3368 int scroll = nChildren - rows_below;
3370 if (scroll > visible_pos)
3371 scroll = visible_pos;
3375 TREEVIEW_ITEM *newFirstVisible
3376 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3380 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3390 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3394 if (wineItem->state & TVIS_EXPANDED)
3395 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3397 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3401 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3403 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3405 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3407 if (TREEVIEW_HasChildren(infoPtr, item))
3408 TREEVIEW_ExpandAll(infoPtr, item);
3412 /* Note:If the specified item is the child of a collapsed parent item,
3413 the parent's list of child items is (recursively) expanded to reveal the
3414 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3415 know if it also applies here.
3419 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3421 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3424 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3425 TREEVIEW_ItemName(wineItem), flag,
3426 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3428 switch (flag & TVE_TOGGLE)
3431 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3435 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3439 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3446 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3450 /* Hit-Testing **********************************************************/
3452 static TREEVIEW_ITEM *
3453 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3455 TREEVIEW_ITEM *wineItem;
3458 if (!infoPtr->firstVisible)
3461 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3463 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3464 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3466 if (row >= wineItem->visibleOrder
3467 && row < wineItem->visibleOrder + wineItem->iIntegral)
3475 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3477 TREEVIEW_ITEM *wineItem;
3483 GetClientRect(infoPtr->hwnd, &rect);
3490 status |= TVHT_TOLEFT;
3492 else if (x > rect.right)
3494 status |= TVHT_TORIGHT;
3499 status |= TVHT_ABOVE;
3501 else if (y > rect.bottom)
3503 status |= TVHT_BELOW;
3508 lpht->flags = status;
3512 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3515 lpht->flags = TVHT_NOWHERE;
3519 if (x >= wineItem->textOffset + wineItem->textWidth)
3521 lpht->flags = TVHT_ONITEMRIGHT;
3523 else if (x >= wineItem->textOffset)
3525 lpht->flags = TVHT_ONITEMLABEL;
3527 else if (x >= wineItem->imageOffset)
3529 lpht->flags = TVHT_ONITEMICON;
3531 else if (x >= wineItem->stateOffset)
3533 lpht->flags = TVHT_ONITEMSTATEICON;
3535 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3537 lpht->flags = TVHT_ONITEMBUTTON;
3541 lpht->flags = TVHT_ONITEMINDENT;
3544 lpht->hItem = wineItem;
3545 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3547 return (LRESULT)wineItem;
3550 /* Item Label Editing ***************************************************/
3553 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3555 return (LRESULT)infoPtr->hwndEdit;
3558 static LRESULT CALLBACK
3559 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3561 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3562 BOOL bCancel = FALSE;
3568 TRACE("WM_PAINT start\n");
3569 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3571 TRACE("WM_PAINT done\n");
3575 if (infoPtr->bIgnoreEditKillFocus)
3581 WNDPROC editProc = infoPtr->wpEditOrig;
3582 infoPtr->wpEditOrig = 0;
3583 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3584 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3588 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3591 if (wParam == (WPARAM)VK_ESCAPE)
3596 else if (wParam == (WPARAM)VK_RETURN)
3603 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3606 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3607 /* eg. Using a messagebox */
3609 infoPtr->bIgnoreEditKillFocus = TRUE;
3610 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3611 infoPtr->bIgnoreEditKillFocus = FALSE;
3617 /* should handle edit control messages here */
3620 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3622 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3624 switch (HIWORD(wParam))
3629 * Adjust the edit window size
3632 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3633 HDC hdc = GetDC(infoPtr->hwndEdit);
3635 HFONT hFont, hOldFont = 0;
3637 TRACE("edit=%p\n", infoPtr->hwndEdit);
3639 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3641 infoPtr->bLabelChanged = TRUE;
3643 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3645 /* Select font to get the right dimension of the string */
3646 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3650 hOldFont = SelectObject(hdc, hFont);
3653 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3655 TEXTMETRICW textMetric;
3657 /* Add Extra spacing for the next character */
3658 GetTextMetricsW(hdc, &textMetric);
3659 sz.cx += (textMetric.tmMaxCharWidth * 2);
3661 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3663 infoPtr->clientWidth - editItem->textOffset + 2);
3665 SetWindowPos(infoPtr->hwndEdit,
3670 editItem->rect.bottom - editItem->rect.top + 3,
3671 SWP_NOMOVE | SWP_DRAWFRAME);
3676 SelectObject(hdc, hOldFont);
3679 ReleaseDC(infoPtr->hwnd, hdc);
3683 /* apparently we should respect passed handle value */
3684 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3686 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3690 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3697 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3699 HWND hwnd = infoPtr->hwnd;
3702 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3705 TEXTMETRICW textMetric;
3707 TRACE("%p %p\n", hwnd, hItem);
3708 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3711 if (infoPtr->hwndEdit)
3712 return infoPtr->hwndEdit;
3714 infoPtr->bLabelChanged = FALSE;
3716 /* make edit item visible */
3717 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3719 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3722 /* Select the font to get appropriate metric dimensions */
3723 if (infoPtr->hFont != 0)
3725 hOldFont = SelectObject(hdc, infoPtr->hFont);
3728 /* Get string length in pixels */
3730 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3733 GetTextExtentPoint32A(hdc, "", 0, &sz);
3735 /* Add Extra spacing for the next character */
3736 GetTextMetricsW(hdc, &textMetric);
3737 sz.cx += (textMetric.tmMaxCharWidth * 2);
3739 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3740 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3742 if (infoPtr->hFont != 0)
3744 SelectObject(hdc, hOldFont);
3747 ReleaseDC(hwnd, hdc);
3749 infoPtr->editItem = hItem;
3751 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3754 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3755 WS_CLIPSIBLINGS | ES_WANTRETURN |
3756 ES_LEFT, hItem->textOffset - 2,
3757 hItem->rect.top - 1, sz.cx + 3,
3758 hItem->rect.bottom -
3759 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3760 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3762 infoPtr->hwndEdit = hwndEdit;
3764 /* Get a 2D border. */
3765 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3766 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3767 SetWindowLongW(hwndEdit, GWL_STYLE,
3768 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3770 SendMessageW(hwndEdit, WM_SETFONT,
3771 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3773 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3775 TREEVIEW_Edit_SubclassProc);
3777 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3779 DestroyWindow(hwndEdit);
3780 infoPtr->hwndEdit = 0;
3781 infoPtr->editItem = NULL;
3786 SetWindowTextW(hwndEdit, hItem->pszText);
3789 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3790 ShowWindow(hwndEdit, SW_SHOW);
3797 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3799 HWND hwnd = infoPtr->hwnd;
3800 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3803 WCHAR tmpText[1024] = { '\0' };
3804 WCHAR *newText = tmpText;
3807 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3809 tvdi.hdr.hwndFrom = hwnd;
3810 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3811 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3813 tvdi.item.hItem = editedItem;
3814 tvdi.item.state = editedItem->state;
3815 tvdi.item.lParam = editedItem->lParam;
3819 if (!infoPtr->bNtfUnicode)
3820 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3822 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3824 if (iLength >= 1023)
3826 ERR("Insufficient space to retrieve new item label\n");
3829 tvdi.item.mask = TVIF_TEXT;
3830 tvdi.item.pszText = tmpText;
3831 tvdi.item.cchTextMax = iLength + 1;
3835 tvdi.item.pszText = NULL;
3836 tvdi.item.cchTextMax = 0;
3839 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3841 if (!bCancel && bCommit) /* Apply the changes */
3843 if (!infoPtr->bNtfUnicode)
3845 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3846 newText = Alloc(len * sizeof(WCHAR));
3847 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3851 if (strcmpW(newText, editedItem->pszText) != 0)
3853 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3856 ERR("OutOfMemory, cannot allocate space for label\n");
3857 if(newText != tmpText) Free(newText);
3858 DestroyWindow(infoPtr->hwndEdit);
3859 infoPtr->hwndEdit = 0;
3860 infoPtr->editItem = NULL;
3865 editedItem->pszText = ptr;
3866 editedItem->cchTextMax = iLength + 1;
3867 strcpyW(editedItem->pszText, newText);
3868 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3871 if(newText != tmpText) Free(newText);
3874 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3875 DestroyWindow(infoPtr->hwndEdit);
3876 infoPtr->hwndEdit = 0;
3877 infoPtr->editItem = NULL;
3882 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3884 if (wParam != TV_EDIT_TIMER)
3886 ERR("got unknown timer\n");
3890 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3891 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3893 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3899 /* Mouse Tracking/Drag **************************************************/
3901 /***************************************************************************
3902 * This is quite unusual piece of code, but that's how it's implemented in
3906 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3908 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3909 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3913 r.top = pt.y - cyDrag;
3914 r.left = pt.x - cxDrag;
3915 r.bottom = pt.y + cyDrag;
3916 r.right = pt.x + cxDrag;
3918 SetCapture(infoPtr->hwnd);
3922 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3924 if (msg.message == WM_MOUSEMOVE)
3926 pt.x = (short)LOWORD(msg.lParam);
3927 pt.y = (short)HIWORD(msg.lParam);
3928 if (PtInRect(&r, pt))
3936 else if (msg.message >= WM_LBUTTONDOWN &&
3937 msg.message <= WM_RBUTTONDBLCLK)
3939 if (msg.message == WM_RBUTTONUP)
3940 TREEVIEW_RButtonUp(infoPtr, &pt);
3944 DispatchMessageW(&msg);
3947 if (GetCapture() != infoPtr->hwnd)
3957 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3959 TREEVIEW_ITEM *wineItem;
3963 SetFocus(infoPtr->hwnd);
3965 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3967 /* If there is pending 'edit label' event - kill it now */
3968 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3971 hit.pt.x = (short)LOWORD(lParam);
3972 hit.pt.y = (short)HIWORD(lParam);
3974 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3977 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3979 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3983 case TVHT_ONITEMRIGHT:
3984 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3987 case TVHT_ONITEMINDENT:
3988 if (!(infoPtr->dwStyle & TVS_HASLINES))
3994 int level = hit.pt.x / infoPtr->uIndent;
3995 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3997 while (wineItem->iLevel > level)
3999 wineItem = wineItem->parent;
4005 case TVHT_ONITEMLABEL:
4006 case TVHT_ONITEMICON:
4007 case TVHT_ONITEMBUTTON:
4008 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4011 case TVHT_ONITEMSTATEICON:
4012 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4013 TREEVIEW_ToggleItemState(infoPtr, wineItem);
4015 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4024 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4026 HWND hwnd = infoPtr->hwnd;
4028 BOOL bTrack, bDoLabelEdit;
4031 /* If Edit control is active - kill it and return.
4032 * The best way to do it is to set focus to itself.
4033 * Edit control subclassed procedure will automatically call
4036 if (infoPtr->hwndEdit)
4042 ht.pt.x = (short)LOWORD(lParam);
4043 ht.pt.y = (short)HIWORD(lParam);
4045 TREEVIEW_HitTest(infoPtr, &ht);
4046 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4048 /* update focusedItem and redraw both items */
4049 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4051 infoPtr->focusedItem = ht.hItem;
4052 InvalidateRect(hwnd, &ht.hItem->rect, TRUE);
4054 if(infoPtr->selectedItem)
4055 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
4058 bTrack = (ht.flags & TVHT_ONITEM)
4059 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4062 * If the style allows editing and the node is already selected
4063 * and the click occurred on the item label...
4065 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4066 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4068 /* Send NM_CLICK right away */
4070 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4073 if (ht.flags & TVHT_ONITEMBUTTON)
4075 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4079 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4080 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4082 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4083 infoPtr->dropItem = ht.hItem;
4085 /* clean up focusedItem as we dragged and won't select this item */
4086 if(infoPtr->focusedItem)
4088 /* refresh the item that was focused */
4089 tempItem = infoPtr->focusedItem;
4090 infoPtr->focusedItem = 0;
4091 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
4093 /* refresh the selected item to return the filled background */
4094 if (infoPtr->selectedItem)
4095 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
4102 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4107 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4108 KillTimer(hwnd, TV_EDIT_TIMER);
4110 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4111 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4113 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4116 * if we are TVS_SINGLEEXPAND then we want this single click to
4117 * do a bunch of things.
4119 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4120 (infoPtr->hwndEdit == 0))
4122 TREEVIEW_ITEM *SelItem;
4125 * Send the notification
4127 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4130 * Close the previous selection all the way to the root
4131 * as long as the new selection is not a child
4133 if((infoPtr->selectedItem)
4134 && (infoPtr->selectedItem != ht.hItem))
4136 BOOL closeit = TRUE;
4139 /* determine if the hitItem is a child of the currently selected item */
4140 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4142 closeit = (SelItem != infoPtr->selectedItem);
4143 SelItem = SelItem->parent;
4148 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4149 SelItem = infoPtr->selectedItem;
4151 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4153 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4154 SelItem = SelItem->parent;
4160 * Expand the current item
4162 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4165 /* Select the current item */
4166 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4168 else if (ht.flags & TVHT_ONITEMSTATEICON)
4170 /* TVS_CHECKBOXES requires us to toggle the current state */
4171 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4172 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4182 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4186 if (infoPtr->hwndEdit)
4188 SetFocus(infoPtr->hwnd);
4192 ht.pt.x = (short)LOWORD(lParam);
4193 ht.pt.y = (short)HIWORD(lParam);
4195 TREEVIEW_HitTest(infoPtr, &ht);
4197 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4201 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4202 infoPtr->dropItem = ht.hItem;
4207 SetFocus(infoPtr->hwnd);
4208 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4215 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4221 TREEVIEW_HitTest(infoPtr, &ht);
4225 /* Change to screen coordinate for WM_CONTEXTMENU */
4226 ClientToScreen(infoPtr->hwnd, &ht.pt);
4228 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4229 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4230 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4237 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4239 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4243 HBITMAP hbmp, hOldbmp;
4250 if (!(infoPtr->himlNormal))
4253 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4256 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4258 hwtop = GetDesktopWindow();
4259 htopdc = GetDC(hwtop);
4260 hdc = CreateCompatibleDC(htopdc);
4262 hOldFont = SelectObject(hdc, infoPtr->hFont);
4264 if (dragItem->pszText)
4265 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4268 GetTextExtentPoint32A(hdc, "", 0, &size);
4270 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4271 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4272 hOldbmp = SelectObject(hdc, hbmp);
4274 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4279 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4280 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4284 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4285 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4288 /* draw item text */
4290 SetRect(&rc, cx, 0, size.cx, size.cy);
4292 if (dragItem->pszText)
4293 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4296 SelectObject(hdc, hOldFont);
4297 SelectObject(hdc, hOldbmp);
4299 ImageList_Add(infoPtr->dragList, hbmp, 0);
4303 ReleaseDC(hwtop, htopdc);
4305 return (LRESULT)infoPtr->dragList;
4308 /* Selection ************************************************************/
4311 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4314 TREEVIEW_ITEM *prevSelect;
4317 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4319 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4320 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4321 newSelect ? newSelect->state : 0);
4323 /* reset and redraw focusedItem if focusedItem was set so we don't */
4324 /* have to worry about the previously focused item when we set a new one */
4325 if(infoPtr->focusedItem)
4327 rcFocused = (infoPtr->focusedItem)->rect;
4328 infoPtr->focusedItem = 0;
4329 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4335 prevSelect = infoPtr->selectedItem;
4337 if (prevSelect == newSelect) {
4338 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4342 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4345 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4351 prevSelect->state &= ~TVIS_SELECTED;
4353 newSelect->state |= TVIS_SELECTED;
4355 infoPtr->selectedItem = newSelect;
4357 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4360 TREEVIEW_Invalidate(infoPtr, prevSelect);
4362 TREEVIEW_Invalidate(infoPtr, newSelect);
4364 TREEVIEW_SendTreeviewNotify(infoPtr,
4367 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4372 case TVGN_DROPHILITE:
4373 prevSelect = infoPtr->dropItem;
4376 prevSelect->state &= ~TVIS_DROPHILITED;
4378 infoPtr->dropItem = newSelect;
4381 newSelect->state |= TVIS_DROPHILITED;
4383 TREEVIEW_Invalidate(infoPtr, prevSelect);
4384 TREEVIEW_Invalidate(infoPtr, newSelect);
4387 case TVGN_FIRSTVISIBLE:
4388 if (newSelect != NULL)
4390 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4391 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4392 TREEVIEW_Invalidate(infoPtr, NULL);
4397 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4401 /* FIXME: handle NM_KILLFOCUS etc */
4403 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4405 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4408 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4410 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4416 /*************************************************************************
4417 * TREEVIEW_ProcessLetterKeys
4419 * Processes keyboard messages generated by pressing the letter keys
4421 * What this does is perform a case insensitive search from the
4422 * current position with the following quirks:
4423 * - If two chars or more are pressed in quick succession we search
4424 * for the corresponding string (e.g. 'abc').
4425 * - If there is a delay we wipe away the current search string and
4426 * restart with just that char.
4427 * - If the user keeps pressing the same character, whether slowly or
4428 * fast, so that the search string is entirely composed of this
4429 * character ('aaaaa' for instance), then we search for first item
4430 * that starting with that character.
4431 * - If the user types the above character in quick succession, then
4432 * we must also search for the corresponding string ('aaaaa'), and
4433 * go to that string if there is a match.
4441 * - The current implementation has a list of characters it will
4442 * accept and it ignores everything else. In particular it will
4443 * ignore accentuated characters which seems to match what
4444 * Windows does. But I'm not sure it makes sense to follow
4446 * - We don't sound a beep when the search fails.
4447 * - The search should start from the focused item, not from the selected
4448 * item. One reason for this is to allow for multiple selections in trees.
4449 * But currently infoPtr->focusedItem does not seem very usable.
4453 * TREEVIEW_ProcessLetterKeys
4455 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4458 HTREEITEM endidx,idx;
4460 WCHAR buffer[MAX_PATH];
4461 DWORD timestamp,elapsed;
4463 /* simple parameter checking */
4464 if (!charCode || !keyData) return 0;
4466 /* only allow the valid WM_CHARs through */
4467 if (!isalnum(charCode) &&
4468 charCode != '.' && charCode != '`' && charCode != '!' &&
4469 charCode != '@' && charCode != '#' && charCode != '$' &&
4470 charCode != '%' && charCode != '^' && charCode != '&' &&
4471 charCode != '*' && charCode != '(' && charCode != ')' &&
4472 charCode != '-' && charCode != '_' && charCode != '+' &&
4473 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4474 charCode != '}' && charCode != '[' && charCode != '{' &&
4475 charCode != '/' && charCode != '?' && charCode != '>' &&
4476 charCode != '<' && charCode != ',' && charCode != '~')
4479 /* compute how much time elapsed since last keypress */
4480 timestamp = GetTickCount();
4481 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4482 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4484 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4487 /* update the search parameters */
4488 infoPtr->lastKeyPressTimestamp=timestamp;
4489 if (elapsed < KEY_DELAY) {
4490 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4491 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4493 if (infoPtr->charCode != charCode) {
4494 infoPtr->charCode=charCode=0;
4497 infoPtr->charCode=charCode;
4498 infoPtr->szSearchParam[0]=charCode;
4499 infoPtr->nSearchParamLength=1;
4500 /* Redundant with the 1 char string */
4504 /* and search from the current position */
4506 if (infoPtr->selectedItem != NULL) {
4507 endidx=infoPtr->selectedItem;
4508 /* if looking for single character match,
4509 * then we must always move forward
4511 if (infoPtr->nSearchParamLength == 1)
4512 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4517 idx=infoPtr->root->firstChild;
4520 /* At the end point, sort out wrapping */
4523 /* If endidx is null, stop at the last item (ie top to bottom) */
4527 /* Otherwise, start again at the very beginning */
4528 idx=infoPtr->root->firstChild;
4530 /* But if we are stopping on the first child, end now! */
4531 if (idx == endidx) break;
4535 ZeroMemory(&item, sizeof(item));
4536 item.mask = TVIF_TEXT;
4538 item.pszText = buffer;
4539 item.cchTextMax = sizeof(buffer);
4540 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4542 /* check for a match */
4543 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4546 } else if ( (charCode != 0) && (nItem == NULL) &&
4547 (nItem != infoPtr->selectedItem) &&
4548 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4549 /* This would work but we must keep looking for a longer match */
4552 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4553 } while (idx != endidx);
4555 if (nItem != NULL) {
4556 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4557 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4564 /* Scrolling ************************************************************/
4567 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4570 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4571 HTREEITEM newFirstVisible = NULL;
4572 int visible_pos = -1;
4574 if (!TREEVIEW_ValidItem(infoPtr, item))
4577 if (!ISVISIBLE(item))
4579 /* Expand parents as necessary. */
4582 /* see if we are trying to ensure that root is visible */
4583 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4584 parent = item->parent;
4586 parent = item; /* this item is the topmost item */
4588 while (parent != infoPtr->root)
4590 if (!(parent->state & TVIS_EXPANDED))
4591 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4593 parent = parent->parent;
4597 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4599 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4600 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4602 if (hasFirstVisible)
4603 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4605 if (visible_pos < 0)
4607 /* item is before the start of the list: put it at the top. */
4608 newFirstVisible = item;
4610 else if (visible_pos >= viscount
4611 /* Sometimes, before we are displayed, GVC is 0, causing us to
4612 * spuriously scroll up. */
4613 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4615 /* item is past the end of the list. */
4616 int scroll = visible_pos - viscount;
4618 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4624 /* Scroll window so item's text is visible as much as possible */
4625 /* Calculation of amount of extra space is taken from EditLabel code */
4627 TEXTMETRICW textMetric;
4628 HDC hdc = GetWindowDC(infoPtr->hwnd);
4630 x = item->textWidth;
4632 GetTextMetricsW(hdc, &textMetric);
4633 ReleaseDC(infoPtr->hwnd, hdc);
4635 x += (textMetric.tmMaxCharWidth * 2);
4636 x = max(x, textMetric.tmMaxCharWidth * 3);
4638 if (item->textOffset < 0)
4639 pos = item->textOffset;
4640 else if (item->textOffset + x > infoPtr->clientWidth)
4642 if (x > infoPtr->clientWidth)
4643 pos = item->textOffset;
4645 pos = item->textOffset + x - infoPtr->clientWidth;
4650 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4653 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4655 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4664 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4665 TREEVIEW_ITEM *newFirstVisible,
4666 BOOL bUpdateScrollPos)
4670 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4672 if (newFirstVisible != NULL)
4674 /* Prevent an empty gap from appearing at the bottom... */
4675 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4676 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4680 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4683 /* ... unless we just don't have enough items. */
4684 if (newFirstVisible == NULL)
4685 newFirstVisible = infoPtr->root->firstChild;
4689 if (infoPtr->firstVisible != newFirstVisible)
4691 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4693 infoPtr->firstVisible = newFirstVisible;
4694 TREEVIEW_Invalidate(infoPtr, NULL);
4698 TREEVIEW_ITEM *item;
4699 int scroll = infoPtr->uItemHeight *
4700 (infoPtr->firstVisible->visibleOrder
4701 - newFirstVisible->visibleOrder);
4703 infoPtr->firstVisible = newFirstVisible;
4705 for (item = infoPtr->root->firstChild; item != NULL;
4706 item = TREEVIEW_GetNextListItem(infoPtr, item))
4708 item->rect.top += scroll;
4709 item->rect.bottom += scroll;
4712 if (bUpdateScrollPos)
4713 SetScrollPos(infoPtr->hwnd, SB_VERT,
4714 newFirstVisible->visibleOrder, TRUE);
4716 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4721 /************************************************************************
4722 * VScroll is always in units of visible items. i.e. we always have a
4723 * visible item aligned to the top of the control. (Unless we have no
4727 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4729 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4730 TREEVIEW_ITEM *newFirstVisible = NULL;
4732 int nScrollCode = LOWORD(wParam);
4734 TRACE("wp %lx\n", wParam);
4736 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4739 if (!oldFirstVisible)
4741 assert(infoPtr->root->firstChild == NULL);
4745 switch (nScrollCode)
4748 newFirstVisible = infoPtr->root->firstChild;
4752 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4756 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4760 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4764 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4765 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4769 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4770 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4774 case SB_THUMBPOSITION:
4775 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4776 infoPtr->root->firstChild,
4777 (LONG)(SHORT)HIWORD(wParam));
4784 if (newFirstVisible != NULL)
4786 if (newFirstVisible != oldFirstVisible)
4787 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4788 nScrollCode != SB_THUMBTRACK);
4789 else if (nScrollCode == SB_THUMBPOSITION)
4790 SetScrollPos(infoPtr->hwnd, SB_VERT,
4791 newFirstVisible->visibleOrder, TRUE);
4798 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4801 int scrollX = infoPtr->scrollX;
4802 int nScrollCode = LOWORD(wParam);
4804 TRACE("wp %lx\n", wParam);
4806 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4809 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4810 /* shall never occur */
4817 switch (nScrollCode)
4820 scrollX -= infoPtr->uItemHeight;
4823 scrollX += infoPtr->uItemHeight;
4826 scrollX -= infoPtr->clientWidth;
4829 scrollX += infoPtr->clientWidth;
4833 case SB_THUMBPOSITION:
4834 scrollX = (int)(SHORT)HIWORD(wParam);
4841 if (scrollX > maxWidth)
4843 else if (scrollX < 0)
4847 if (scrollX != infoPtr->scrollX)
4849 TREEVIEW_ITEM *item;
4850 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4852 for (item = infoPtr->root->firstChild; item != NULL;
4853 item = TREEVIEW_GetNextListItem(infoPtr, item))
4855 item->linesOffset += scroll_pixels;
4856 item->stateOffset += scroll_pixels;
4857 item->imageOffset += scroll_pixels;
4858 item->textOffset += scroll_pixels;
4861 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4862 infoPtr->scrollX = scrollX;
4863 UpdateWindow(infoPtr->hwnd);
4866 if (nScrollCode != SB_THUMBTRACK)
4867 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4873 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4876 UINT pulScrollLines = 3;
4878 if (wParam & (MK_SHIFT | MK_CONTROL))
4879 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4881 if (infoPtr->firstVisible == NULL)
4884 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4886 gcWheelDelta = -(short)HIWORD(wParam);
4887 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4889 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4891 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4892 int maxDy = infoPtr->maxVisibleOrder;
4900 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4905 /* Create/Destroy *******************************************************/
4908 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4911 HBITMAP hbm, hbmOld;
4915 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4917 hdcScreen = GetDC(0);
4919 hdc = CreateCompatibleDC(hdcScreen);
4920 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4921 hbmOld = SelectObject(hdc, hbm);
4923 SetRect(&rc, 0, 0, 48, 16);
4924 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4926 SetRect(&rc, 18, 2, 30, 14);
4927 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4928 DFCS_BUTTONCHECK|DFCS_FLAT);
4930 SetRect(&rc, 34, 2, 46, 14);
4931 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4932 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4934 SelectObject(hdc, hbmOld);
4935 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4936 comctl32_color.clrWindow);
4937 TRACE("checkbox index %d\n", nIndex);
4941 ReleaseDC(0, hdcScreen);
4943 infoPtr->stateImageWidth = 16;
4944 infoPtr->stateImageHeight = 16;
4948 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4951 TREEVIEW_INFO *infoPtr;
4954 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4956 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4958 if (infoPtr == NULL)
4960 ERR("could not allocate info memory!\n");
4964 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4966 infoPtr->hwnd = hwnd;
4967 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4969 infoPtr->uNumItems = 0;
4970 infoPtr->cdmode = 0;
4971 infoPtr->uScrollTime = 300; /* milliseconds */
4972 infoPtr->bRedraw = TRUE;
4974 GetClientRect(hwnd, &rcClient);
4976 /* No scroll bars yet. */
4977 infoPtr->clientWidth = rcClient.right;
4978 infoPtr->clientHeight = rcClient.bottom;
4979 infoPtr->uInternalStatus = 0;
4981 infoPtr->treeWidth = 0;
4982 infoPtr->treeHeight = 0;
4984 infoPtr->uIndent = MINIMUM_INDENT;
4985 infoPtr->selectedItem = NULL;
4986 infoPtr->focusedItem = NULL;
4987 infoPtr->hotItem = NULL;
4988 infoPtr->editItem = NULL;
4989 infoPtr->firstVisible = NULL;
4990 infoPtr->maxVisibleOrder = 0;
4991 infoPtr->dropItem = NULL;
4992 infoPtr->insertMarkItem = NULL;
4993 infoPtr->insertBeforeorAfter = 0;
4996 infoPtr->scrollX = 0;
4998 infoPtr->clrBk = -1; /* use system color */
4999 infoPtr->clrText = -1; /* use system color */
5000 infoPtr->clrLine = RGB(128, 128, 128);
5001 infoPtr->clrInsertMark = comctl32_color.clrBtnText;
5005 infoPtr->hwndEdit = NULL;
5006 infoPtr->wpEditOrig = NULL;
5007 infoPtr->bIgnoreEditKillFocus = FALSE;
5008 infoPtr->bLabelChanged = FALSE;
5010 infoPtr->himlNormal = NULL;
5011 infoPtr->himlState = NULL;
5012 infoPtr->normalImageWidth = 0;
5013 infoPtr->normalImageHeight = 0;
5014 infoPtr->stateImageWidth = 0;
5015 infoPtr->stateImageHeight = 0;
5017 infoPtr->items = DPA_Create(16);
5019 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5020 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5021 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5022 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5023 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5025 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5027 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5028 infoPtr->root->state = TVIS_EXPANDED;
5029 infoPtr->root->iLevel = -1;
5030 infoPtr->root->visibleOrder = -1;
5032 infoPtr->hwndNotify = lpcs->hwndParent;
5034 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
5037 infoPtr->hwndToolTip = 0;
5039 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5041 /* Determine what type of notify should be issued */
5042 /* sets infoPtr->bNtfUnicode */
5043 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5045 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5046 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5047 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5050 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5051 initialize_checkboxes(infoPtr);
5053 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5054 ShowScrollBar(hwnd, SB_VERT, FALSE);
5055 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5057 OpenThemeData (hwnd, themeClass);
5064 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5068 /* free item data */
5069 TREEVIEW_RemoveTree(infoPtr);
5070 /* root isn't freed with other items */
5071 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5072 DPA_Destroy(infoPtr->items);
5074 /* tool tip is automatically destroyed: we are its owner */
5076 /* Restore original wndproc */
5077 if (infoPtr->hwndEdit)
5078 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5079 (DWORD_PTR)infoPtr->wpEditOrig);
5081 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5083 /* Deassociate treeview from the window before doing anything drastic. */
5084 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5087 DeleteObject(infoPtr->hDefaultFont);
5088 DeleteObject(infoPtr->hBoldFont);
5089 DeleteObject(infoPtr->hUnderlineFont);
5095 /* Miscellaneous Messages ***********************************************/
5098 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5106 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5107 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5108 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5109 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5110 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5111 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5112 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5113 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5114 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5118 if (key >= VK_PRIOR && key <= VK_DOWN)
5120 unsigned char code = scroll[key - VK_PRIOR].code;
5122 (((code & (1 << 7)) == (SB_HORZ << 7))
5124 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5130 /************************************************************************
5133 * VK_UP Move selection to the previous non-hidden item.
5134 * VK_DOWN Move selection to the next non-hidden item.
5135 * VK_HOME Move selection to the first item.
5136 * VK_END Move selection to the last item.
5137 * VK_LEFT If expanded then collapse, otherwise move to parent.
5138 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5140 * VK_SUBTRACT Collapse.
5141 * VK_MULTIPLY Expand all.
5142 * VK_PRIOR Move up GetVisibleCount items.
5143 * VK_NEXT Move down GetVisibleCount items.
5144 * VK_BACK Move to parent.
5145 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5148 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5150 /* If it is non-NULL and different, it will be selected and visible. */
5151 TREEVIEW_ITEM *newSelection = NULL;
5153 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5155 TRACE("%lx\n", wParam);
5157 if (prevItem == NULL)
5160 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5161 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5166 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5168 newSelection = infoPtr->root->firstChild;
5172 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5176 newSelection = infoPtr->root->firstChild;
5180 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5184 if (prevItem->state & TVIS_EXPANDED)
5186 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5188 else if (prevItem->parent != infoPtr->root)
5190 newSelection = prevItem->parent;
5195 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5197 if (!(prevItem->state & TVIS_EXPANDED))
5198 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5201 newSelection = prevItem->firstChild;
5208 TREEVIEW_ExpandAll(infoPtr, prevItem);
5212 if (!(prevItem->state & TVIS_EXPANDED))
5213 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5217 if (prevItem->state & TVIS_EXPANDED)
5218 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5223 = TREEVIEW_GetListItem(infoPtr, prevItem,
5224 -TREEVIEW_GetVisibleCount(infoPtr));
5229 = TREEVIEW_GetListItem(infoPtr, prevItem,
5230 TREEVIEW_GetVisibleCount(infoPtr));
5234 newSelection = prevItem->parent;
5235 if (newSelection == infoPtr->root)
5236 newSelection = NULL;
5240 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5241 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5245 if (newSelection && newSelection != prevItem)
5247 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5250 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5258 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5260 if (infoPtr->hotItem)
5262 /* remove hot effect from item */
5263 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5264 infoPtr->hotItem = NULL;
5270 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5273 TRACKMOUSEEVENT trackinfo;
5274 TREEVIEW_ITEM * item;
5276 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5278 /* fill in the TRACKMOUSEEVENT struct */
5279 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5280 trackinfo.dwFlags = TME_QUERY;
5281 trackinfo.hwndTrack = infoPtr->hwnd;
5282 trackinfo.dwHoverTime = HOVER_DEFAULT;
5284 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5285 _TrackMouseEvent(&trackinfo);
5287 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5288 if(!(trackinfo.dwFlags & TME_LEAVE))
5290 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5292 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5293 /* and can properly deactivate the hot item */
5294 _TrackMouseEvent(&trackinfo);
5297 pt.x = (short)LOWORD(lParam);
5298 pt.y = (short)HIWORD(lParam);
5300 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5302 if (item != infoPtr->hotItem)
5304 /* redraw old hot item */
5305 if (infoPtr->hotItem)
5306 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5307 infoPtr->hotItem = item;
5308 /* redraw new hot item */
5309 if (infoPtr->hotItem)
5310 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5316 /* Draw themed border */
5317 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5319 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5323 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5324 cyEdge = GetSystemMetrics (SM_CYEDGE);
5327 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5329 GetWindowRect(infoPtr->hwnd, &r);
5331 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5332 r.right - cxEdge, r.bottom - cyEdge);
5333 if (region != (HRGN)1)
5334 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5335 OffsetRect(&r, -r.left, -r.top);
5337 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5338 OffsetRect(&r, -r.left, -r.top);
5340 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5341 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5342 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5343 ReleaseDC(infoPtr->hwnd, dc);
5345 /* Call default proc to get the scrollbars etc. painted */
5346 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5352 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5354 LPNMHDR lpnmh = (LPNMHDR)lParam;
5356 if (lpnmh->code == PGN_CALCSIZE) {
5357 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5359 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5360 lppgc->iWidth = infoPtr->treeWidth;
5361 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5362 infoPtr->treeWidth, infoPtr->clientWidth);
5365 lppgc->iHeight = infoPtr->treeHeight;
5366 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5367 infoPtr->treeHeight, infoPtr->clientHeight);
5371 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5374 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5378 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5380 if (nCommand != NF_REQUERY) return 0;
5382 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5383 TRACE("format=%d\n", format);
5385 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5387 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5393 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5395 if (wParam == SIZE_RESTORED)
5397 infoPtr->clientWidth = (short)LOWORD(lParam);
5398 infoPtr->clientHeight = (short)HIWORD(lParam);
5400 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5401 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5402 TREEVIEW_UpdateScrollBars(infoPtr);
5406 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5409 TREEVIEW_Invalidate(infoPtr, NULL);
5414 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5416 TRACE("(%lx %lx)\n", wParam, lParam);
5418 if (wParam == GWL_STYLE)
5420 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5422 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5424 if (dwNewStyle & TVS_CHECKBOXES)
5426 initialize_checkboxes(infoPtr);
5427 TRACE("checkboxes enabled\n");
5431 FIXME("tried to disable checkboxes\n");
5435 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5437 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5439 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5440 TRACE("tooltips enabled\n");
5444 DestroyWindow(infoPtr->hwndToolTip);
5445 infoPtr->hwndToolTip = 0;
5446 TRACE("tooltips disabled\n");
5450 infoPtr->dwStyle = dwNewStyle;
5453 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5454 TREEVIEW_UpdateScrollBars(infoPtr);
5455 TREEVIEW_Invalidate(infoPtr, NULL);
5461 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5464 TREEVIEW_ITEM * item;
5467 ScreenToClient(infoPtr->hwnd, &pt);
5469 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5471 /* FIXME: send NM_SETCURSOR */
5473 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5475 SetCursor(infoPtr->hcurHand);
5479 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5483 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5487 if (!infoPtr->selectedItem)
5489 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5493 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5494 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5499 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5503 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5504 UpdateWindow(infoPtr->hwnd);
5505 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5509 /* update theme after a WM_THEMECHANGED message */
5510 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5512 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5513 CloseThemeData (theme);
5514 OpenThemeData (infoPtr->hwnd, themeClass);
5519 static LRESULT WINAPI
5520 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5522 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5524 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5526 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5529 if (uMsg == WM_CREATE)
5530 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5537 case TVM_CREATEDRAGIMAGE:
5538 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5540 case TVM_DELETEITEM:
5541 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5543 case TVM_EDITLABELA:
5544 case TVM_EDITLABELW:
5545 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5547 case TVM_ENDEDITLABELNOW:
5548 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5550 case TVM_ENSUREVISIBLE:
5551 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5554 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5556 case TVM_GETBKCOLOR:
5557 return TREEVIEW_GetBkColor(infoPtr);
5560 return TREEVIEW_GetCount(infoPtr);
5562 case TVM_GETEDITCONTROL:
5563 return TREEVIEW_GetEditControl(infoPtr);
5565 case TVM_GETIMAGELIST:
5566 return TREEVIEW_GetImageList(infoPtr, wParam);
5569 return TREEVIEW_GetIndent(infoPtr);
5571 case TVM_GETINSERTMARKCOLOR:
5572 return TREEVIEW_GetInsertMarkColor(infoPtr);
5574 case TVM_GETISEARCHSTRINGA:
5575 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5578 case TVM_GETISEARCHSTRINGW:
5579 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5584 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5585 uMsg == TVM_GETITEMW);
5586 case TVM_GETITEMHEIGHT:
5587 return TREEVIEW_GetItemHeight(infoPtr);
5589 case TVM_GETITEMRECT:
5590 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5592 case TVM_GETITEMSTATE:
5593 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5595 case TVM_GETLINECOLOR:
5596 return TREEVIEW_GetLineColor(infoPtr);
5598 case TVM_GETNEXTITEM:
5599 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5601 case TVM_GETSCROLLTIME:
5602 return TREEVIEW_GetScrollTime(infoPtr);
5604 case TVM_GETTEXTCOLOR:
5605 return TREEVIEW_GetTextColor(infoPtr);
5607 case TVM_GETTOOLTIPS:
5608 return TREEVIEW_GetToolTips(infoPtr);
5610 case TVM_GETUNICODEFORMAT:
5611 return TREEVIEW_GetUnicodeFormat(infoPtr);
5613 case TVM_GETVISIBLECOUNT:
5614 return TREEVIEW_GetVisibleCount(infoPtr);
5617 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5619 case TVM_INSERTITEMA:
5620 case TVM_INSERTITEMW:
5621 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5622 uMsg == TVM_INSERTITEMW);
5623 case TVM_SELECTITEM:
5624 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5626 case TVM_SETBKCOLOR:
5627 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5629 case TVM_SETIMAGELIST:
5630 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5633 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5635 case TVM_SETINSERTMARK:
5636 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5638 case TVM_SETINSERTMARKCOLOR:
5639 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5643 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5644 uMsg == TVM_SETITEMW);
5645 case TVM_SETLINECOLOR:
5646 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5648 case TVM_SETITEMHEIGHT:
5649 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5651 case TVM_SETSCROLLTIME:
5652 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5654 case TVM_SETTEXTCOLOR:
5655 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5657 case TVM_SETTOOLTIPS:
5658 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5660 case TVM_SETUNICODEFORMAT:
5661 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5663 case TVM_SORTCHILDREN:
5664 return TREEVIEW_SortChildren(infoPtr, lParam);
5666 case TVM_SORTCHILDRENCB:
5667 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5670 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5673 return TREEVIEW_Command(infoPtr, wParam, lParam);
5676 return TREEVIEW_Destroy(infoPtr);
5681 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5684 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5687 return TREEVIEW_GetFont(infoPtr);
5690 return TREEVIEW_HScroll(infoPtr, wParam);
5693 return TREEVIEW_KeyDown(infoPtr, wParam);
5696 return TREEVIEW_KillFocus(infoPtr);
5698 case WM_LBUTTONDBLCLK:
5699 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5701 case WM_LBUTTONDOWN:
5702 return TREEVIEW_LButtonDown(infoPtr, lParam);
5704 /* WM_MBUTTONDOWN */
5707 return TREEVIEW_MouseLeave(infoPtr);
5710 return TREEVIEW_MouseMove(infoPtr, lParam);
5712 case WM_NCLBUTTONDOWN:
5713 if (infoPtr->hwndEdit)
5714 SetFocus(infoPtr->hwnd);
5718 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5721 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5723 case WM_NOTIFYFORMAT:
5724 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5726 case WM_PRINTCLIENT:
5727 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5730 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5732 case WM_RBUTTONDOWN:
5733 return TREEVIEW_RButtonDown(infoPtr, lParam);
5736 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5739 return TREEVIEW_SetFocus(infoPtr);
5742 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5745 return TREEVIEW_SetRedraw(infoPtr, wParam);
5748 return TREEVIEW_Size(infoPtr, wParam, lParam);
5750 case WM_STYLECHANGED:
5751 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5753 case WM_SYSCOLORCHANGE:
5754 COMCTL32_RefreshSysColors();
5760 return TREEVIEW_HandleTimer(infoPtr, wParam);
5762 case WM_THEMECHANGED:
5763 return TREEVIEW_ThemeChanged (infoPtr);
5766 return TREEVIEW_VScroll(infoPtr, wParam);
5768 /* WM_WININICHANGE */
5771 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5774 TRACE("drawItem\n");
5778 /* This mostly catches MFC and Delphi messages. :( */
5779 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5780 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5782 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5787 /* Class Registration ***************************************************/
5790 TREEVIEW_Register(void)
5796 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5797 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5798 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5799 wndClass.cbClsExtra = 0;
5800 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5802 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5803 wndClass.hbrBackground = 0;
5804 wndClass.lpszClassName = WC_TREEVIEWW;
5806 RegisterClassW(&wndClass);
5811 TREEVIEW_Unregister(void)
5813 UnregisterClassW(WC_TREEVIEWW, NULL);
5817 /* Tree Verification ****************************************************/
5821 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5823 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5824 TREEVIEW_ITEM *item)
5826 assert(infoPtr != NULL);
5827 assert(item != NULL);
5829 /* both NULL, or both non-null */
5830 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5832 assert(item->firstChild != item);
5833 assert(item->lastChild != item);
5835 if (item->firstChild)
5837 assert(item->firstChild->parent == item);
5838 assert(item->firstChild->prevSibling == NULL);
5841 if (item->lastChild)
5843 assert(item->lastChild->parent == item);
5844 assert(item->lastChild->nextSibling == NULL);
5847 assert(item->nextSibling != item);
5848 if (item->nextSibling)
5850 assert(item->nextSibling->parent == item->parent);
5851 assert(item->nextSibling->prevSibling == item);
5854 assert(item->prevSibling != item);
5855 if (item->prevSibling)
5857 assert(item->prevSibling->parent == item->parent);
5858 assert(item->prevSibling->nextSibling == item);
5863 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5865 assert(item != NULL);
5867 assert(item->parent != NULL);
5868 assert(item->parent != item);
5869 assert(item->iLevel == item->parent->iLevel + 1);
5871 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5873 TREEVIEW_VerifyItemCommon(infoPtr, item);
5875 TREEVIEW_VerifyChildren(infoPtr, item);
5879 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5881 TREEVIEW_ITEM *child;
5882 assert(item != NULL);
5884 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5885 TREEVIEW_VerifyItem(infoPtr, child);
5889 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5891 TREEVIEW_ITEM *root = infoPtr->root;
5893 assert(root != NULL);
5894 assert(root->iLevel == -1);
5895 assert(root->parent == NULL);
5896 assert(root->prevSibling == NULL);
5898 TREEVIEW_VerifyItemCommon(infoPtr, root);
5900 TREEVIEW_VerifyChildren(infoPtr, root);
5904 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5906 assert(infoPtr != NULL);
5908 TREEVIEW_VerifyRoot(infoPtr);