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: All items always! have valid (allocated) pszText field.
27 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
28 * of size TEXT_CALLBACK_SIZE in DoSetItem.
29 * We use callbackMask to keep track of fields to be updated.
32 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
33 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
35 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
37 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
44 #include "wine/port.h"
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 /* internal structures */
69 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
80 int iIntegral; /* item height multiplier (1 is normal) */
81 int iLevel; /* indentation level:0=root level */
82 HTREEITEM parent; /* handle to parent or 0 if at root */
83 HTREEITEM firstChild; /* handle to first child or 0 if no child */
85 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
86 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
92 LONG textWidth; /* horizontal text extent for pszText */
93 LONG visibleOrder; /* visible ordering, 0 is first visible item */
97 typedef struct tagTREEVIEW_INFO
100 HWND hwndNotify; /* Owner window to send notifications to */
103 UINT uInternalStatus;
105 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
106 INT cdmode; /* last custom draw setting */
107 UINT uScrollTime; /* max. time for scrolling in milliseconds */
108 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
110 UINT uItemHeight; /* item height */
113 LONG clientWidth; /* width of control window */
114 LONG clientHeight; /* height of control window */
116 LONG treeWidth; /* width of visible tree items */
117 LONG treeHeight; /* height of visible tree items */
119 UINT uIndent; /* indentation in pixels */
120 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
121 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
122 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
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 /* dito 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(TREEVIEW_INFO *, 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(TREEVIEW_INFO *, LPPOINT);
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(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
244 assert(infoPtr != NULL);
246 return DPA_GetPtrIndex(infoPtr->items, handle);
249 /* Checks if item has changed and needs to be redrawn */
250 static inline BOOL item_changed (TREEVIEW_ITEM *tiOld, TREEVIEW_ITEM *tiNew, LPTVITEMEXW tvChange)
252 /* Number of children has changed */
253 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
256 /* Image has changed and it's not a callback */
257 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
258 tiNew->iImage != I_IMAGECALLBACK)
261 /* Selected image has changed and it's not a callback */
262 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
263 tiNew->iSelectedImage != I_IMAGECALLBACK)
266 /* Text has changed and it's not a callback */
267 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
268 tiNew->pszText != LPSTR_TEXTCALLBACKW)
271 /* Indent has changed */
272 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
275 /* Item state has changed */
276 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
282 /***************************************************************************
283 * This method checks that handle is an item for this tree.
286 TREEVIEW_ValidItem(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
288 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
290 TRACE("invalid item %p\n", handle);
298 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
302 GetObjectW(hOrigFont, sizeof(font), &font);
303 font.lfWeight = FW_BOLD;
304 return CreateFontIndirectW(&font);
308 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
312 GetObjectW(hOrigFont, sizeof(font), &font);
313 font.lfUnderline = TRUE;
314 return CreateFontIndirectW(&font);
318 TREEVIEW_FontForItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
320 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
321 return infoPtr->hUnderlineFont;
322 if (item->state & TVIS_BOLD)
323 return infoPtr->hBoldFont;
324 return infoPtr->hFont;
327 /* for trace/debugging purposes only */
329 TREEVIEW_ItemName(TREEVIEW_ITEM *item)
331 if (item == NULL) return "<null item>";
332 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
333 if (item->pszText == NULL) return "<null>";
334 return debugstr_w(item->pszText);
337 /* An item is not a child of itself. */
339 TREEVIEW_IsChildOf(TREEVIEW_ITEM *parent, TREEVIEW_ITEM *child)
343 child = child->parent;
344 if (child == parent) return TRUE;
345 } while (child != NULL);
351 /* Tree Traversal *******************************************************/
353 /***************************************************************************
354 * This method returns the last expanded sibling or child child item
357 static TREEVIEW_ITEM *
358 TREEVIEW_GetLastListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
363 while (wineItem->lastChild)
365 if (wineItem->state & TVIS_EXPANDED)
366 wineItem = wineItem->lastChild;
371 if (wineItem == infoPtr->root)
377 /***************************************************************************
378 * This method returns the previous non-hidden item in the list not
379 * considering the tree hierarchy.
381 static TREEVIEW_ITEM *
382 TREEVIEW_GetPrevListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
384 if (tvItem->prevSibling)
386 /* This item has a prevSibling, get the last item in the sibling's tree. */
387 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
389 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
390 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
396 /* this item does not have a prevSibling, get the parent */
397 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
402 /***************************************************************************
403 * This method returns the next physical item in the treeview not
404 * considering the tree hierarchy.
406 static TREEVIEW_ITEM *
407 TREEVIEW_GetNextListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
409 assert(tvItem != NULL);
412 * If this item has children and is expanded, return the first child
414 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
416 return tvItem->firstChild;
421 * try to get the sibling
423 if (tvItem->nextSibling)
424 return tvItem->nextSibling;
427 * Otherwise, get the parent's sibling.
429 while (tvItem->parent)
431 tvItem = tvItem->parent;
433 if (tvItem->nextSibling)
434 return tvItem->nextSibling;
440 /***************************************************************************
441 * This method returns the nth item starting at the given item. It returns
442 * the last item (or first) we we run out of items.
444 * Will scroll backward if count is <0.
445 * forward if count is >0.
447 static TREEVIEW_ITEM *
448 TREEVIEW_GetListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
451 TREEVIEW_ITEM *(*next_item)(TREEVIEW_INFO *, TREEVIEW_ITEM *);
452 TREEVIEW_ITEM *previousItem;
454 assert(wineItem != NULL);
458 next_item = TREEVIEW_GetNextListItem;
463 next_item = TREEVIEW_GetPrevListItem;
470 previousItem = wineItem;
471 wineItem = next_item(infoPtr, wineItem);
473 } while (--count && wineItem != NULL);
476 return wineItem ? wineItem : previousItem;
479 /* Notifications ************************************************************/
481 static INT get_notifycode(TREEVIEW_INFO *infoPtr, INT code)
483 if (!infoPtr->bNtfUnicode) {
485 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
486 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
487 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
488 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
489 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
490 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
491 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
492 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
493 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
494 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
495 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
496 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
503 TREEVIEW_SendRealNotify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
505 TRACE("wParam=%d, lParam=%ld\n", wParam, lParam);
506 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
510 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO *infoPtr, UINT code)
513 HWND hwnd = infoPtr->hwnd;
516 nmhdr.hwndFrom = hwnd;
517 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
518 nmhdr.code = get_notifycode(infoPtr, code);
520 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
521 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
525 TREEVIEW_TVItemFromItem(TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
528 tvItem->hItem = item;
529 tvItem->state = item->state;
530 tvItem->stateMask = 0;
531 tvItem->iImage = item->iImage;
532 tvItem->iSelectedImage = item->iSelectedImage;
533 tvItem->cChildren = item->cChildren;
534 tvItem->lParam = item->lParam;
538 if (!infoPtr->bNtfUnicode)
540 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
541 tvItem->pszText = Alloc (tvItem->cchTextMax);
542 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
546 tvItem->cchTextMax = item->cchTextMax;
547 tvItem->pszText = item->pszText;
552 tvItem->cchTextMax = 0;
553 tvItem->pszText = NULL;
558 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO *infoPtr, UINT code, UINT action,
559 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
561 HWND hwnd = infoPtr->hwnd;
565 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
566 code, action, oldItem, newItem);
568 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
570 nmhdr.hdr.hwndFrom = hwnd;
571 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
572 nmhdr.hdr.code = get_notifycode(infoPtr, code);
573 nmhdr.action = action;
576 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
579 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
584 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
585 (WPARAM)nmhdr.hdr.idFrom,
587 if (!infoPtr->bNtfUnicode)
589 Free(nmhdr.itemOld.pszText);
590 Free(nmhdr.itemNew.pszText);
596 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO *infoPtr, UINT code,
597 HTREEITEM dragItem, POINT pt)
599 HWND hwnd = infoPtr->hwnd;
602 TRACE("code:%d dragitem:%p\n", code, dragItem);
604 nmhdr.hdr.hwndFrom = hwnd;
605 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
606 nmhdr.hdr.code = get_notifycode(infoPtr, code);
608 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
609 nmhdr.itemNew.hItem = dragItem;
610 nmhdr.itemNew.state = dragItem->state;
611 nmhdr.itemNew.lParam = dragItem->lParam;
613 nmhdr.ptDrag.x = pt.x;
614 nmhdr.ptDrag.y = pt.y;
616 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
617 (WPARAM)nmhdr.hdr.idFrom,
623 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
626 HWND hwnd = infoPtr->hwnd;
627 NMTVCUSTOMDRAW nmcdhdr;
630 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
632 nmcd = &nmcdhdr.nmcd;
633 nmcd->hdr.hwndFrom = hwnd;
634 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
635 nmcd->hdr.code = NM_CUSTOMDRAW;
636 nmcd->dwDrawStage = dwDrawStage;
639 nmcd->dwItemSpec = 0;
640 nmcd->uItemState = 0;
641 nmcd->lItemlParam = 0;
642 nmcdhdr.clrText = infoPtr->clrText;
643 nmcdhdr.clrTextBk = infoPtr->clrBk;
646 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
647 (WPARAM)nmcd->hdr.idFrom,
653 /* FIXME: need to find out when the flags in uItemState need to be set */
656 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO *infoPtr, HDC hdc,
657 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
658 NMTVCUSTOMDRAW *nmcdhdr)
660 HWND hwnd = infoPtr->hwnd;
663 DWORD_PTR dwItemSpec;
667 dwDrawStage = CDDS_ITEM | uItemDrawState;
668 dwItemSpec = (DWORD_PTR)wineItem;
670 if (wineItem->state & TVIS_SELECTED)
671 uItemState |= CDIS_SELECTED;
672 if (wineItem == infoPtr->selectedItem)
673 uItemState |= CDIS_FOCUS;
674 if (wineItem == infoPtr->hotItem)
675 uItemState |= CDIS_HOT;
677 nmcd = &nmcdhdr->nmcd;
678 nmcd->hdr.hwndFrom = hwnd;
679 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
680 nmcd->hdr.code = NM_CUSTOMDRAW;
681 nmcd->dwDrawStage = dwDrawStage;
683 nmcd->rc = wineItem->rect;
684 nmcd->dwItemSpec = dwItemSpec;
685 nmcd->uItemState = uItemState;
686 nmcd->lItemlParam = wineItem->lParam;
687 nmcdhdr->iLevel = wineItem->iLevel;
689 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
690 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
691 nmcd->uItemState, nmcd->lItemlParam);
693 retval = TREEVIEW_SendRealNotify(infoPtr,
694 (WPARAM)nmcd->hdr.idFrom,
701 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
703 HWND hwnd = infoPtr->hwnd;
707 tvdi.hdr.hwndFrom = hwnd;
708 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
709 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
711 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
712 &tvdi.item, editItem);
714 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
716 if (!infoPtr->bNtfUnicode)
717 Free(tvdi.item.pszText);
723 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
726 NMTVDISPINFOW callback;
727 HWND hwnd = infoPtr->hwnd;
729 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
730 mask &= wineItem->callbackMask;
732 if (mask == 0) return;
734 callback.hdr.hwndFrom = hwnd;
735 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
736 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
738 /* 'state' always contains valid value, as well as 'lParam'.
739 * All other parameters are uninitialized.
741 callback.item.pszText = wineItem->pszText;
742 callback.item.cchTextMax = wineItem->cchTextMax;
743 callback.item.mask = mask;
744 callback.item.hItem = wineItem;
745 callback.item.state = wineItem->state;
746 callback.item.lParam = wineItem->lParam;
748 /* If text is changed we need to recalculate textWidth */
749 if (mask & TVIF_TEXT)
750 wineItem->textWidth = 0;
752 TREEVIEW_SendRealNotify(infoPtr,
753 (WPARAM)callback.hdr.idFrom, (LPARAM)&callback);
755 /* It may have changed due to a call to SetItem. */
756 mask &= wineItem->callbackMask;
758 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
760 /* Instead of copying text into our buffer user specified its own */
761 if (!infoPtr->bNtfUnicode) {
764 int len = MultiByteToWideChar( CP_ACP, 0,
765 (LPSTR)callback.item.pszText, -1,
767 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
768 newText = (LPWSTR)ReAlloc(wineItem->pszText, buflen);
770 TRACE("returned str %s, len=%d, buflen=%d\n",
771 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
775 wineItem->pszText = newText;
776 MultiByteToWideChar( CP_ACP, 0,
777 (LPSTR)callback.item.pszText, -1,
778 wineItem->pszText, buflen/sizeof(WCHAR));
779 wineItem->cchTextMax = buflen/sizeof(WCHAR);
781 /* If ReAlloc fails we have nothing to do, but keep original text */
784 int len = max(lstrlenW(callback.item.pszText) + 1,
786 LPWSTR newText = ReAlloc(wineItem->pszText, len);
788 TRACE("returned wstr %s, len=%d\n",
789 debugstr_w(callback.item.pszText), len);
793 wineItem->pszText = newText;
794 strcpyW(wineItem->pszText, callback.item.pszText);
795 wineItem->cchTextMax = len;
797 /* If ReAlloc fails we have nothing to do, but keep original text */
800 else if (mask & TVIF_TEXT) {
801 /* User put text into our buffer, that is ok unless A string */
802 if (!infoPtr->bNtfUnicode) {
804 LPWSTR oldText = NULL;
806 int len = MultiByteToWideChar( CP_ACP, 0,
807 (LPSTR)callback.item.pszText, -1,
809 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
810 newText = (LPWSTR)Alloc(buflen);
812 TRACE("same buffer str %s, len=%d, buflen=%d\n",
813 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
817 oldText = wineItem->pszText;
818 wineItem->pszText = newText;
819 MultiByteToWideChar( CP_ACP, 0,
820 (LPSTR)callback.item.pszText, -1,
821 wineItem->pszText, buflen/sizeof(WCHAR));
822 wineItem->cchTextMax = buflen/sizeof(WCHAR);
828 if (mask & TVIF_IMAGE)
829 wineItem->iImage = callback.item.iImage;
831 if (mask & TVIF_SELECTEDIMAGE)
832 wineItem->iSelectedImage = callback.item.iSelectedImage;
834 if (mask & TVIF_CHILDREN)
835 wineItem->cChildren = callback.item.cChildren;
837 /* These members are now permanently set. */
838 if (callback.item.mask & TVIF_DI_SETITEM)
839 wineItem->callbackMask &= ~callback.item.mask;
842 /***************************************************************************
843 * This function uses cChildren field to decide whether the item has
845 * Note: if this returns TRUE, the child items may not actually exist,
846 * they could be virtual.
848 * Just use wineItem->firstChild to check for physical children.
851 TREEVIEW_HasChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
853 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
855 return wineItem->cChildren > 0;
859 /* Item Position ********************************************************/
861 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
863 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO *infoPtr,
866 /* Same effect, different optimisation. */
868 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
869 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
871 BOOL lar = ((infoPtr->dwStyle
872 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
876 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
878 item->stateOffset = item->linesOffset + infoPtr->uIndent;
879 item->imageOffset = item->stateOffset
880 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
881 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
885 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
891 /* DRAW's OM docker creates items like this */
892 if (item->pszText == NULL)
904 hdc = GetDC(infoPtr->hwnd);
905 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
908 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
909 item->textWidth = sz.cx;
913 SelectObject(hdc, hOldFont);
919 TREEVIEW_ComputeItemRect(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
921 item->rect.top = infoPtr->uItemHeight *
922 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
924 item->rect.bottom = item->rect.top
925 + infoPtr->uItemHeight * item->iIntegral - 1;
928 item->rect.right = infoPtr->clientWidth;
931 /* We know that only items after start need their order updated. */
933 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
940 start = infoPtr->root->firstChild;
944 order = start->visibleOrder;
946 for (item = start; item != NULL;
947 item = TREEVIEW_GetNextListItem(infoPtr, item))
949 item->visibleOrder = order;
950 order += item->iIntegral;
953 infoPtr->maxVisibleOrder = order;
955 for (item = start; item != NULL;
956 item = TREEVIEW_GetNextListItem(infoPtr, item))
958 TREEVIEW_ComputeItemRect(infoPtr, item);
963 /* Update metrics of all items in selected subtree.
964 * root must be expanded
967 TREEVIEW_UpdateSubTree(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
969 TREEVIEW_ITEM *sibling;
973 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
976 root->state &= ~TVIS_EXPANDED;
977 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
978 root->state |= TVIS_EXPANDED;
980 hdc = GetDC(infoPtr->hwnd);
981 hOldFont = SelectObject(hdc, infoPtr->hFont);
983 for (; root != sibling;
984 root = TREEVIEW_GetNextListItem(infoPtr, root))
986 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
988 if (root->callbackMask & TVIF_TEXT)
989 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
991 if (root->textWidth == 0)
993 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
994 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
998 SelectObject(hdc, hOldFont);
999 ReleaseDC(infoPtr->hwnd, hdc);
1002 /* Item Allocation **********************************************************/
1004 static TREEVIEW_ITEM *
1005 TREEVIEW_AllocateItem(TREEVIEW_INFO *infoPtr)
1007 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1012 /* I_IMAGENONE would make more sense but this is neither what is
1013 * documented (MSDN doesn't specify) nor what Windows actually does
1014 * (it sets it to zero)... and I can so imagine an application using
1015 * inc/dec to toggle the images. */
1016 newItem->iImage = 0;
1017 newItem->iSelectedImage = 0;
1019 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1028 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1029 * free item->pszText. */
1031 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1033 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1035 if (infoPtr->selectedItem == item)
1036 infoPtr->selectedItem = NULL;
1037 if (infoPtr->hotItem == item)
1038 infoPtr->hotItem = NULL;
1039 if (infoPtr->focusedItem == item)
1040 infoPtr->focusedItem = NULL;
1041 if (infoPtr->firstVisible == item)
1042 infoPtr->firstVisible = NULL;
1043 if (infoPtr->dropItem == item)
1044 infoPtr->dropItem = NULL;
1045 if (infoPtr->insertMarkItem == item)
1046 infoPtr->insertMarkItem = NULL;
1050 /* Item Insertion *******************************************************/
1052 /***************************************************************************
1053 * This method inserts newItem before sibling as a child of parent.
1054 * sibling can be NULL, but only if parent has no children.
1057 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1058 TREEVIEW_ITEM *parent)
1060 assert(newItem != NULL);
1061 assert(parent != NULL);
1063 if (sibling != NULL)
1065 assert(sibling->parent == parent);
1067 if (sibling->prevSibling != NULL)
1068 sibling->prevSibling->nextSibling = newItem;
1070 newItem->prevSibling = sibling->prevSibling;
1071 sibling->prevSibling = newItem;
1074 newItem->prevSibling = NULL;
1076 newItem->nextSibling = sibling;
1078 if (parent->firstChild == sibling)
1079 parent->firstChild = newItem;
1081 if (parent->lastChild == NULL)
1082 parent->lastChild = newItem;
1085 /***************************************************************************
1086 * This method inserts newItem after sibling as a child of parent.
1087 * sibling can be NULL, but only if parent has no children.
1090 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1091 TREEVIEW_ITEM *parent)
1093 assert(newItem != NULL);
1094 assert(parent != NULL);
1096 if (sibling != NULL)
1098 assert(sibling->parent == parent);
1100 if (sibling->nextSibling != NULL)
1101 sibling->nextSibling->prevSibling = newItem;
1103 newItem->nextSibling = sibling->nextSibling;
1104 sibling->nextSibling = newItem;
1107 newItem->nextSibling = NULL;
1109 newItem->prevSibling = sibling;
1111 if (parent->lastChild == sibling)
1112 parent->lastChild = newItem;
1114 if (parent->firstChild == NULL)
1115 parent->firstChild = newItem;
1119 TREEVIEW_DoSetItemT(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1120 const TVITEMEXW *tvItem, BOOL isW)
1122 UINT callbackClear = 0;
1123 UINT callbackSet = 0;
1125 TRACE("item %p\n", wineItem);
1126 /* Do this first in case it fails. */
1127 if (tvItem->mask & TVIF_TEXT)
1129 wineItem->textWidth = 0; /* force width recalculation */
1130 if (tvItem->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
1135 len = lstrlenW(tvItem->pszText) + 1;
1137 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1139 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1141 if (newText == NULL) return FALSE;
1143 callbackClear |= TVIF_TEXT;
1145 wineItem->pszText = newText;
1146 wineItem->cchTextMax = len;
1148 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1150 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1151 wineItem->pszText, len);
1153 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1157 callbackSet |= TVIF_TEXT;
1159 wineItem->pszText = ReAlloc(wineItem->pszText,
1160 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1161 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1162 TRACE("setting callback, item %p\n", wineItem);
1166 if (tvItem->mask & TVIF_CHILDREN)
1168 wineItem->cChildren = tvItem->cChildren;
1170 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1171 callbackSet |= TVIF_CHILDREN;
1173 callbackClear |= TVIF_CHILDREN;
1176 if (tvItem->mask & TVIF_IMAGE)
1178 wineItem->iImage = tvItem->iImage;
1180 if (wineItem->iImage == I_IMAGECALLBACK)
1181 callbackSet |= TVIF_IMAGE;
1183 callbackClear |= TVIF_IMAGE;
1186 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1188 wineItem->iSelectedImage = tvItem->iSelectedImage;
1190 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1191 callbackSet |= TVIF_SELECTEDIMAGE;
1193 callbackClear |= TVIF_SELECTEDIMAGE;
1196 if (tvItem->mask & TVIF_PARAM)
1197 wineItem->lParam = tvItem->lParam;
1199 /* If the application sets TVIF_INTEGRAL without
1200 * supplying a TVITEMEX structure, it's toast. */
1201 if (tvItem->mask & TVIF_INTEGRAL)
1202 wineItem->iIntegral = tvItem->iIntegral;
1204 if (tvItem->mask & TVIF_STATE)
1206 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1208 wineItem->state &= ~tvItem->stateMask;
1209 wineItem->state |= (tvItem->state & tvItem->stateMask);
1212 wineItem->callbackMask |= callbackSet;
1213 wineItem->callbackMask &= ~callbackClear;
1218 /* Note that the new item is pre-zeroed. */
1220 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1222 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1223 HTREEITEM insertAfter;
1224 TREEVIEW_ITEM *newItem, *parentItem;
1225 BOOL bTextUpdated = FALSE;
1227 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1229 parentItem = infoPtr->root;
1233 parentItem = ptdi->hParent;
1235 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1237 WARN("invalid parent %p\n", parentItem);
1238 return (LRESULT)(HTREEITEM)NULL;
1242 insertAfter = ptdi->hInsertAfter;
1244 /* Validate this now for convenience. */
1245 switch ((DWORD_PTR)insertAfter)
1247 case (DWORD_PTR)TVI_FIRST:
1248 case (DWORD_PTR)TVI_LAST:
1249 case (DWORD_PTR)TVI_SORT:
1253 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1254 insertAfter->parent != parentItem)
1256 WARN("invalid insert after %p\n", insertAfter);
1257 insertAfter = TVI_LAST;
1261 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1262 (tvItem->mask & TVIF_TEXT)
1263 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1264 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1267 newItem = TREEVIEW_AllocateItem(infoPtr);
1268 if (newItem == NULL)
1269 return (LRESULT)(HTREEITEM)NULL;
1271 newItem->parent = parentItem;
1272 newItem->iIntegral = 1;
1274 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1275 return (LRESULT)(HTREEITEM)NULL;
1277 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1279 infoPtr->uNumItems++;
1281 switch ((DWORD_PTR)insertAfter)
1283 case (DWORD_PTR)TVI_FIRST:
1285 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1286 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1287 if (infoPtr->firstVisible == originalFirst)
1288 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1292 case (DWORD_PTR)TVI_LAST:
1293 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1296 /* hInsertAfter names a specific item we want to insert after */
1298 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1301 case (DWORD_PTR)TVI_SORT:
1303 TREEVIEW_ITEM *aChild;
1304 TREEVIEW_ITEM *previousChild = NULL;
1305 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1306 BOOL bItemInserted = FALSE;
1308 aChild = parentItem->firstChild;
1310 bTextUpdated = TRUE;
1311 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1313 /* Iterate the parent children to see where we fit in */
1314 while (aChild != NULL)
1318 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1319 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1321 if (comp < 0) /* we are smaller than the current one */
1323 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1324 if (infoPtr->firstVisible == originalFirst &&
1325 aChild == originalFirst)
1326 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1327 bItemInserted = TRUE;
1330 else if (comp > 0) /* we are bigger than the current one */
1332 previousChild = aChild;
1334 /* This will help us to exit if there is no more sibling */
1335 aChild = (aChild->nextSibling == 0)
1337 : aChild->nextSibling;
1339 /* Look at the next item */
1345 * An item with this name is already existing, therefore,
1346 * we add after the one we found
1348 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1349 bItemInserted = TRUE;
1355 * we reach the end of the child list and the item has not
1356 * yet been inserted, therefore, insert it after the last child.
1358 if ((!bItemInserted) && (aChild == NULL))
1359 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1366 TRACE("new item %p; parent %p, mask %x\n", newItem,
1367 newItem->parent, tvItem->mask);
1369 newItem->iLevel = newItem->parent->iLevel + 1;
1371 if (newItem->parent->cChildren == 0)
1372 newItem->parent->cChildren = 1;
1374 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1376 if (STATEIMAGEINDEX(newItem->state) == 0)
1377 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1380 if (infoPtr->firstVisible == NULL)
1381 infoPtr->firstVisible = newItem;
1383 TREEVIEW_VerifyTree(infoPtr);
1385 if (parentItem == infoPtr->root ||
1386 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1388 TREEVIEW_ITEM *item;
1389 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1391 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1392 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1395 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1397 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1398 TREEVIEW_UpdateScrollBars(infoPtr);
1400 * if the item was inserted in a visible part of the tree,
1401 * invalidate it, as well as those after it
1403 for (item = newItem;
1405 item = TREEVIEW_GetNextListItem(infoPtr, item))
1406 TREEVIEW_Invalidate(infoPtr, item);
1410 newItem->visibleOrder = -1;
1412 /* refresh treeview if newItem is the first item inserted under parentItem */
1413 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1415 /* parent got '+' - update it */
1416 TREEVIEW_Invalidate(infoPtr, parentItem);
1420 return (LRESULT)newItem;
1423 /* Item Deletion ************************************************************/
1425 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1428 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1430 TREEVIEW_ITEM *kill = parentItem->firstChild;
1432 while (kill != NULL)
1434 TREEVIEW_ITEM *next = kill->nextSibling;
1436 TREEVIEW_RemoveItem(infoPtr, kill);
1441 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1442 assert(parentItem->firstChild == NULL);
1443 assert(parentItem->lastChild == NULL);
1447 TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1449 TREEVIEW_ITEM *parentItem = item->parent;
1451 assert(item != NULL);
1452 assert(item->parent != NULL); /* i.e. it must not be the root */
1454 if (parentItem->firstChild == item)
1455 parentItem->firstChild = item->nextSibling;
1457 if (parentItem->lastChild == item)
1458 parentItem->lastChild = item->prevSibling;
1460 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1461 && parentItem->cChildren > 0)
1462 parentItem->cChildren = 0;
1464 if (item->prevSibling)
1465 item->prevSibling->nextSibling = item->nextSibling;
1467 if (item->nextSibling)
1468 item->nextSibling->prevSibling = item->prevSibling;
1472 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1474 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1476 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1477 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1479 if (wineItem->firstChild)
1480 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1482 TREEVIEW_UnlinkItem(wineItem);
1484 infoPtr->uNumItems--;
1486 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1487 Free(wineItem->pszText);
1489 TREEVIEW_FreeItem(infoPtr, wineItem);
1493 /* Empty out the tree. */
1495 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1497 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1499 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1503 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1505 TREEVIEW_ITEM *newSelection = NULL;
1506 TREEVIEW_ITEM *newFirstVisible = NULL;
1507 TREEVIEW_ITEM *parent, *prev = NULL;
1508 BOOL visible = FALSE;
1510 if (wineItem == TVI_ROOT)
1512 TRACE("TVI_ROOT\n");
1513 parent = infoPtr->root;
1514 newSelection = NULL;
1516 TREEVIEW_RemoveTree(infoPtr);
1520 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1523 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1524 parent = wineItem->parent;
1526 if (ISVISIBLE(wineItem))
1528 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1532 if (infoPtr->selectedItem != NULL
1533 && (wineItem == infoPtr->selectedItem
1534 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1536 if (wineItem->nextSibling)
1537 newSelection = wineItem->nextSibling;
1538 else if (wineItem->parent != infoPtr->root)
1539 newSelection = wineItem->parent;
1541 newSelection = wineItem->prevSibling;
1542 TRACE("newSelection = %p\n", newSelection);
1545 if (infoPtr->firstVisible == wineItem)
1547 if (wineItem->nextSibling)
1548 newFirstVisible = wineItem->nextSibling;
1549 else if (wineItem->prevSibling)
1550 newFirstVisible = wineItem->prevSibling;
1551 else if (wineItem->parent != infoPtr->root)
1552 newFirstVisible = wineItem->parent;
1553 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1556 newFirstVisible = infoPtr->firstVisible;
1558 TREEVIEW_RemoveItem(infoPtr, wineItem);
1561 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1562 if (!infoPtr->selectedItem && newSelection)
1564 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1565 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1568 /* Validate insertMark dropItem.
1569 * hotItem ??? - used for comparison only.
1571 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1572 infoPtr->insertMarkItem = 0;
1574 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1575 infoPtr->dropItem = 0;
1577 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1578 newFirstVisible = infoPtr->root->firstChild;
1580 TREEVIEW_VerifyTree(infoPtr);
1585 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1586 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1587 TREEVIEW_UpdateScrollBars(infoPtr);
1588 TREEVIEW_Invalidate(infoPtr, NULL);
1590 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1592 /* parent lost '+/-' - update it */
1593 TREEVIEW_Invalidate(infoPtr, parent);
1600 /* Get/Set Messages *********************************************************/
1602 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1605 infoPtr->bRedraw = TRUE;
1607 infoPtr->bRedraw = FALSE;
1613 TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1616 return infoPtr->uIndent;
1620 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1624 if (newIndent < MINIMUM_INDENT)
1625 newIndent = MINIMUM_INDENT;
1627 if (infoPtr->uIndent != newIndent)
1629 infoPtr->uIndent = newIndent;
1630 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1631 TREEVIEW_UpdateScrollBars(infoPtr);
1632 TREEVIEW_Invalidate(infoPtr, NULL);
1640 TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1643 return (LRESULT)infoPtr->hwndToolTip;
1647 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1652 prevToolTip = infoPtr->hwndToolTip;
1653 infoPtr->hwndToolTip = hwndTT;
1655 return (LRESULT)prevToolTip;
1659 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1661 BOOL rc = infoPtr->bNtfUnicode;
1662 infoPtr->bNtfUnicode = fUnicode;
1667 TREEVIEW_GetUnicodeFormat(TREEVIEW_INFO *infoPtr)
1669 return infoPtr->bNtfUnicode;
1673 TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1675 return infoPtr->uScrollTime;
1679 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1681 UINT uOldScrollTime = infoPtr->uScrollTime;
1683 infoPtr->uScrollTime = min(uScrollTime, 100);
1685 return uOldScrollTime;
1690 TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1696 case (WPARAM)TVSIL_NORMAL:
1697 return (LRESULT)infoPtr->himlNormal;
1699 case (WPARAM)TVSIL_STATE:
1700 return (LRESULT)infoPtr->himlState;
1707 #define TVHEIGHT_MIN 16
1708 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1710 /* Compute the natural height for items. */
1712 TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1716 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1719 /* Height is the maximum of:
1720 * 16 (a hack because our fonts are tiny), and
1721 * The text height + border & margin, and
1722 * The size of the normal image list
1724 GetTextMetricsW(hdc, &tm);
1725 SelectObject(hdc, hOldFont);
1728 height = TVHEIGHT_MIN;
1729 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1730 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1731 if (height < infoPtr->normalImageHeight)
1732 height = infoPtr->normalImageHeight;
1737 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1739 HIMAGELIST himlOld = 0;
1740 int oldWidth = infoPtr->normalImageWidth;
1741 int oldHeight = infoPtr->normalImageHeight;
1744 TRACE("%x,%p\n", wParam, himlNew);
1748 case (WPARAM)TVSIL_NORMAL:
1749 himlOld = infoPtr->himlNormal;
1750 infoPtr->himlNormal = himlNew;
1752 if (himlNew != NULL)
1753 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1754 &infoPtr->normalImageHeight);
1757 infoPtr->normalImageWidth = 0;
1758 infoPtr->normalImageHeight = 0;
1763 case (WPARAM)TVSIL_STATE:
1764 himlOld = infoPtr->himlState;
1765 infoPtr->himlState = himlNew;
1767 if (himlNew != NULL)
1768 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1769 &infoPtr->stateImageHeight);
1772 infoPtr->stateImageWidth = 0;
1773 infoPtr->stateImageHeight = 0;
1779 if (oldWidth != infoPtr->normalImageWidth ||
1780 oldHeight != infoPtr->normalImageHeight)
1782 BOOL bRecalcVisible = FALSE;
1784 if (oldHeight != infoPtr->normalImageHeight &&
1785 !infoPtr->bHeightSet)
1787 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1788 bRecalcVisible = TRUE;
1791 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1792 infoPtr->normalImageWidth != infoPtr->uIndent)
1794 infoPtr->uIndent = infoPtr->normalImageWidth;
1795 bRecalcVisible = TRUE;
1799 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1801 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1802 TREEVIEW_UpdateScrollBars(infoPtr);
1805 TREEVIEW_Invalidate(infoPtr, NULL);
1807 return (LRESULT)himlOld;
1811 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1813 INT prevHeight = infoPtr->uItemHeight;
1815 TRACE("%d\n", newHeight);
1816 if (newHeight == -1)
1818 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1819 infoPtr->bHeightSet = FALSE;
1823 infoPtr->uItemHeight = newHeight;
1824 infoPtr->bHeightSet = TRUE;
1827 /* Round down, unless we support odd ("non even") heights. */
1828 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1829 infoPtr->uItemHeight &= ~1;
1831 if (infoPtr->uItemHeight != prevHeight)
1833 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1834 TREEVIEW_UpdateScrollBars(infoPtr);
1835 TREEVIEW_Invalidate(infoPtr, NULL);
1842 TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1845 return infoPtr->uItemHeight;
1850 TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1852 TRACE("%p\n", infoPtr->hFont);
1853 return (LRESULT)infoPtr->hFont;
1858 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1862 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1868 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1870 UINT uHeight = infoPtr->uItemHeight;
1872 TRACE("%p %i\n", hFont, bRedraw);
1874 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1876 DeleteObject(infoPtr->hBoldFont);
1877 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1878 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1880 if (!infoPtr->bHeightSet)
1881 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1883 if (uHeight != infoPtr->uItemHeight)
1884 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1886 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1888 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1889 TREEVIEW_UpdateScrollBars(infoPtr);
1892 TREEVIEW_Invalidate(infoPtr, NULL);
1899 TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1902 return (LRESULT)infoPtr->clrLine;
1906 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1908 COLORREF prevColor = infoPtr->clrLine;
1911 infoPtr->clrLine = color;
1912 return (LRESULT)prevColor;
1917 TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1920 return (LRESULT)infoPtr->clrText;
1924 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1926 COLORREF prevColor = infoPtr->clrText;
1929 infoPtr->clrText = color;
1931 if (infoPtr->clrText != prevColor)
1932 TREEVIEW_Invalidate(infoPtr, NULL);
1934 return (LRESULT)prevColor;
1939 TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1942 return (LRESULT)infoPtr->clrBk;
1946 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1948 COLORREF prevColor = infoPtr->clrBk;
1951 infoPtr->clrBk = newColor;
1953 if (newColor != prevColor)
1954 TREEVIEW_Invalidate(infoPtr, NULL);
1956 return (LRESULT)prevColor;
1961 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1964 return (LRESULT)infoPtr->clrInsertMark;
1968 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1970 COLORREF prevColor = infoPtr->clrInsertMark;
1972 TRACE("%x\n", color);
1973 infoPtr->clrInsertMark = color;
1975 return (LRESULT)prevColor;
1980 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1982 TRACE("%d %p\n", wParam, item);
1984 if (!TREEVIEW_ValidItem(infoPtr, item))
1987 infoPtr->insertBeforeorAfter = wParam;
1988 infoPtr->insertMarkItem = item;
1990 TREEVIEW_Invalidate(infoPtr, NULL);
1996 /************************************************************************
1997 * Some serious braindamage here. lParam is a pointer to both the
1998 * input HTREEITEM and the output RECT.
2001 TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2003 TREEVIEW_ITEM *wineItem;
2004 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2008 * validate parameters
2014 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2018 * If wParam is TRUE return the text size otherwise return
2019 * the whole item size
2023 /* Windows does not send TVN_GETDISPINFO here. */
2025 lpRect->top = wineItem->rect.top;
2026 lpRect->bottom = wineItem->rect.bottom;
2028 lpRect->left = wineItem->textOffset;
2029 lpRect->right = wineItem->textOffset + wineItem->textWidth;
2033 *lpRect = wineItem->rect;
2036 TRACE("%s [L:%d R:%d T:%d B:%d]\n", fTextRect ? "text" : "item",
2037 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
2042 static inline LRESULT
2043 TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
2045 /* Suprise! This does not take integral height into account. */
2046 return infoPtr->clientHeight / infoPtr->uItemHeight;
2051 TREEVIEW_GetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2053 TREEVIEW_ITEM *wineItem;
2055 wineItem = tvItem->hItem;
2056 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2059 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2061 if (tvItem->mask & TVIF_CHILDREN)
2063 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2064 FIXME("I_CHILDRENCALLBACK not supported\n");
2065 tvItem->cChildren = wineItem->cChildren;
2068 if (tvItem->mask & TVIF_HANDLE)
2069 tvItem->hItem = wineItem;
2071 if (tvItem->mask & TVIF_IMAGE)
2072 tvItem->iImage = wineItem->iImage;
2074 if (tvItem->mask & TVIF_INTEGRAL)
2075 tvItem->iIntegral = wineItem->iIntegral;
2077 /* undocumented: windows ignores TVIF_PARAM and
2078 * * always sets lParam
2080 tvItem->lParam = wineItem->lParam;
2082 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2083 tvItem->iSelectedImage = wineItem->iSelectedImage;
2085 if (tvItem->mask & TVIF_STATE)
2086 /* Careful here - Windows ignores the stateMask when you get the state
2087 That contradicts the documentation, but makes more common sense, masking
2088 retrieval in this way seems overkill */
2089 tvItem->state = wineItem->state;
2091 if (tvItem->mask & TVIF_TEXT)
2095 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2097 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2098 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2102 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2107 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2109 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2110 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2114 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2115 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2119 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2120 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2125 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2126 * which is wrong. */
2128 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2130 TREEVIEW_ITEM *wineItem;
2131 TREEVIEW_ITEM originalItem;
2133 wineItem = tvItem->hItem;
2135 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2138 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2141 /* store the orignal item values */
2142 originalItem = *wineItem;
2144 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2147 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2148 if ((tvItem->mask & TVIF_TEXT
2149 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2150 && ISVISIBLE(wineItem))
2152 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2153 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2156 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2158 /* The refresh updates everything, but we can't wait until then. */
2159 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2161 /* if any of the item's values changed and it's not a callback, redraw the item */
2162 if (item_changed(&originalItem, wineItem, tvItem))
2164 if (tvItem->mask & TVIF_INTEGRAL)
2166 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2167 TREEVIEW_UpdateScrollBars(infoPtr);
2169 TREEVIEW_Invalidate(infoPtr, NULL);
2173 TREEVIEW_UpdateScrollBars(infoPtr);
2174 TREEVIEW_Invalidate(infoPtr, wineItem);
2183 TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2187 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2190 return (wineItem->state & mask);
2194 TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2196 TREEVIEW_ITEM *retval;
2200 /* handle all the global data here */
2203 case TVGN_CHILD: /* Special case: child of 0 is root */
2208 retval = infoPtr->root->firstChild;
2212 retval = infoPtr->selectedItem;
2215 case TVGN_FIRSTVISIBLE:
2216 retval = infoPtr->firstVisible;
2219 case TVGN_DROPHILITE:
2220 retval = infoPtr->dropItem;
2223 case TVGN_LASTVISIBLE:
2224 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2230 TRACE("flags:%x, returns %p\n", which, retval);
2231 return (LRESULT)retval;
2234 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2236 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2242 retval = wineItem->nextSibling;
2245 retval = wineItem->prevSibling;
2248 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2251 retval = wineItem->firstChild;
2253 case TVGN_NEXTVISIBLE:
2254 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2256 case TVGN_PREVIOUSVISIBLE:
2257 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2260 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2264 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2265 return (LRESULT)retval;
2270 TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2272 TRACE(" %d\n", infoPtr->uNumItems);
2273 return (LRESULT)infoPtr->uNumItems;
2277 TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2279 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2281 static const unsigned int state_table[] = { 0, 2, 1 };
2285 state = STATEIMAGEINDEX(item->state);
2286 TRACE("state:%x\n", state);
2287 item->state &= ~TVIS_STATEIMAGEMASK;
2290 state = state_table[state];
2292 item->state |= INDEXTOSTATEIMAGEMASK(state);
2294 TRACE("state:%x\n", state);
2295 TREEVIEW_Invalidate(infoPtr, item);
2300 /* Painting *************************************************************/
2302 /* Draw the lines and expand button for an item. Also draws one section
2303 * of the line from item's parent to item's parent's next sibling. */
2305 TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2307 LONG centerx, centery;
2308 BOOL lar = ((infoPtr->dwStyle
2309 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2313 if (!lar && item->iLevel == 0)
2316 hbr = CreateSolidBrush(infoPtr->clrBk);
2317 hbrOld = SelectObject(hdc, hbr);
2319 centerx = (item->linesOffset + item->stateOffset) / 2;
2320 centery = (item->rect.top + item->rect.bottom) / 2;
2322 if (infoPtr->dwStyle & TVS_HASLINES)
2324 HPEN hOldPen, hNewPen;
2329 * Get a dotted grey pen
2331 lb.lbStyle = BS_SOLID;
2332 lb.lbColor = infoPtr->clrLine;
2333 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2334 hOldPen = SelectObject(hdc, hNewPen);
2336 MoveToEx(hdc, item->stateOffset, centery, NULL);
2337 LineTo(hdc, centerx - 1, centery);
2339 if (item->prevSibling || item->parent != infoPtr->root)
2341 MoveToEx(hdc, centerx, item->rect.top, NULL);
2342 LineTo(hdc, centerx, centery);
2345 if (item->nextSibling)
2347 MoveToEx(hdc, centerx, centery, NULL);
2348 LineTo(hdc, centerx, item->rect.bottom + 1);
2351 /* Draw the line from our parent to its next sibling. */
2352 parent = item->parent;
2353 while (parent != infoPtr->root)
2355 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2357 if (parent->nextSibling
2358 /* skip top-levels unless TVS_LINESATROOT */
2359 && parent->stateOffset > parent->linesOffset)
2361 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2362 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2365 parent = parent->parent;
2368 SelectObject(hdc, hOldPen);
2369 DeleteObject(hNewPen);
2373 * Display the (+/-) signs
2376 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2378 if (item->cChildren)
2380 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2383 RECT glyphRect = item->rect;
2384 glyphRect.left = item->linesOffset;
2385 glyphRect.right = item->stateOffset;
2386 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2387 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2392 LONG height = item->rect.bottom - item->rect.top;
2393 LONG width = item->stateOffset - item->linesOffset;
2394 LONG rectsize = min(height, width) / 4;
2395 /* plussize = ceil(rectsize * 3/4) */
2396 LONG plussize = (rectsize + 1) * 3 / 4;
2398 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2399 HPEN hOldPen = SelectObject(hdc, hNewPen);
2401 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2402 centerx + rectsize + 2, centery + rectsize + 2);
2404 SelectObject(hdc, hOldPen);
2405 DeleteObject(hNewPen);
2407 if (height < 18 || width < 18)
2409 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2410 LineTo(hdc, centerx + plussize, centery);
2412 if (!(item->state & TVIS_EXPANDED))
2414 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2415 LineTo(hdc, centerx, centery + plussize);
2420 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2421 centerx + plussize, centery + 2);
2423 if (!(item->state & TVIS_EXPANDED))
2425 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2426 centerx + 2, centery + plussize);
2427 SetPixel(hdc, centerx - 1, centery, infoPtr->clrBk);
2428 SetPixel(hdc, centerx + 1, centery, infoPtr->clrBk);
2434 SelectObject(hdc, hbrOld);
2439 TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2443 COLORREF oldTextColor, oldTextBkColor;
2445 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2446 NMTVCUSTOMDRAW nmcdhdr;
2448 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2450 /* - If item is drop target or it is selected and window is in focus -
2451 * use blue background (COLOR_HIGHLIGHT).
2452 * - If item is selected, window is not in focus, but it has style
2453 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2454 * - Otherwise - use background color
2456 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2457 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2458 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2460 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2462 nmcdhdr.clrTextBk = GetSysColor(COLOR_HIGHLIGHT);
2463 nmcdhdr.clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
2467 nmcdhdr.clrTextBk = GetSysColor(COLOR_BTNFACE);
2468 if (infoPtr->clrText == -1)
2469 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2471 nmcdhdr.clrText = infoPtr->clrText;
2476 nmcdhdr.clrTextBk = infoPtr->clrBk;
2477 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2478 nmcdhdr.clrText = comctl32_color.clrHighlight;
2479 else if (infoPtr->clrText == -1)
2480 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2482 nmcdhdr.clrText = infoPtr->clrText;
2485 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2487 /* The custom draw handler can query the text rectangle,
2489 /* should already be known, set to 0 when changed */
2490 if (!wineItem->textWidth)
2491 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2495 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2497 cditem = TREEVIEW_SendCustomDrawItemNotify
2498 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2499 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2501 if (cditem & CDRF_SKIPDEFAULT)
2503 SelectObject(hdc, hOldFont);
2508 if (cditem & CDRF_NEWFONT)
2509 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2511 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2513 /* Set colors. Custom draw handler can change these so we do this after it. */
2514 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2515 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2517 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2520 * Display the images associated with this item
2525 /* State images are displayed to the left of the Normal image
2526 * image number is in state; zero should be `display no image'.
2528 imageIndex = STATEIMAGEINDEX(wineItem->state);
2530 if (infoPtr->himlState && imageIndex)
2532 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2533 wineItem->stateOffset,
2534 centery - infoPtr->stateImageHeight / 2,
2538 /* Now, draw the normal image; can be either selected or
2539 * non-selected image.
2542 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2544 /* The item is currently selected */
2545 imageIndex = wineItem->iSelectedImage;
2549 /* The item is not selected */
2550 imageIndex = wineItem->iImage;
2553 if (infoPtr->himlNormal)
2555 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2557 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2558 wineItem->imageOffset,
2559 centery - infoPtr->normalImageHeight / 2,
2560 ILD_NORMAL | ovlIdx);
2566 * Display the text associated with this item
2569 /* Don't paint item's text if it's being edited */
2570 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2572 if (wineItem->pszText)
2576 rcText.top = wineItem->rect.top;
2577 rcText.bottom = wineItem->rect.bottom;
2578 rcText.left = wineItem->textOffset;
2579 rcText.right = rcText.left + wineItem->textWidth + 4;
2581 TRACE("drawing text %s at (%d,%d)-(%d,%d)\n",
2582 debugstr_w(wineItem->pszText),
2583 rcText.left, rcText.top, rcText.right, rcText.bottom);
2586 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2587 ETO_CLIPPED | ETO_OPAQUE,
2590 lstrlenW(wineItem->pszText),
2593 /* Draw the box around the selected item */
2594 if ((wineItem == infoPtr->selectedItem) && inFocus)
2596 DrawFocusRect(hdc,&rcText);
2602 /* Draw insertion mark if necessary */
2604 if (infoPtr->insertMarkItem)
2605 TRACE("item:%d,mark:%p\n",
2606 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2607 infoPtr->insertMarkItem);
2609 if (wineItem == infoPtr->insertMarkItem)
2611 HPEN hNewPen, hOldPen;
2615 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2616 hOldPen = SelectObject(hdc, hNewPen);
2618 if (infoPtr->insertBeforeorAfter)
2619 offset = wineItem->rect.bottom - 1;
2621 offset = wineItem->rect.top + 1;
2623 left = wineItem->textOffset - 2;
2624 right = wineItem->textOffset + wineItem->textWidth + 2;
2626 MoveToEx(hdc, left, offset - 3, NULL);
2627 LineTo(hdc, left, offset + 4);
2629 MoveToEx(hdc, left, offset, NULL);
2630 LineTo(hdc, right + 1, offset);
2632 MoveToEx(hdc, right, offset + 3, NULL);
2633 LineTo(hdc, right, offset - 4);
2635 SelectObject(hdc, hOldPen);
2636 DeleteObject(hNewPen);
2639 if (cditem & CDRF_NOTIFYPOSTPAINT)
2641 cditem = TREEVIEW_SendCustomDrawItemNotify
2642 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2643 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2646 /* Restore the hdc state */
2647 SetTextColor(hdc, oldTextColor);
2648 SetBkColor(hdc, oldTextBkColor);
2649 SelectObject(hdc, hOldFont);
2652 /* Computes treeHeight and treeWidth and updates the scroll bars.
2655 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2657 TREEVIEW_ITEM *wineItem;
2658 HWND hwnd = infoPtr->hwnd;
2662 LONG scrollX = infoPtr->scrollX;
2664 infoPtr->treeWidth = 0;
2665 infoPtr->treeHeight = 0;
2667 /* We iterate through all visible items in order to get the tree height
2669 wineItem = infoPtr->root->firstChild;
2671 while (wineItem != NULL)
2673 if (ISVISIBLE(wineItem))
2675 /* actually we draw text at textOffset + 2 */
2676 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2677 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2679 /* This is scroll-adjusted, but we fix this below. */
2680 infoPtr->treeHeight = wineItem->rect.bottom;
2683 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2686 /* Fix the scroll adjusted treeHeight and treeWidth. */
2687 if (infoPtr->root->firstChild)
2688 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2690 infoPtr->treeWidth += infoPtr->scrollX;
2692 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2694 /* Adding one scroll bar may take up enough space that it forces us
2695 * to add the other as well. */
2696 if (infoPtr->treeHeight > infoPtr->clientHeight)
2700 if (infoPtr->treeWidth
2701 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2704 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2707 if (!vert && horz && infoPtr->treeHeight
2708 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2711 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2713 si.cbSize = sizeof(SCROLLINFO);
2714 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2719 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2720 if ( si.nPage && NULL != infoPtr->firstVisible)
2722 si.nPos = infoPtr->firstVisible->visibleOrder;
2723 si.nMax = infoPtr->maxVisibleOrder - 1;
2725 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2727 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2728 ShowScrollBar(hwnd, SB_VERT, TRUE);
2729 infoPtr->uInternalStatus |= TV_VSCROLL;
2733 if (infoPtr->uInternalStatus & TV_VSCROLL)
2734 ShowScrollBar(hwnd, SB_VERT, FALSE);
2735 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2740 if (infoPtr->uInternalStatus & TV_VSCROLL)
2741 ShowScrollBar(hwnd, SB_VERT, FALSE);
2742 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2747 si.nPage = infoPtr->clientWidth;
2748 si.nPos = infoPtr->scrollX;
2749 si.nMax = infoPtr->treeWidth - 1;
2751 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2753 si.nPos = si.nMax - max( si.nPage-1, 0 );
2757 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2758 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2759 infoPtr->uInternalStatus |= TV_HSCROLL;
2761 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2762 TREEVIEW_HScroll(infoPtr,
2763 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2767 if (infoPtr->uInternalStatus & TV_HSCROLL)
2768 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2769 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2772 if (infoPtr->scrollX != 0)
2774 TREEVIEW_HScroll(infoPtr,
2775 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2780 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2783 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2785 TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2787 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2790 GetClientRect(infoPtr->hwnd, &rect);
2791 FillRect(hDC, &rect, hBrush);
2792 DeleteObject(hBrush);
2798 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2800 HWND hwnd = infoPtr->hwnd;
2802 TREEVIEW_ITEM *wineItem;
2804 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2806 TRACE("empty window\n");
2810 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2813 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2815 ReleaseDC(hwnd, hdc);
2819 for (wineItem = infoPtr->root->firstChild;
2821 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2823 if (ISVISIBLE(wineItem))
2825 /* Avoid unneeded calculations */
2826 if (wineItem->rect.top > rect.bottom)
2828 if (wineItem->rect.bottom < rect.top)
2831 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2835 TREEVIEW_UpdateScrollBars(infoPtr);
2837 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2839 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2843 TREEVIEW_Invalidate(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2846 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2848 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2852 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2863 GetClientRect(infoPtr->hwnd, &rc);
2864 TREEVIEW_EraseBackground(infoPtr, hdc);
2868 hdc = BeginPaint(infoPtr->hwnd, &ps);
2872 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2873 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2876 EndPaint(infoPtr->hwnd, &ps);
2882 /* Sorting **************************************************************/
2884 /***************************************************************************
2885 * Forward the DPA local callback to the treeview owner callback
2888 TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2890 /* Forward the call to the client-defined callback */
2891 return pCallBackSort->lpfnCompare(first->lParam,
2893 pCallBackSort->lParam);
2896 /***************************************************************************
2897 * Treeview native sort routine: sort on item text.
2900 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2901 TREEVIEW_INFO *infoPtr)
2903 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2904 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2906 if(first->pszText && second->pszText)
2907 return lstrcmpiW(first->pszText, second->pszText);
2908 else if(first->pszText)
2910 else if(second->pszText)
2916 /* Returns the number of physical children belonging to item. */
2918 TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2923 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2929 /* Returns a DPA containing a pointer to each physical child of item in
2930 * sibling order. If item has no children, an empty DPA is returned. */
2932 TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2934 HTREEITEM child = item->firstChild;
2936 HDPA list = DPA_Create(8);
2937 if (list == 0) return NULL;
2939 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2941 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2951 /***************************************************************************
2952 * Setup the treeview structure with regards of the sort method
2953 * and sort the children of the TV item specified in lParam
2954 * fRecurse: currently unused. Should be zero.
2955 * parent: if pSort!=NULL, should equal pSort->hParent.
2956 * otherwise, item which child items are to be sorted.
2957 * pSort: sort method info. if NULL, sort on item text.
2958 * if non-NULL, sort on item's lParam content, and let the
2959 * application decide what that means. See also TVM_SORTCHILDRENCB.
2963 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2967 PFNDPACOMPARE pfnCompare;
2970 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2971 if (parent == TVI_ROOT || parent == NULL)
2972 parent = infoPtr->root;
2974 /* Check for a valid handle to the parent item */
2975 if (!TREEVIEW_ValidItem(infoPtr, parent))
2977 ERR("invalid item hParent=%p\n", parent);
2983 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2984 lpCompare = (LPARAM)pSort;
2988 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2989 lpCompare = (LPARAM)infoPtr;
2992 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2994 /* Make sure there is something to sort */
2997 /* TREEVIEW_ITEM rechaining */
3000 HTREEITEM nextItem = 0;
3001 HTREEITEM prevItem = 0;
3003 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
3005 if (sortList == NULL)
3008 /* let DPA sort the list */
3009 DPA_Sort(sortList, pfnCompare, lpCompare);
3011 /* The order of DPA entries has been changed, so fixup the
3012 * nextSibling and prevSibling pointers. */
3014 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
3015 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
3017 /* link the two current item toghether */
3018 item->nextSibling = nextItem;
3019 nextItem->prevSibling = item;
3021 if (prevItem == NULL)
3023 /* this is the first item, update the parent */
3024 parent->firstChild = item;
3025 item->prevSibling = NULL;
3029 /* fix the back chaining */
3030 item->prevSibling = prevItem;
3033 /* get ready for the next one */
3038 /* the last item is pointed to by item and never has a sibling */
3039 item->nextSibling = NULL;
3040 parent->lastChild = item;
3042 DPA_Destroy(sortList);
3044 TREEVIEW_VerifyTree(infoPtr);
3046 if (parent->state & TVIS_EXPANDED)
3048 int visOrder = infoPtr->firstVisible->visibleOrder;
3050 if (parent == infoPtr->root)
3051 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3053 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3055 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3057 TREEVIEW_ITEM *item;
3059 for (item = infoPtr->root->firstChild; item != NULL;
3060 item = TREEVIEW_GetNextListItem(infoPtr, item))
3062 if (item->visibleOrder == visOrder)
3066 if (!item) item = parent->firstChild;
3067 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3070 TREEVIEW_Invalidate(infoPtr, NULL);
3079 /***************************************************************************
3080 * Setup the treeview structure with regards of the sort method
3081 * and sort the children of the TV item specified in lParam
3084 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3086 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3090 /***************************************************************************
3091 * Sort the children of the TV item specified in lParam.
3094 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3096 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3100 /* Expansion/Collapse ***************************************************/
3103 TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3106 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3107 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3108 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3113 TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3116 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3117 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3118 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3123 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3124 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3126 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3127 BOOL bRemoveChildren, BOOL bUser)
3129 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3130 BOOL bSetSelection, bSetFirstVisible;
3132 LONG scrollDist = 0;
3133 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3135 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3137 if (!(wineItem->state & TVIS_EXPANDED))
3140 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3141 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3143 if (wineItem->firstChild == NULL)
3146 wineItem->state &= ~TVIS_EXPANDED;
3148 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3149 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3151 bSetSelection = (infoPtr->selectedItem != NULL
3152 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3154 bSetFirstVisible = (infoPtr->firstVisible != NULL
3155 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3160 if (tmpItem->nextSibling)
3162 nextItem = tmpItem->nextSibling;
3165 tmpItem = tmpItem->parent;
3169 scrollDist = nextItem->rect.top;
3171 if (bRemoveChildren)
3173 INT old_cChildren = wineItem->cChildren;
3174 TRACE("TVE_COLLAPSERESET\n");
3175 wineItem->state &= ~TVIS_EXPANDEDONCE;
3176 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3177 wineItem->cChildren = old_cChildren;
3180 if (wineItem->firstChild)
3182 TREEVIEW_ITEM *item, *sibling;
3184 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3186 for (item = wineItem->firstChild; item != sibling;
3187 item = TREEVIEW_GetNextListItem(infoPtr, item))
3189 item->visibleOrder = -1;
3193 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3196 scrollDist = -(scrollDist - nextItem->rect.top);
3200 /* Don't call DoSelectItem, it sends notifications. */
3201 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3202 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3203 wineItem->state |= TVIS_SELECTED;
3204 infoPtr->selectedItem = wineItem;
3207 TREEVIEW_UpdateScrollBars(infoPtr);
3209 scrollRect.left = 0;
3210 scrollRect.right = infoPtr->clientWidth;
3211 scrollRect.bottom = infoPtr->clientHeight;
3215 scrollRect.top = nextItem->rect.top;
3217 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3218 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3219 TREEVIEW_Invalidate(infoPtr, wineItem);
3221 scrollRect.top = wineItem->rect.top;
3222 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3225 TREEVIEW_SetFirstVisible(infoPtr,
3226 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3233 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3234 BOOL bExpandPartial, BOOL bUser)
3237 LONG orgNextTop = 0;
3239 TREEVIEW_ITEM *nextItem, *tmpItem;
3243 if (wineItem->state & TVIS_EXPANDED)
3246 tmpItem = wineItem; nextItem = NULL;
3249 if (tmpItem->nextSibling)
3251 nextItem = tmpItem->nextSibling;
3254 tmpItem = tmpItem->parent;
3258 orgNextTop = nextItem->rect.top;
3260 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3262 if (bUser || ((wineItem->cChildren != 0) &&
3263 !(wineItem->state & TVIS_EXPANDEDONCE)))
3265 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3267 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3271 if (!wineItem->firstChild)
3274 wineItem->state |= TVIS_EXPANDED;
3275 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3276 wineItem->state |= TVIS_EXPANDEDONCE;
3280 if (!wineItem->firstChild)
3283 /* this item has already been expanded */
3284 wineItem->state |= TVIS_EXPANDED;
3288 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3290 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3291 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3292 TREEVIEW_UpdateScrollBars(infoPtr);
3294 scrollRect.left = 0;
3295 scrollRect.bottom = infoPtr->treeHeight;
3296 scrollRect.right = infoPtr->clientWidth;
3299 scrollDist = nextItem->rect.top - orgNextTop;
3300 scrollRect.top = orgNextTop;
3302 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3303 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3304 TREEVIEW_Invalidate (infoPtr, wineItem);
3306 scrollRect.top = wineItem->rect.top;
3307 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3310 /* Scroll up so that as many children as possible are visible.
3311 * This fails when expanding causes an HScroll bar to appear, but we
3312 * don't know that yet, so the last item is obscured. */
3313 if (wineItem->firstChild != NULL)
3315 int nChildren = wineItem->lastChild->visibleOrder
3316 - wineItem->firstChild->visibleOrder + 1;
3318 int visible_pos = wineItem->visibleOrder
3319 - infoPtr->firstVisible->visibleOrder;
3321 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3323 if (visible_pos > 0 && nChildren > rows_below)
3325 int scroll = nChildren - rows_below;
3327 if (scroll > visible_pos)
3328 scroll = visible_pos;
3332 TREEVIEW_ITEM *newFirstVisible
3333 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3337 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3346 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3350 if (wineItem->state & TVIS_EXPANDED)
3351 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3353 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3357 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3359 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3361 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3363 if (TREEVIEW_HasChildren(infoPtr, item))
3364 TREEVIEW_ExpandAll(infoPtr, item);
3368 /* Note:If the specified item is the child of a collapsed parent item,
3369 the parent's list of child items is (recursively) expanded to reveal the
3370 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3371 know if it also applies here.
3375 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3377 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3380 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3381 TREEVIEW_ItemName(wineItem), flag,
3382 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3384 switch (flag & TVE_TOGGLE)
3387 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3391 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3395 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3402 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3406 /* Hit-Testing **********************************************************/
3408 static TREEVIEW_ITEM *
3409 TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3411 TREEVIEW_ITEM *wineItem;
3414 if (!infoPtr->firstVisible)
3417 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3419 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3420 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3422 if (row >= wineItem->visibleOrder
3423 && row < wineItem->visibleOrder + wineItem->iIntegral)
3431 TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3433 TREEVIEW_ITEM *wineItem;
3439 GetClientRect(infoPtr->hwnd, &rect);
3446 status |= TVHT_TOLEFT;
3448 else if (x > rect.right)
3450 status |= TVHT_TORIGHT;
3455 status |= TVHT_ABOVE;
3457 else if (y > rect.bottom)
3459 status |= TVHT_BELOW;
3464 lpht->flags = status;
3465 return (LRESULT)(HTREEITEM)NULL;
3468 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3471 lpht->flags = TVHT_NOWHERE;
3472 return (LRESULT)(HTREEITEM)NULL;
3475 if (x >= wineItem->textOffset + wineItem->textWidth)
3477 lpht->flags = TVHT_ONITEMRIGHT;
3479 else if (x >= wineItem->textOffset)
3481 lpht->flags = TVHT_ONITEMLABEL;
3483 else if (x >= wineItem->imageOffset)
3485 lpht->flags = TVHT_ONITEMICON;
3487 else if (x >= wineItem->stateOffset)
3489 lpht->flags = TVHT_ONITEMSTATEICON;
3491 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3493 lpht->flags = TVHT_ONITEMBUTTON;
3497 lpht->flags = TVHT_ONITEMINDENT;
3500 lpht->hItem = wineItem;
3501 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3503 return (LRESULT)wineItem;
3506 /* Item Label Editing ***************************************************/
3509 TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3511 return (LRESULT)infoPtr->hwndEdit;
3514 static LRESULT CALLBACK
3515 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3517 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3518 BOOL bCancel = FALSE;
3524 TRACE("WM_PAINT start\n");
3525 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3527 TRACE("WM_PAINT done\n");
3531 if (infoPtr->bIgnoreEditKillFocus)
3536 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3539 if (wParam == (WPARAM)VK_ESCAPE)
3544 else if (wParam == (WPARAM)VK_RETURN)
3551 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3554 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3555 /* eg. Using a messagebox */
3557 infoPtr->bIgnoreEditKillFocus = TRUE;
3558 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3559 infoPtr->bIgnoreEditKillFocus = FALSE;
3565 /* should handle edit control messages here */
3568 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3570 TRACE("%x %ld\n", wParam, lParam);
3572 switch (HIWORD(wParam))
3577 * Adjust the edit window size
3580 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3581 HDC hdc = GetDC(infoPtr->hwndEdit);
3584 HFONT hFont, hOldFont = 0;
3586 infoPtr->bLabelChanged = TRUE;
3588 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer));
3590 /* Select font to get the right dimension of the string */
3591 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3595 hOldFont = SelectObject(hdc, hFont);
3598 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3600 TEXTMETRICW textMetric;
3602 /* Add Extra spacing for the next character */
3603 GetTextMetricsW(hdc, &textMetric);
3604 sz.cx += (textMetric.tmMaxCharWidth * 2);
3606 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3608 infoPtr->clientWidth - editItem->textOffset + 2);
3610 SetWindowPos(infoPtr->hwndEdit,
3615 editItem->rect.bottom - editItem->rect.top + 3,
3616 SWP_NOMOVE | SWP_DRAWFRAME);
3621 SelectObject(hdc, hOldFont);
3624 ReleaseDC(infoPtr->hwnd, hdc);
3629 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3636 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3638 HWND hwnd = infoPtr->hwnd;
3641 TREEVIEW_ITEM *editItem = hItem;
3642 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3645 TEXTMETRICW textMetric;
3646 static const WCHAR EditW[] = {'E','d','i','t',0};
3648 TRACE("%p %p\n", hwnd, hItem);
3649 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3652 if (infoPtr->hwndEdit)
3653 return infoPtr->hwndEdit;
3655 infoPtr->bLabelChanged = FALSE;
3657 /* Make sure that edit item is selected */
3658 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3659 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3661 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3664 /* Select the font to get appropriate metric dimensions */
3665 if (infoPtr->hFont != 0)
3667 hOldFont = SelectObject(hdc, infoPtr->hFont);
3670 /* Get string length in pixels */
3671 GetTextExtentPoint32W(hdc, editItem->pszText, strlenW(editItem->pszText),
3674 /* Add Extra spacing for the next character */
3675 GetTextMetricsW(hdc, &textMetric);
3676 sz.cx += (textMetric.tmMaxCharWidth * 2);
3678 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3679 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3681 if (infoPtr->hFont != 0)
3683 SelectObject(hdc, hOldFont);
3686 ReleaseDC(hwnd, hdc);
3687 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3690 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3691 WS_CLIPSIBLINGS | ES_WANTRETURN |
3692 ES_LEFT, editItem->textOffset - 2,
3693 editItem->rect.top - 1, sz.cx + 3,
3694 editItem->rect.bottom -
3695 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3696 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3698 infoPtr->hwndEdit = hwndEdit;
3700 /* Get a 2D border. */
3701 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3702 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3703 SetWindowLongW(hwndEdit, GWL_STYLE,
3704 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3706 SendMessageW(hwndEdit, WM_SETFONT,
3707 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3709 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3711 TREEVIEW_Edit_SubclassProc);
3713 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3715 DestroyWindow(hwndEdit);
3716 infoPtr->hwndEdit = 0;
3720 infoPtr->selectedItem = hItem;
3721 SetWindowTextW(hwndEdit, editItem->pszText);
3723 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3724 ShowWindow(hwndEdit, SW_SHOW);
3731 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3733 HWND hwnd = infoPtr->hwnd;
3734 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3737 WCHAR tmpText[1024] = { '\0' };
3738 WCHAR *newText = tmpText;
3741 if (!infoPtr->hwndEdit)
3744 tvdi.hdr.hwndFrom = hwnd;
3745 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3746 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3748 tvdi.item.hItem = editedItem;
3749 tvdi.item.state = editedItem->state;
3750 tvdi.item.lParam = editedItem->lParam;
3754 if (!infoPtr->bNtfUnicode)
3755 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3757 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3759 if (iLength >= 1023)
3761 ERR("Insufficient space to retrieve new item label\n");
3764 tvdi.item.mask = TVIF_TEXT;
3765 tvdi.item.pszText = tmpText;
3766 tvdi.item.cchTextMax = iLength + 1;
3770 tvdi.item.pszText = NULL;
3771 tvdi.item.cchTextMax = 0;
3774 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
3775 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3777 if (!bCancel && bCommit) /* Apply the changes */
3779 if (!infoPtr->bNtfUnicode)
3781 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3782 newText = Alloc(len * sizeof(WCHAR));
3783 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3787 if (strcmpW(newText, editedItem->pszText) != 0)
3789 if (NULL == ReAlloc(editedItem->pszText, iLength + 1))
3791 ERR("OutOfMemory, cannot allocate space for label\n");
3792 DestroyWindow(infoPtr->hwndEdit);
3793 infoPtr->hwndEdit = 0;
3798 editedItem->cchTextMax = iLength + 1;
3799 strcpyW(editedItem->pszText, newText);
3802 if(newText != tmpText) Free(newText);
3805 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3806 DestroyWindow(infoPtr->hwndEdit);
3807 infoPtr->hwndEdit = 0;
3812 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3814 if (wParam != TV_EDIT_TIMER)
3816 ERR("got unknown timer\n");
3820 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3821 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3823 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3829 /* Mouse Tracking/Drag **************************************************/
3831 /***************************************************************************
3832 * This is quite unusual piece of code, but that's how it's implemented in
3836 TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3838 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3839 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3843 r.top = pt.y - cyDrag;
3844 r.left = pt.x - cxDrag;
3845 r.bottom = pt.y + cyDrag;
3846 r.right = pt.x + cxDrag;
3848 SetCapture(infoPtr->hwnd);
3852 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3854 if (msg.message == WM_MOUSEMOVE)
3856 pt.x = (short)LOWORD(msg.lParam);
3857 pt.y = (short)HIWORD(msg.lParam);
3858 if (PtInRect(&r, pt))
3866 else if (msg.message >= WM_LBUTTONDOWN &&
3867 msg.message <= WM_RBUTTONDBLCLK)
3869 if (msg.message == WM_RBUTTONUP)
3870 TREEVIEW_RButtonUp(infoPtr, &pt);
3874 DispatchMessageW(&msg);
3877 if (GetCapture() != infoPtr->hwnd)
3887 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3889 TREEVIEW_ITEM *wineItem;
3893 SetFocus(infoPtr->hwnd);
3895 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3897 /* If there is pending 'edit label' event - kill it now */
3898 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3901 hit.pt.x = (short)LOWORD(lParam);
3902 hit.pt.y = (short)HIWORD(lParam);
3904 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3907 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3909 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3913 case TVHT_ONITEMRIGHT:
3914 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3917 case TVHT_ONITEMINDENT:
3918 if (!(infoPtr->dwStyle & TVS_HASLINES))
3924 int level = hit.pt.x / infoPtr->uIndent;
3925 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3927 while (wineItem->iLevel > level)
3929 wineItem = wineItem->parent;
3935 case TVHT_ONITEMLABEL:
3936 case TVHT_ONITEMICON:
3937 case TVHT_ONITEMBUTTON:
3938 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3941 case TVHT_ONITEMSTATEICON:
3942 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3943 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3945 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3954 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3956 HWND hwnd = infoPtr->hwnd;
3958 BOOL bTrack, bDoLabelEdit;
3961 /* If Edit control is active - kill it and return.
3962 * The best way to do it is to set focus to itself.
3963 * Edit control subclassed procedure will automatically call
3966 if (infoPtr->hwndEdit)
3972 ht.pt.x = (short)LOWORD(lParam);
3973 ht.pt.y = (short)HIWORD(lParam);
3975 TREEVIEW_HitTest(infoPtr, &ht);
3976 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3978 /* update focusedItem and redraw both items */
3979 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3981 infoPtr->focusedItem = ht.hItem;
3982 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3984 if(infoPtr->selectedItem)
3985 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3988 bTrack = (ht.flags & TVHT_ONITEM)
3989 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3992 * If the style allows editing and the node is already selected
3993 * and the click occurred on the item label...
3995 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
3996 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
3998 /* Send NM_CLICK right away */
4000 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4003 if (ht.flags & TVHT_ONITEMBUTTON)
4005 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4009 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4010 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4012 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4013 infoPtr->dropItem = ht.hItem;
4015 /* clean up focusedItem as we dragged and won't select this item */
4016 if(infoPtr->focusedItem)
4018 /* refresh the item that was focused */
4019 tempItem = infoPtr->focusedItem;
4020 infoPtr->focusedItem = 0;
4021 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
4023 /* refresh the selected item to return the filled background */
4024 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
4031 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4036 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4037 KillTimer(hwnd, TV_EDIT_TIMER);
4039 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4040 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4042 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4045 * if we are TVS_SINGLEEXPAND then we want this single click to
4046 * do a bunch of things.
4048 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4049 (infoPtr->hwndEdit == 0))
4051 TREEVIEW_ITEM *SelItem;
4054 * Send the notification
4056 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4059 * Close the previous selection all the way to the root
4060 * as long as the new selection is not a child
4062 if((infoPtr->selectedItem)
4063 && (infoPtr->selectedItem != ht.hItem))
4065 BOOL closeit = TRUE;
4068 /* determine if the hitItem is a child of the currently selected item */
4069 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4071 closeit = (SelItem != infoPtr->selectedItem);
4072 SelItem = SelItem->parent;
4077 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4078 SelItem = infoPtr->selectedItem;
4080 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4082 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4083 SelItem = SelItem->parent;
4089 * Expand the current item
4091 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4094 /* Select the current item */
4095 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4097 else if (ht.flags & TVHT_ONITEMSTATEICON)
4099 /* TVS_CHECKBOXES requires us to toggle the current state */
4100 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4101 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4111 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4115 if (infoPtr->hwndEdit)
4117 SetFocus(infoPtr->hwnd);
4121 ht.pt.x = (short)LOWORD(lParam);
4122 ht.pt.y = (short)HIWORD(lParam);
4124 TREEVIEW_HitTest(infoPtr, &ht);
4126 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4130 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4131 infoPtr->dropItem = ht.hItem;
4136 SetFocus(infoPtr->hwnd);
4137 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4144 TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
4151 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4153 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4157 HBITMAP hbmp, hOldbmp;
4164 if (!(infoPtr->himlNormal))
4167 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4170 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4172 hwtop = GetDesktopWindow();
4173 htopdc = GetDC(hwtop);
4174 hdc = CreateCompatibleDC(htopdc);
4176 hOldFont = SelectObject(hdc, infoPtr->hFont);
4177 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4179 TRACE("%d %d %s %d\n", size.cx, size.cy, debugstr_w(dragItem->pszText),
4180 strlenW(dragItem->pszText));
4181 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4182 hOldbmp = SelectObject(hdc, hbmp);
4184 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4189 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4190 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4194 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4195 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4198 /* draw item text */
4200 SetRect(&rc, cx, 0, size.cx, size.cy);
4201 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4203 SelectObject(hdc, hOldFont);
4204 SelectObject(hdc, hOldbmp);
4206 ImageList_Add(infoPtr->dragList, hbmp, 0);
4210 ReleaseDC(hwtop, htopdc);
4212 return (LRESULT)infoPtr->dragList;
4215 /* Selection ************************************************************/
4218 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4221 TREEVIEW_ITEM *prevSelect;
4224 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4226 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4227 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4228 newSelect ? newSelect->state : 0);
4230 /* reset and redraw focusedItem if focusedItem was set so we don't */
4231 /* have to worry about the previously focused item when we set a new one */
4232 if(infoPtr->focusedItem)
4234 rcFocused = (infoPtr->focusedItem)->rect;
4235 infoPtr->focusedItem = 0;
4236 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4242 prevSelect = infoPtr->selectedItem;
4244 if (prevSelect == newSelect) {
4245 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4249 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4252 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4258 prevSelect->state &= ~TVIS_SELECTED;
4260 newSelect->state |= TVIS_SELECTED;
4262 infoPtr->selectedItem = newSelect;
4264 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4267 TREEVIEW_Invalidate(infoPtr, prevSelect);
4269 TREEVIEW_Invalidate(infoPtr, newSelect);
4271 TREEVIEW_SendTreeviewNotify(infoPtr,
4274 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4279 case TVGN_DROPHILITE:
4280 prevSelect = infoPtr->dropItem;
4283 prevSelect->state &= ~TVIS_DROPHILITED;
4285 infoPtr->dropItem = newSelect;
4288 newSelect->state |= TVIS_DROPHILITED;
4290 TREEVIEW_Invalidate(infoPtr, prevSelect);
4291 TREEVIEW_Invalidate(infoPtr, newSelect);
4294 case TVGN_FIRSTVISIBLE:
4295 if (newSelect != NULL)
4297 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4298 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4299 TREEVIEW_Invalidate(infoPtr, NULL);
4304 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4308 /* FIXME: handle NM_KILLFOCUS etc */
4310 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4312 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4315 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4317 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4323 /*************************************************************************
4324 * TREEVIEW_ProcessLetterKeys
4326 * Processes keyboard messages generated by pressing the letter keys
4328 * What this does is perform a case insensitive search from the
4329 * current position with the following quirks:
4330 * - If two chars or more are pressed in quick succession we search
4331 * for the corresponding string (e.g. 'abc').
4332 * - If there is a delay we wipe away the current search string and
4333 * restart with just that char.
4334 * - If the user keeps pressing the same character, whether slowly or
4335 * fast, so that the search string is entirely composed of this
4336 * character ('aaaaa' for instance), then we search for first item
4337 * that starting with that character.
4338 * - If the user types the above character in quick succession, then
4339 * we must also search for the corresponding string ('aaaaa'), and
4340 * go to that string if there is a match.
4348 * - The current implementation has a list of characters it will
4349 * accept and it ignores averything else. In particular it will
4350 * ignore accentuated characters which seems to match what
4351 * Windows does. But I'm not sure it makes sense to follow
4353 * - We don't sound a beep when the search fails.
4354 * - The search should start from the focused item, not from the selected
4355 * item. One reason for this is to allow for multiple selections in trees.
4356 * But currently infoPtr->focusedItem does not seem very usable.
4360 * TREEVIEW_ProcessLetterKeys
4362 static INT TREEVIEW_ProcessLetterKeys(
4363 HWND hwnd, /* handle to the window */
4364 WPARAM charCode, /* the character code, the actual character */
4365 LPARAM keyData /* key data */
4368 TREEVIEW_INFO *infoPtr;
4370 HTREEITEM endidx,idx;
4372 WCHAR buffer[MAX_PATH];
4373 DWORD timestamp,elapsed;
4375 /* simple parameter checking */
4376 if (!hwnd || !charCode || !keyData)
4379 infoPtr=(TREEVIEW_INFO*)GetWindowLongPtrW(hwnd, 0);
4383 /* only allow the valid WM_CHARs through */
4384 if (!isalnum(charCode) &&
4385 charCode != '.' && charCode != '`' && charCode != '!' &&
4386 charCode != '@' && charCode != '#' && charCode != '$' &&
4387 charCode != '%' && charCode != '^' && charCode != '&' &&
4388 charCode != '*' && charCode != '(' && charCode != ')' &&
4389 charCode != '-' && charCode != '_' && charCode != '+' &&
4390 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4391 charCode != '}' && charCode != '[' && charCode != '{' &&
4392 charCode != '/' && charCode != '?' && charCode != '>' &&
4393 charCode != '<' && charCode != ',' && charCode != '~')
4396 /* compute how much time elapsed since last keypress */
4397 timestamp = GetTickCount();
4398 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4399 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4401 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4404 /* update the search parameters */
4405 infoPtr->lastKeyPressTimestamp=timestamp;
4406 if (elapsed < KEY_DELAY) {
4407 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4408 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4410 if (infoPtr->charCode != charCode) {
4411 infoPtr->charCode=charCode=0;
4414 infoPtr->charCode=charCode;
4415 infoPtr->szSearchParam[0]=charCode;
4416 infoPtr->nSearchParamLength=1;
4417 /* Redundant with the 1 char string */
4421 /* and search from the current position */
4423 if (infoPtr->selectedItem != NULL) {
4424 endidx=infoPtr->selectedItem;
4425 /* if looking for single character match,
4426 * then we must always move forward
4428 if (infoPtr->nSearchParamLength == 1)
4429 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4434 idx=infoPtr->root->firstChild;
4437 /* At the end point, sort out wrapping */
4440 /* If endidx is null, stop at the last item (ie top to bottom) */
4444 /* Otherwise, start again at the very beginning */
4445 idx=infoPtr->root->firstChild;
4447 /* But if we are stopping on the first child, end now! */
4448 if (idx == endidx) break;
4452 ZeroMemory(&item, sizeof(item));
4453 item.mask = TVIF_TEXT;
4455 item.pszText = buffer;
4456 item.cchTextMax = sizeof(buffer);
4457 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4459 /* check for a match */
4460 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4463 } else if ( (charCode != 0) && (nItem == NULL) &&
4464 (nItem != infoPtr->selectedItem) &&
4465 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4466 /* This would work but we must keep looking for a longer match */
4469 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4470 } while (idx != endidx);
4472 if (nItem != NULL) {
4473 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4474 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4481 /* Scrolling ************************************************************/
4484 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4487 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4488 HTREEITEM newFirstVisible = NULL;
4489 int visible_pos = -1;
4491 if (!TREEVIEW_ValidItem(infoPtr, item))
4494 if (!ISVISIBLE(item))
4496 /* Expand parents as necessary. */
4499 /* see if we are trying to ensure that root is vislble */
4500 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4501 parent = item->parent;
4503 parent = item; /* this item is the topmost item */
4505 while (parent != infoPtr->root)
4507 if (!(parent->state & TVIS_EXPANDED))
4508 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4510 parent = parent->parent;
4514 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4516 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4517 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4519 if (hasFirstVisible)
4520 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4522 if (visible_pos < 0)
4524 /* item is before the start of the list: put it at the top. */
4525 newFirstVisible = item;
4527 else if (visible_pos >= viscount
4528 /* Sometimes, before we are displayed, GVC is 0, causing us to
4529 * spuriously scroll up. */
4530 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4532 /* item is past the end of the list. */
4533 int scroll = visible_pos - viscount;
4535 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4541 /* Scroll window so item's text is visible as much as possible */
4542 /* Calculation of amount of extra space is taken from EditLabel code */
4544 TEXTMETRICW textMetric;
4545 HDC hdc = GetWindowDC(infoPtr->hwnd);
4547 x = item->textWidth;
4549 GetTextMetricsW(hdc, &textMetric);
4550 ReleaseDC(infoPtr->hwnd, hdc);
4552 x += (textMetric.tmMaxCharWidth * 2);
4553 x = max(x, textMetric.tmMaxCharWidth * 3);
4555 if (item->textOffset < 0)
4556 pos = item->textOffset;
4557 else if (item->textOffset + x > infoPtr->clientWidth)
4559 if (x > infoPtr->clientWidth)
4560 pos = item->textOffset;
4562 pos = item->textOffset + x - infoPtr->clientWidth;
4567 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4570 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4572 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4581 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4582 TREEVIEW_ITEM *newFirstVisible,
4583 BOOL bUpdateScrollPos)
4587 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4589 if (newFirstVisible != NULL)
4591 /* Prevent an empty gap from appearing at the bottom... */
4592 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4593 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4597 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4600 /* ... unless we just don't have enough items. */
4601 if (newFirstVisible == NULL)
4602 newFirstVisible = infoPtr->root->firstChild;
4606 if (infoPtr->firstVisible != newFirstVisible)
4608 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4610 infoPtr->firstVisible = newFirstVisible;
4611 TREEVIEW_Invalidate(infoPtr, NULL);
4615 TREEVIEW_ITEM *item;
4616 int scroll = infoPtr->uItemHeight *
4617 (infoPtr->firstVisible->visibleOrder
4618 - newFirstVisible->visibleOrder);
4620 infoPtr->firstVisible = newFirstVisible;
4622 for (item = infoPtr->root->firstChild; item != NULL;
4623 item = TREEVIEW_GetNextListItem(infoPtr, item))
4625 item->rect.top += scroll;
4626 item->rect.bottom += scroll;
4629 if (bUpdateScrollPos)
4630 SetScrollPos(infoPtr->hwnd, SB_VERT,
4631 newFirstVisible->visibleOrder, TRUE);
4633 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4638 /************************************************************************
4639 * VScroll is always in units of visible items. i.e. we always have a
4640 * visible item aligned to the top of the control. (Unless we have no
4644 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4646 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4647 TREEVIEW_ITEM *newFirstVisible = NULL;
4649 int nScrollCode = LOWORD(wParam);
4651 TRACE("wp %x\n", wParam);
4653 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4656 if (!oldFirstVisible)
4658 assert(infoPtr->root->firstChild == NULL);
4662 switch (nScrollCode)
4665 newFirstVisible = infoPtr->root->firstChild;
4669 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4673 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4677 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4681 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4682 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4686 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4687 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4691 case SB_THUMBPOSITION:
4692 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4693 infoPtr->root->firstChild,
4694 (LONG)(SHORT)HIWORD(wParam));
4701 if (newFirstVisible != NULL)
4703 if (newFirstVisible != oldFirstVisible)
4704 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4705 nScrollCode != SB_THUMBTRACK);
4706 else if (nScrollCode == SB_THUMBPOSITION)
4707 SetScrollPos(infoPtr->hwnd, SB_VERT,
4708 newFirstVisible->visibleOrder, TRUE);
4715 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4718 int scrollX = infoPtr->scrollX;
4719 int nScrollCode = LOWORD(wParam);
4721 TRACE("wp %x\n", wParam);
4723 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4726 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4727 /* shall never occur */
4734 switch (nScrollCode)
4737 scrollX -= infoPtr->uItemHeight;
4740 scrollX += infoPtr->uItemHeight;
4743 scrollX -= infoPtr->clientWidth;
4746 scrollX += infoPtr->clientWidth;
4750 case SB_THUMBPOSITION:
4751 scrollX = (int)(SHORT)HIWORD(wParam);
4758 if (scrollX > maxWidth)
4760 else if (scrollX < 0)
4764 if (scrollX != infoPtr->scrollX)
4766 TREEVIEW_ITEM *item;
4767 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4769 for (item = infoPtr->root->firstChild; item != NULL;
4770 item = TREEVIEW_GetNextListItem(infoPtr, item))
4772 item->linesOffset += scroll_pixels;
4773 item->stateOffset += scroll_pixels;
4774 item->imageOffset += scroll_pixels;
4775 item->textOffset += scroll_pixels;
4778 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4779 infoPtr->scrollX = scrollX;
4780 UpdateWindow(infoPtr->hwnd);
4783 if (nScrollCode != SB_THUMBTRACK)
4784 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4790 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4793 UINT pulScrollLines = 3;
4795 if (infoPtr->firstVisible == NULL)
4798 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4800 gcWheelDelta = -(short)HIWORD(wParam);
4801 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4803 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4805 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4806 int maxDy = infoPtr->maxVisibleOrder;
4814 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4819 /* Create/Destroy *******************************************************/
4822 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4825 TREEVIEW_INFO *infoPtr;
4828 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4830 infoPtr = (TREEVIEW_INFO *)Alloc(sizeof(TREEVIEW_INFO));
4832 if (infoPtr == NULL)
4834 ERR("could not allocate info memory!\n");
4838 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4840 infoPtr->hwnd = hwnd;
4841 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4843 infoPtr->uNumItems = 0;
4844 infoPtr->cdmode = 0;
4845 infoPtr->uScrollTime = 300; /* milliseconds */
4846 infoPtr->bRedraw = TRUE;
4848 GetClientRect(hwnd, &rcClient);
4850 /* No scroll bars yet. */
4851 infoPtr->clientWidth = rcClient.right;
4852 infoPtr->clientHeight = rcClient.bottom;
4853 infoPtr->uInternalStatus = 0;
4855 infoPtr->treeWidth = 0;
4856 infoPtr->treeHeight = 0;
4858 infoPtr->uIndent = MINIMUM_INDENT;
4859 infoPtr->selectedItem = 0;
4860 infoPtr->focusedItem = 0;
4861 infoPtr->hotItem = 0;
4862 infoPtr->firstVisible = 0;
4863 infoPtr->maxVisibleOrder = 0;
4864 infoPtr->dropItem = 0;
4865 infoPtr->insertMarkItem = 0;
4866 infoPtr->insertBeforeorAfter = 0;
4869 infoPtr->scrollX = 0;
4871 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4872 infoPtr->clrText = -1; /* use system color */
4873 infoPtr->clrLine = RGB(128, 128, 128);
4874 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4878 infoPtr->hwndEdit = 0;
4879 infoPtr->wpEditOrig = NULL;
4880 infoPtr->bIgnoreEditKillFocus = FALSE;
4881 infoPtr->bLabelChanged = FALSE;
4883 infoPtr->himlNormal = NULL;
4884 infoPtr->himlState = NULL;
4885 infoPtr->normalImageWidth = 0;
4886 infoPtr->normalImageHeight = 0;
4887 infoPtr->stateImageWidth = 0;
4888 infoPtr->stateImageHeight = 0;
4890 infoPtr->items = DPA_Create(16);
4892 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
4893 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
4894 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4895 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
4896 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
4898 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4900 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4901 infoPtr->root->state = TVIS_EXPANDED;
4902 infoPtr->root->iLevel = -1;
4903 infoPtr->root->visibleOrder = -1;
4905 infoPtr->hwndNotify = lpcs->hwndParent;
4907 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4910 infoPtr->hwndToolTip = 0;
4912 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
4914 /* Determine what type of notify should be issued */
4915 /* sets infoPtr->bNtfUnicode */
4916 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
4918 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4919 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4921 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4924 HBITMAP hbm, hbmOld;
4928 infoPtr->himlState =
4929 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4931 hdcScreen = GetDC(0);
4933 /* Create a coloured bitmap compatible with the screen depth
4934 because checkboxes are not black&white */
4935 hdc = CreateCompatibleDC(hdcScreen);
4936 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4937 hbmOld = SelectObject(hdc, hbm);
4939 rc.left = 0; rc.top = 0;
4940 rc.right = 48; rc.bottom = 16;
4941 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4943 rc.left = 18; rc.top = 2;
4944 rc.right = 30; rc.bottom = 14;
4945 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4946 DFCS_BUTTONCHECK|DFCS_FLAT);
4948 rc.left = 34; rc.right = 46;
4949 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4950 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4952 SelectObject(hdc, hbmOld);
4953 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4954 GetSysColor(COLOR_WINDOW));
4955 TRACE("checkbox index %d\n", nIndex);
4959 ReleaseDC(0, hdcScreen);
4961 infoPtr->stateImageWidth = 16;
4962 infoPtr->stateImageHeight = 16;
4965 /* Make sure actual scrollbar state is consistent with uInternalStatus */
4966 ShowScrollBar(hwnd, SB_VERT, FALSE);
4967 ShowScrollBar(hwnd, SB_HORZ, FALSE);
4969 OpenThemeData (hwnd, themeClass);
4976 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4980 TREEVIEW_RemoveTree(infoPtr);
4982 /* tool tip is automatically destroyed: we are its owner */
4984 /* Restore original wndproc */
4985 if (infoPtr->hwndEdit)
4986 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
4987 (DWORD_PTR)infoPtr->wpEditOrig);
4989 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
4991 /* Deassociate treeview from the window before doing anything drastic. */
4992 SetWindowLongPtrW(infoPtr->hwnd, 0, (DWORD_PTR)NULL);
4995 DeleteObject(infoPtr->hDefaultFont);
4996 DeleteObject(infoPtr->hBoldFont);
4997 DeleteObject(infoPtr->hUnderlineFont);
5003 /* Miscellaneous Messages ***********************************************/
5006 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5014 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5015 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5016 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5017 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5018 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5019 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5020 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5021 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5022 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5026 if (key >= VK_PRIOR && key <= VK_DOWN)
5028 unsigned char code = scroll[key - VK_PRIOR].code;
5030 (((code & (1 << 7)) == (SB_HORZ << 7))
5032 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5038 /************************************************************************
5041 * VK_UP Move selection to the previous non-hidden item.
5042 * VK_DOWN Move selection to the next non-hidden item.
5043 * VK_HOME Move selection to the first item.
5044 * VK_END Move selection to the last item.
5045 * VK_LEFT If expanded then collapse, otherwise move to parent.
5046 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5048 * VK_SUBTRACT Collapse.
5049 * VK_MULTIPLY Expand all.
5050 * VK_PRIOR Move up GetVisibleCount items.
5051 * VK_NEXT Move down GetVisibleCount items.
5052 * VK_BACK Move to parent.
5053 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5056 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5058 /* If it is non-NULL and different, it will be selected and visible. */
5059 TREEVIEW_ITEM *newSelection = NULL;
5061 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5063 TRACE("%x\n", wParam);
5065 if (prevItem == NULL)
5068 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5069 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5074 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5076 newSelection = infoPtr->root->firstChild;
5080 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5084 newSelection = infoPtr->root->firstChild;
5088 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5092 if (prevItem->state & TVIS_EXPANDED)
5094 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5096 else if (prevItem->parent != infoPtr->root)
5098 newSelection = prevItem->parent;
5103 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5105 if (!(prevItem->state & TVIS_EXPANDED))
5106 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5109 newSelection = prevItem->firstChild;
5116 TREEVIEW_ExpandAll(infoPtr, prevItem);
5120 if (!(prevItem->state & TVIS_EXPANDED))
5121 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5125 if (prevItem->state & TVIS_EXPANDED)
5126 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5131 = TREEVIEW_GetListItem(infoPtr, prevItem,
5132 -TREEVIEW_GetVisibleCount(infoPtr));
5137 = TREEVIEW_GetListItem(infoPtr, prevItem,
5138 TREEVIEW_GetVisibleCount(infoPtr));
5142 newSelection = prevItem->parent;
5143 if (newSelection == infoPtr->root)
5144 newSelection = NULL;
5148 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5149 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5153 if (newSelection && newSelection != prevItem)
5155 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5158 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5166 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5168 if (infoPtr->hotItem)
5170 /* remove hot effect from item */
5171 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5172 infoPtr->hotItem = NULL;
5178 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, WPARAM wParam, LPARAM lParam)
5181 TRACKMOUSEEVENT trackinfo;
5182 TREEVIEW_ITEM * item;
5184 /* fill in the TRACKMOUSEEVENT struct */
5185 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5186 trackinfo.dwFlags = TME_QUERY;
5187 trackinfo.hwndTrack = infoPtr->hwnd;
5188 trackinfo.dwHoverTime = HOVER_DEFAULT;
5190 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5191 _TrackMouseEvent(&trackinfo);
5193 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5194 if(!(trackinfo.dwFlags & TME_LEAVE))
5196 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5198 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5199 /* and can properly deactivate the hot item */
5200 _TrackMouseEvent(&trackinfo);
5203 pt.x = (short)LOWORD(lParam);
5204 pt.y = (short)HIWORD(lParam);
5206 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5208 if (item != infoPtr->hotItem)
5210 /* redraw old hot item */
5211 if (infoPtr->hotItem)
5212 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5213 infoPtr->hotItem = item;
5214 /* redraw new hot item */
5215 if (infoPtr->hotItem)
5216 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5222 /* Draw themed border */
5223 static BOOL nc_paint (TREEVIEW_INFO *infoPtr, HRGN region)
5225 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5229 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5230 cyEdge = GetSystemMetrics (SM_CYEDGE);
5232 if (!theme) return FALSE;
5234 GetWindowRect(infoPtr->hwnd, &r);
5236 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5237 r.right - cxEdge, r.bottom - cyEdge);
5238 if (region != (HRGN)1)
5239 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5240 OffsetRect(&r, -r.left, -r.top);
5242 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5243 OffsetRect(&r, -r.left, -r.top);
5245 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5246 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5247 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5248 ReleaseDC(infoPtr->hwnd, dc);
5250 /* Call default proc to get the scrollbars etc. painted */
5251 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5257 TREEVIEW_Notify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5259 LPNMHDR lpnmh = (LPNMHDR)lParam;
5261 if (lpnmh->code == PGN_CALCSIZE) {
5262 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5264 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5265 lppgc->iWidth = infoPtr->treeWidth;
5266 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5267 infoPtr->treeWidth, infoPtr->clientWidth);
5270 lppgc->iHeight = infoPtr->treeHeight;
5271 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5272 infoPtr->treeHeight, infoPtr->clientHeight);
5276 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5279 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5283 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5285 if (nCommand != NF_REQUERY) return 0;
5287 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5288 TRACE("format=%d\n", format);
5290 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5292 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5298 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5300 if (wParam == SIZE_RESTORED)
5302 infoPtr->clientWidth = (short)LOWORD(lParam);
5303 infoPtr->clientHeight = (short)HIWORD(lParam);
5305 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5306 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5307 TREEVIEW_UpdateScrollBars(infoPtr);
5311 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
5314 TREEVIEW_Invalidate(infoPtr, NULL);
5319 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5321 TRACE("(%x %lx)\n", wParam, lParam);
5323 if (wParam == GWL_STYLE)
5325 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5327 /* we have to take special care about tooltips */
5328 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5330 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5332 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5337 DestroyWindow(infoPtr->hwndToolTip);
5338 infoPtr->hwndToolTip = 0;
5342 infoPtr->dwStyle = dwNewStyle;
5345 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5346 TREEVIEW_UpdateScrollBars(infoPtr);
5347 TREEVIEW_Invalidate(infoPtr, NULL);
5353 TREEVIEW_SetCursor(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5356 TREEVIEW_ITEM * item;
5359 ScreenToClient(infoPtr->hwnd, &pt);
5361 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5363 /* FIXME: send NM_SETCURSOR */
5365 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5367 SetCursor(infoPtr->hcurHand);
5371 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5375 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5379 if (!infoPtr->selectedItem)
5381 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5385 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5386 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5391 TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
5395 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5396 UpdateWindow(infoPtr->hwnd);
5397 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5401 /* update theme after a WM_THEMECHANGED message */
5402 static LRESULT theme_changed (TREEVIEW_INFO* infoPtr)
5404 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5405 CloseThemeData (theme);
5406 OpenThemeData (infoPtr->hwnd, themeClass);
5411 static LRESULT WINAPI
5412 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5414 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5416 TRACE("hwnd %p msg %04x wp=%08x lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5418 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5421 if (uMsg == WM_CREATE)
5422 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5429 case TVM_CREATEDRAGIMAGE:
5430 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5432 case TVM_DELETEITEM:
5433 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5435 case TVM_EDITLABELA:
5436 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5438 case TVM_EDITLABELW:
5439 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5441 case TVM_ENDEDITLABELNOW:
5442 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5444 case TVM_ENSUREVISIBLE:
5445 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5448 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5450 case TVM_GETBKCOLOR:
5451 return TREEVIEW_GetBkColor(infoPtr);
5454 return TREEVIEW_GetCount(infoPtr);
5456 case TVM_GETEDITCONTROL:
5457 return TREEVIEW_GetEditControl(infoPtr);
5459 case TVM_GETIMAGELIST:
5460 return TREEVIEW_GetImageList(infoPtr, wParam);
5463 return TREEVIEW_GetIndent(infoPtr);
5465 case TVM_GETINSERTMARKCOLOR:
5466 return TREEVIEW_GetInsertMarkColor(infoPtr);
5468 case TVM_GETISEARCHSTRINGA:
5469 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5472 case TVM_GETISEARCHSTRINGW:
5473 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5477 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5480 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5482 case TVM_GETITEMHEIGHT:
5483 return TREEVIEW_GetItemHeight(infoPtr);
5485 case TVM_GETITEMRECT:
5486 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5488 case TVM_GETITEMSTATE:
5489 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5491 case TVM_GETLINECOLOR:
5492 return TREEVIEW_GetLineColor(infoPtr);
5494 case TVM_GETNEXTITEM:
5495 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5497 case TVM_GETSCROLLTIME:
5498 return TREEVIEW_GetScrollTime(infoPtr);
5500 case TVM_GETTEXTCOLOR:
5501 return TREEVIEW_GetTextColor(infoPtr);
5503 case TVM_GETTOOLTIPS:
5504 return TREEVIEW_GetToolTips(infoPtr);
5506 case TVM_GETUNICODEFORMAT:
5507 return TREEVIEW_GetUnicodeFormat(infoPtr);
5509 case TVM_GETVISIBLECOUNT:
5510 return TREEVIEW_GetVisibleCount(infoPtr);
5513 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5515 case TVM_INSERTITEMA:
5516 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, FALSE);
5518 case TVM_INSERTITEMW:
5519 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, TRUE);
5521 case TVM_SELECTITEM:
5522 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5524 case TVM_SETBKCOLOR:
5525 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5527 case TVM_SETIMAGELIST:
5528 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5531 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5533 case TVM_SETINSERTMARK:
5534 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5536 case TVM_SETINSERTMARKCOLOR:
5537 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5540 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5543 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5545 case TVM_SETLINECOLOR:
5546 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5548 case TVM_SETITEMHEIGHT:
5549 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5551 case TVM_SETSCROLLTIME:
5552 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5554 case TVM_SETTEXTCOLOR:
5555 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5557 case TVM_SETTOOLTIPS:
5558 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5560 case TVM_SETUNICODEFORMAT:
5561 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5563 case TVM_SORTCHILDREN:
5564 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5566 case TVM_SORTCHILDRENCB:
5567 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5570 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5573 return TREEVIEW_Command(infoPtr, wParam, lParam);
5576 return TREEVIEW_Destroy(infoPtr);
5581 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5584 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5587 return TREEVIEW_GetFont(infoPtr);
5590 return TREEVIEW_HScroll(infoPtr, wParam);
5593 return TREEVIEW_KeyDown(infoPtr, wParam);
5596 return TREEVIEW_KillFocus(infoPtr);
5598 case WM_LBUTTONDBLCLK:
5599 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5601 case WM_LBUTTONDOWN:
5602 return TREEVIEW_LButtonDown(infoPtr, lParam);
5604 /* WM_MBUTTONDOWN */
5607 return TREEVIEW_MouseLeave(infoPtr);
5610 if (infoPtr->dwStyle & TVS_TRACKSELECT)
5611 return TREEVIEW_MouseMove(infoPtr, wParam, lParam);
5615 case WM_NCLBUTTONDOWN:
5616 if (infoPtr->hwndEdit)
5617 SetFocus(infoPtr->hwnd);
5621 if (nc_paint (infoPtr, (HRGN)wParam))
5626 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5628 case WM_NOTIFYFORMAT:
5629 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5631 case WM_PRINTCLIENT:
5633 return TREEVIEW_Paint(infoPtr, wParam);
5635 case WM_RBUTTONDOWN:
5636 return TREEVIEW_RButtonDown(infoPtr, lParam);
5639 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5642 return TREEVIEW_SetFocus(infoPtr);
5645 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5648 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5651 return TREEVIEW_Size(infoPtr, wParam, lParam);
5653 case WM_STYLECHANGED:
5654 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5656 /* WM_SYSCOLORCHANGE */
5661 return TREEVIEW_HandleTimer(infoPtr, wParam);
5663 case WM_THEMECHANGED:
5664 return theme_changed (infoPtr);
5667 return TREEVIEW_VScroll(infoPtr, wParam);
5669 /* WM_WININICHANGE */
5672 if (wParam & (MK_SHIFT | MK_CONTROL))
5674 return TREEVIEW_MouseWheel(infoPtr, wParam);
5677 TRACE("drawItem\n");
5681 /* This mostly catches MFC and Delphi messages. :( */
5682 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5683 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
5685 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5690 /* Class Registration ***************************************************/
5693 TREEVIEW_Register(void)
5699 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5700 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5701 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5702 wndClass.cbClsExtra = 0;
5703 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5705 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5706 wndClass.hbrBackground = 0;
5707 wndClass.lpszClassName = WC_TREEVIEWW;
5709 RegisterClassW(&wndClass);
5714 TREEVIEW_Unregister(void)
5716 UnregisterClassW(WC_TREEVIEWW, NULL);
5720 /* Tree Verification ****************************************************/
5724 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5726 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5727 TREEVIEW_ITEM *item)
5729 assert(infoPtr != NULL);
5730 assert(item != NULL);
5732 /* both NULL, or both non-null */
5733 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5735 assert(item->firstChild != item);
5736 assert(item->lastChild != item);
5738 if (item->firstChild)
5740 assert(item->firstChild->parent == item);
5741 assert(item->firstChild->prevSibling == NULL);
5744 if (item->lastChild)
5746 assert(item->lastChild->parent == item);
5747 assert(item->lastChild->nextSibling == NULL);
5750 assert(item->nextSibling != item);
5751 if (item->nextSibling)
5753 assert(item->nextSibling->parent == item->parent);
5754 assert(item->nextSibling->prevSibling == item);
5757 assert(item->prevSibling != item);
5758 if (item->prevSibling)
5760 assert(item->prevSibling->parent == item->parent);
5761 assert(item->prevSibling->nextSibling == item);
5766 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5768 assert(item != NULL);
5770 assert(item->parent != NULL);
5771 assert(item->parent != item);
5772 assert(item->iLevel == item->parent->iLevel + 1);
5774 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5776 TREEVIEW_VerifyItemCommon(infoPtr, item);
5778 TREEVIEW_VerifyChildren(infoPtr, item);
5782 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5784 TREEVIEW_ITEM *child;
5785 assert(item != NULL);
5787 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5788 TREEVIEW_VerifyItem(infoPtr, child);
5792 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5794 TREEVIEW_ITEM *root = infoPtr->root;
5796 assert(root != NULL);
5797 assert(root->iLevel == -1);
5798 assert(root->parent == NULL);
5799 assert(root->prevSibling == NULL);
5801 TREEVIEW_VerifyItemCommon(infoPtr, root);
5803 TREEVIEW_VerifyChildren(infoPtr, root);
5807 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5809 assert(infoPtr != NULL);
5811 TREEVIEW_VerifyRoot(infoPtr);