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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
24 * Note2: All items always! have valid (allocated) pszText field.
25 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
26 * of size TEXT_CALLBACK_SIZE in DoSetItem.
27 * We use callbackMask to keep track of fields to be updated.
30 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
31 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
33 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
35 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
37 * Make the insertion mark look right.
38 * Scroll (instead of repaint) as much as possible.
42 #include "wine/port.h"
51 #define NONAMELESSUNION
52 #define NONAMELESSSTRUCT
60 #include "wine/unicode.h"
61 #include "wine/debug.h"
63 /* internal structures */
65 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
76 int iIntegral; /* item height multiplier (1 is normal) */
77 int iLevel; /* indentation level:0=root level */
78 HTREEITEM parent; /* handle to parent or 0 if at root */
79 HTREEITEM firstChild; /* handle to first child or 0 if no child */
81 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
82 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
88 LONG textWidth; /* horizontal text extent for pszText */
89 LONG visibleOrder; /* visible ordering, 0 is first visible item */
93 typedef struct tagTREEVIEW_INFO
96 HWND hwndNotify; /* Owner window to send notifications to */
101 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
102 INT cdmode; /* last custom draw setting */
103 UINT uScrollTime; /* max. time for scrolling in milliseconds */
104 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
106 UINT uItemHeight; /* item height */
109 LONG clientWidth; /* width of control window */
110 LONG clientHeight; /* height of control window */
112 LONG treeWidth; /* width of visible tree items */
113 LONG treeHeight; /* height of visible tree items */
115 UINT uIndent; /* indentation in pixels */
116 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
117 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
118 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
120 HTREEITEM firstVisible; /* handle to first visible item */
121 LONG maxVisibleOrder;
122 HTREEITEM dropItem; /* handle to item selected by drag cursor */
123 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
124 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
125 HIMAGELIST dragList; /* Bitmap of dragged item */
130 COLORREF clrInsertMark;
134 HFONT hUnderlineFont;
139 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
140 BOOL bIgnoreEditKillFocus;
143 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
144 HIMAGELIST himlNormal;
145 int normalImageHeight;
146 int normalImageWidth;
147 HIMAGELIST himlState;
148 int stateImageHeight;
152 DWORD lastKeyPressTimestamp; /* Added */
153 WPARAM charCode; /* Added */
154 INT nSearchParamLength; /* Added */
155 WCHAR szSearchParam[ MAX_PATH ]; /* Added */
159 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
160 #define KEY_DELAY 450
162 /* bitflags for infoPtr->uInternalStatus */
164 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
165 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
166 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
167 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
168 #define TV_RDRAG 0x10 /* dito Rbutton */
169 #define TV_RDRAGGING 0x20
171 /* bitflags for infoPtr->timer */
173 #define TV_EDIT_TIMER 2
174 #define TV_EDIT_TIMER_SET 2
177 VOID TREEVIEW_Register (VOID);
178 VOID TREEVIEW_Unregister (VOID);
181 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
184 #define TEXT_CALLBACK_SIZE 260
186 #define TREEVIEW_LEFT_MARGIN 8
188 #define MINIMUM_INDENT 19
190 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
192 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
193 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
194 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
197 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
200 static VOID TREEVIEW_Invalidate(TREEVIEW_INFO *, TREEVIEW_ITEM *);
202 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
203 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
204 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
205 static LRESULT TREEVIEW_RButtonUp(TREEVIEW_INFO *, LPPOINT);
206 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
207 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
208 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
209 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND wParam, UINT lParam);
212 /* Random Utilities *****************************************************/
216 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
221 /* The definition is at the end of the file. */
222 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
225 /* Returns the treeview private data if hwnd is a treeview.
226 * Otherwise returns an undefined value. */
227 static TREEVIEW_INFO *
228 TREEVIEW_GetInfoPtr(HWND hwnd)
230 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
233 /* Don't call this. Nothing wants an item index. */
235 TREEVIEW_GetItemIndex(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
237 assert(infoPtr != NULL);
239 return DPA_GetPtrIndex(infoPtr->items, handle);
242 /***************************************************************************
243 * This method checks that handle is an item for this tree.
246 TREEVIEW_ValidItem(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
248 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
250 TRACE("invalid item %p\n", handle);
258 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
262 GetObjectW(hOrigFont, sizeof(font), &font);
263 font.lfWeight = FW_BOLD;
264 return CreateFontIndirectW(&font);
268 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
272 GetObjectW(hOrigFont, sizeof(font), &font);
273 font.lfUnderline = TRUE;
274 return CreateFontIndirectW(&font);
278 TREEVIEW_FontForItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
280 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
281 return infoPtr->hUnderlineFont;
282 if (item->state & TVIS_BOLD)
283 return infoPtr->hBoldFont;
284 return infoPtr->hFont;
287 /* for trace/debugging purposes only */
289 TREEVIEW_ItemName(TREEVIEW_ITEM *item)
291 if (item == NULL) return "<null item>";
292 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
293 if (item->pszText == NULL) return "<null>";
294 return debugstr_w(item->pszText);
297 /* An item is not a child of itself. */
299 TREEVIEW_IsChildOf(TREEVIEW_ITEM *parent, TREEVIEW_ITEM *child)
303 child = child->parent;
304 if (child == parent) return TRUE;
305 } while (child != NULL);
311 /* Tree Traversal *******************************************************/
313 /***************************************************************************
314 * This method returns the last expanded sibling or child child item
317 static TREEVIEW_ITEM *
318 TREEVIEW_GetLastListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
323 while (wineItem->lastChild)
325 if (wineItem->state & TVIS_EXPANDED)
326 wineItem = wineItem->lastChild;
331 if (wineItem == infoPtr->root)
337 /***************************************************************************
338 * This method returns the previous non-hidden item in the list not
339 * considering the tree hierarchy.
341 static TREEVIEW_ITEM *
342 TREEVIEW_GetPrevListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
344 if (tvItem->prevSibling)
346 /* This item has a prevSibling, get the last item in the sibling's tree. */
347 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
349 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
350 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
356 /* this item does not have a prevSibling, get the parent */
357 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
362 /***************************************************************************
363 * This method returns the next physical item in the treeview not
364 * considering the tree hierarchy.
366 static TREEVIEW_ITEM *
367 TREEVIEW_GetNextListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
369 assert(tvItem != NULL);
372 * If this item has children and is expanded, return the first child
374 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
376 return tvItem->firstChild;
381 * try to get the sibling
383 if (tvItem->nextSibling)
384 return tvItem->nextSibling;
387 * Otherwise, get the parent's sibling.
389 while (tvItem->parent)
391 tvItem = tvItem->parent;
393 if (tvItem->nextSibling)
394 return tvItem->nextSibling;
400 /***************************************************************************
401 * This method returns the nth item starting at the given item. It returns
402 * the last item (or first) we we run out of items.
404 * Will scroll backward if count is <0.
405 * forward if count is >0.
407 static TREEVIEW_ITEM *
408 TREEVIEW_GetListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
411 TREEVIEW_ITEM *(*next_item)(TREEVIEW_INFO *, TREEVIEW_ITEM *);
412 TREEVIEW_ITEM *previousItem;
414 assert(wineItem != NULL);
418 next_item = TREEVIEW_GetNextListItem;
423 next_item = TREEVIEW_GetPrevListItem;
430 previousItem = wineItem;
431 wineItem = next_item(infoPtr, wineItem);
433 } while (--count && wineItem != NULL);
436 return wineItem ? wineItem : previousItem;
439 /* Notifications ************************************************************/
441 static INT get_notifycode(TREEVIEW_INFO *infoPtr, INT code)
443 if (!infoPtr->bNtfUnicode) {
445 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
446 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
447 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
448 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
449 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
450 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
451 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
452 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
453 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
454 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
455 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
456 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
463 TREEVIEW_SendRealNotify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
465 TRACE("wParam=%d, lParam=%ld\n", wParam, lParam);
466 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
470 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO *infoPtr, UINT code)
473 HWND hwnd = infoPtr->hwnd;
476 nmhdr.hwndFrom = hwnd;
477 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
478 nmhdr.code = get_notifycode(infoPtr, code);
480 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
481 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
485 TREEVIEW_TVItemFromItem(TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
488 tvItem->hItem = item;
489 tvItem->state = item->state;
490 tvItem->stateMask = 0;
491 tvItem->iImage = item->iImage;
492 tvItem->iImage = item->iImage;
493 tvItem->iSelectedImage = item->iSelectedImage;
494 tvItem->cChildren = item->cChildren;
495 tvItem->lParam = item->lParam;
499 if (!infoPtr->bNtfUnicode)
501 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
502 tvItem->pszText = Alloc (tvItem->cchTextMax);
503 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
507 tvItem->cchTextMax = item->cchTextMax;
508 tvItem->pszText = item->pszText;
513 tvItem->cchTextMax = 0;
514 tvItem->pszText = NULL;
519 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO *infoPtr, UINT code, UINT action,
520 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
522 HWND hwnd = infoPtr->hwnd;
526 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
527 code, action, oldItem, newItem);
529 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWA));
531 nmhdr.hdr.hwndFrom = hwnd;
532 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
533 nmhdr.hdr.code = get_notifycode(infoPtr, code);
534 nmhdr.action = action;
537 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
540 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
545 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
546 (WPARAM)nmhdr.hdr.idFrom,
548 if (!infoPtr->bNtfUnicode)
550 Free(nmhdr.itemOld.pszText);
551 Free(nmhdr.itemNew.pszText);
557 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO *infoPtr, UINT code,
558 HTREEITEM dragItem, POINT pt)
560 HWND hwnd = infoPtr->hwnd;
563 TRACE("code:%d dragitem:%p\n", code, dragItem);
565 nmhdr.hdr.hwndFrom = hwnd;
566 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
567 nmhdr.hdr.code = get_notifycode(infoPtr, code);
569 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
570 nmhdr.itemNew.hItem = dragItem;
571 nmhdr.itemNew.state = dragItem->state;
572 nmhdr.itemNew.lParam = dragItem->lParam;
574 nmhdr.ptDrag.x = pt.x;
575 nmhdr.ptDrag.y = pt.y;
577 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
578 (WPARAM)nmhdr.hdr.idFrom,
584 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
587 HWND hwnd = infoPtr->hwnd;
588 NMTVCUSTOMDRAW nmcdhdr;
591 TRACE("drawstage:%lx hdc:%p\n", dwDrawStage, hdc);
593 nmcd = &nmcdhdr.nmcd;
594 nmcd->hdr.hwndFrom = hwnd;
595 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
596 nmcd->hdr.code = NM_CUSTOMDRAW;
597 nmcd->dwDrawStage = dwDrawStage;
600 nmcd->dwItemSpec = 0;
601 nmcd->uItemState = 0;
602 nmcd->lItemlParam = 0;
603 nmcdhdr.clrText = infoPtr->clrText;
604 nmcdhdr.clrTextBk = infoPtr->clrBk;
607 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
608 (WPARAM)nmcd->hdr.idFrom,
614 /* FIXME: need to find out when the flags in uItemState need to be set */
617 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO *infoPtr, HDC hdc,
618 TREEVIEW_ITEM *wineItem, UINT uItemDrawState)
620 HWND hwnd = infoPtr->hwnd;
621 NMTVCUSTOMDRAW nmcdhdr;
623 DWORD dwDrawStage, dwItemSpec;
627 dwDrawStage = CDDS_ITEM | uItemDrawState;
628 dwItemSpec = (DWORD)wineItem;
630 if (wineItem->state & TVIS_SELECTED)
631 uItemState |= CDIS_SELECTED;
632 if (wineItem == infoPtr->selectedItem)
633 uItemState |= CDIS_FOCUS;
634 if (wineItem == infoPtr->hotItem)
635 uItemState |= CDIS_HOT;
637 nmcd = &nmcdhdr.nmcd;
638 nmcd->hdr.hwndFrom = hwnd;
639 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
640 nmcd->hdr.code = NM_CUSTOMDRAW;
641 nmcd->dwDrawStage = dwDrawStage;
643 nmcd->rc = wineItem->rect;
644 nmcd->dwItemSpec = dwItemSpec;
645 nmcd->uItemState = uItemState;
646 nmcd->lItemlParam = wineItem->lParam;
647 nmcdhdr.clrText = infoPtr->clrText;
648 nmcdhdr.clrTextBk = infoPtr->clrBk;
649 nmcdhdr.iLevel = wineItem->iLevel;
651 TRACE("drawstage:%lx hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
652 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
653 nmcd->uItemState, nmcd->lItemlParam);
655 retval = TREEVIEW_SendRealNotify(infoPtr,
656 (WPARAM)nmcd->hdr.idFrom,
659 infoPtr->clrText = nmcdhdr.clrText;
660 infoPtr->clrBk = nmcdhdr.clrTextBk;
665 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
667 HWND hwnd = infoPtr->hwnd;
671 tvdi.hdr.hwndFrom = hwnd;
672 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
673 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
675 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
676 &tvdi.item, editItem);
678 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
680 if (!infoPtr->bNtfUnicode)
681 Free(tvdi.item.pszText);
687 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
690 NMTVDISPINFOW callback;
691 HWND hwnd = infoPtr->hwnd;
693 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
694 mask &= wineItem->callbackMask;
696 if (mask == 0) return;
698 callback.hdr.hwndFrom = hwnd;
699 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
700 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
702 /* 'state' always contains valid value, as well as 'lParam'.
703 * All other parameters are uninitialized.
705 callback.item.pszText = wineItem->pszText;
706 callback.item.cchTextMax = wineItem->cchTextMax;
707 callback.item.mask = mask;
708 callback.item.hItem = wineItem;
709 callback.item.state = wineItem->state;
710 callback.item.lParam = wineItem->lParam;
712 /* If text is changed we need to recalculate textWidth */
713 if (mask & TVIF_TEXT)
714 wineItem->textWidth = 0;
716 TREEVIEW_SendRealNotify(infoPtr,
717 (WPARAM)callback.hdr.idFrom, (LPARAM)&callback);
719 /* It may have changed due to a call to SetItem. */
720 mask &= wineItem->callbackMask;
722 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
724 /* Instead of copying text into our buffer user specified its own */
725 if (!infoPtr->bNtfUnicode) {
728 int len = MultiByteToWideChar( CP_ACP, 0,
729 (LPSTR)callback.item.pszText, -1,
731 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
732 newText = (LPWSTR)ReAlloc(wineItem->pszText, buflen);
734 TRACE("returned str %s, len=%d, buflen=%d\n",
735 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
739 wineItem->pszText = newText;
740 MultiByteToWideChar( CP_ACP, 0,
741 (LPSTR)callback.item.pszText, -1,
742 wineItem->pszText, buflen);
743 wineItem->cchTextMax = buflen;
745 /* If ReAlloc fails we have nothing to do, but keep original text */
748 int len = max(lstrlenW(callback.item.pszText) + 1,
750 LPWSTR newText = ReAlloc(wineItem->pszText, len);
752 TRACE("returned wstr %s, len=%d\n",
753 debugstr_w(callback.item.pszText), len);
757 wineItem->pszText = newText;
758 strcpyW(wineItem->pszText, callback.item.pszText);
759 wineItem->cchTextMax = len;
761 /* If ReAlloc fails we have nothing to do, but keep original text */
764 else if (mask & TVIF_TEXT) {
765 /* User put text into our buffer, that is ok unless A string */
766 if (!infoPtr->bNtfUnicode) {
768 LPWSTR oldText = NULL;
770 int len = MultiByteToWideChar( CP_ACP, 0,
771 (LPSTR)callback.item.pszText, -1,
773 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
774 newText = (LPWSTR)Alloc(buflen);
776 TRACE("same buffer str %s, len=%d, buflen=%d\n",
777 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
781 oldText = wineItem->pszText;
782 wineItem->pszText = newText;
783 MultiByteToWideChar( CP_ACP, 0,
784 (LPSTR)callback.item.pszText, -1,
785 wineItem->pszText, buflen);
786 wineItem->cchTextMax = buflen;
793 if (mask & TVIF_IMAGE)
794 wineItem->iImage = callback.item.iImage;
796 if (mask & TVIF_SELECTEDIMAGE)
797 wineItem->iSelectedImage = callback.item.iSelectedImage;
799 if (mask & TVIF_CHILDREN)
800 wineItem->cChildren = callback.item.cChildren;
802 /* These members are now permanently set. */
803 if (callback.item.mask & TVIF_DI_SETITEM)
804 wineItem->callbackMask &= ~callback.item.mask;
807 /***************************************************************************
808 * This function uses cChildren field to decide whether the item has
810 * Note: if this returns TRUE, the child items may not actually exist,
811 * they could be virtual.
813 * Just use wineItem->firstChild to check for physical children.
816 TREEVIEW_HasChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
818 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
820 return wineItem->cChildren > 0;
824 /* Item Position ********************************************************/
826 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
828 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO *infoPtr,
831 /* Same effect, different optimisation. */
833 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
834 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
836 BOOL lar = ((infoPtr->dwStyle
837 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
841 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
843 item->stateOffset = item->linesOffset + infoPtr->uIndent;
844 item->imageOffset = item->stateOffset
845 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
846 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
850 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
856 /* DRAW's OM docker creates items like this */
857 if (item->pszText == NULL)
869 hdc = GetDC(infoPtr->hwnd);
870 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
873 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
874 item->textWidth = sz.cx;
878 SelectObject(hdc, hOldFont);
884 TREEVIEW_ComputeItemRect(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
886 item->rect.top = infoPtr->uItemHeight *
887 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
889 item->rect.bottom = item->rect.top
890 + infoPtr->uItemHeight * item->iIntegral - 1;
893 item->rect.right = infoPtr->clientWidth;
896 /* We know that only items after start need their order updated. */
898 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
905 start = infoPtr->root->firstChild;
909 order = start->visibleOrder;
911 for (item = start; item != NULL;
912 item = TREEVIEW_GetNextListItem(infoPtr, item))
914 item->visibleOrder = order;
915 order += item->iIntegral;
918 infoPtr->maxVisibleOrder = order;
920 for (item = start; item != NULL;
921 item = TREEVIEW_GetNextListItem(infoPtr, item))
923 TREEVIEW_ComputeItemRect(infoPtr, item);
928 /* Update metrics of all items in selected subtree.
929 * root must be expanded
932 TREEVIEW_UpdateSubTree(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
934 TREEVIEW_ITEM *sibling;
938 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
941 root->state &= ~TVIS_EXPANDED;
942 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
943 root->state |= TVIS_EXPANDED;
945 hdc = GetDC(infoPtr->hwnd);
946 hOldFont = SelectObject(hdc, infoPtr->hFont);
948 for (; root != sibling;
949 root = TREEVIEW_GetNextListItem(infoPtr, root))
951 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
953 if (root->callbackMask & TVIF_TEXT)
954 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
956 if (root->textWidth == 0)
958 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
959 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
963 SelectObject(hdc, hOldFont);
964 ReleaseDC(infoPtr->hwnd, hdc);
967 /* Item Allocation **********************************************************/
969 static TREEVIEW_ITEM *
970 TREEVIEW_AllocateItem(TREEVIEW_INFO *infoPtr)
972 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
977 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
986 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
987 * free item->pszText. */
989 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
991 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
993 if (infoPtr->selectedItem == item)
994 infoPtr->selectedItem = NULL;
995 if (infoPtr->hotItem == item)
996 infoPtr->hotItem = NULL;
997 if (infoPtr->focusedItem == item)
998 infoPtr->focusedItem = NULL;
999 if (infoPtr->firstVisible == item)
1000 infoPtr->firstVisible = NULL;
1001 if (infoPtr->dropItem == item)
1002 infoPtr->dropItem = NULL;
1003 if (infoPtr->insertMarkItem == item)
1004 infoPtr->insertMarkItem = NULL;
1008 /* Item Insertion *******************************************************/
1010 /***************************************************************************
1011 * This method inserts newItem before sibling as a child of parent.
1012 * sibling can be NULL, but only if parent has no children.
1015 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1016 TREEVIEW_ITEM *parent)
1018 assert(newItem != NULL);
1019 assert(parent != NULL);
1021 if (sibling != NULL)
1023 assert(sibling->parent == parent);
1025 if (sibling->prevSibling != NULL)
1026 sibling->prevSibling->nextSibling = newItem;
1028 newItem->prevSibling = sibling->prevSibling;
1029 sibling->prevSibling = newItem;
1032 newItem->prevSibling = NULL;
1034 newItem->nextSibling = sibling;
1036 if (parent->firstChild == sibling)
1037 parent->firstChild = newItem;
1039 if (parent->lastChild == NULL)
1040 parent->lastChild = newItem;
1043 /***************************************************************************
1044 * This method inserts newItem after sibling as a child of parent.
1045 * sibling can be NULL, but only if parent has no children.
1048 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1049 TREEVIEW_ITEM *parent)
1051 assert(newItem != NULL);
1052 assert(parent != NULL);
1054 if (sibling != NULL)
1056 assert(sibling->parent == parent);
1058 if (sibling->nextSibling != NULL)
1059 sibling->nextSibling->prevSibling = newItem;
1061 newItem->nextSibling = sibling->nextSibling;
1062 sibling->nextSibling = newItem;
1065 newItem->nextSibling = NULL;
1067 newItem->prevSibling = sibling;
1069 if (parent->lastChild == sibling)
1070 parent->lastChild = newItem;
1072 if (parent->firstChild == NULL)
1073 parent->firstChild = newItem;
1077 TREEVIEW_DoSetItemT(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1078 const TVITEMEXW *tvItem, BOOL isW)
1080 UINT callbackClear = 0;
1081 UINT callbackSet = 0;
1083 TRACE("item %p\n", wineItem);
1084 /* Do this first in case it fails. */
1085 if (tvItem->mask & TVIF_TEXT)
1087 wineItem->textWidth = 0; /* force width recalculation */
1088 if (tvItem->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
1093 len = lstrlenW(tvItem->pszText) + 1;
1095 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1097 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1099 if (newText == NULL) return FALSE;
1101 callbackClear |= TVIF_TEXT;
1103 wineItem->pszText = newText;
1104 wineItem->cchTextMax = len;
1106 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1108 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1109 wineItem->pszText, len);
1111 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1115 callbackSet |= TVIF_TEXT;
1117 wineItem->pszText = ReAlloc(wineItem->pszText,
1118 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1119 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1120 TRACE("setting callback, item %p\n", wineItem);
1124 if (tvItem->mask & TVIF_CHILDREN)
1126 wineItem->cChildren = tvItem->cChildren;
1128 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1129 callbackSet |= TVIF_CHILDREN;
1131 callbackClear |= TVIF_CHILDREN;
1134 if (tvItem->mask & TVIF_IMAGE)
1136 wineItem->iImage = tvItem->iImage;
1138 if (wineItem->iImage == I_IMAGECALLBACK)
1139 callbackSet |= TVIF_IMAGE;
1141 callbackClear |= TVIF_IMAGE;
1144 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1146 wineItem->iSelectedImage = tvItem->iSelectedImage;
1148 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1149 callbackSet |= TVIF_SELECTEDIMAGE;
1151 callbackClear |= TVIF_SELECTEDIMAGE;
1154 if (tvItem->mask & TVIF_PARAM)
1155 wineItem->lParam = tvItem->lParam;
1157 /* If the application sets TVIF_INTEGRAL without
1158 * supplying a TVITEMEX structure, it's toast. */
1159 if (tvItem->mask & TVIF_INTEGRAL)
1160 wineItem->iIntegral = tvItem->iIntegral;
1162 if (tvItem->mask & TVIF_STATE)
1164 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1166 wineItem->state &= ~tvItem->stateMask;
1167 wineItem->state |= (tvItem->state & tvItem->stateMask);
1170 wineItem->callbackMask |= callbackSet;
1171 wineItem->callbackMask &= ~callbackClear;
1176 /* Note that the new item is pre-zeroed. */
1178 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1180 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1181 HTREEITEM insertAfter;
1182 TREEVIEW_ITEM *newItem, *parentItem;
1183 BOOL bTextUpdated = FALSE;
1185 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1187 parentItem = infoPtr->root;
1191 parentItem = ptdi->hParent;
1193 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1195 WARN("invalid parent %p\n", parentItem);
1196 return (LRESULT)(HTREEITEM)NULL;
1200 insertAfter = ptdi->hInsertAfter;
1202 /* Validate this now for convenience. */
1203 switch ((DWORD)insertAfter)
1205 case (DWORD)TVI_FIRST:
1206 case (DWORD)TVI_LAST:
1207 case (DWORD)TVI_SORT:
1211 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1212 insertAfter->parent != parentItem)
1214 WARN("invalid insert after %p\n", insertAfter);
1215 insertAfter = TVI_LAST;
1219 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1220 (tvItem->mask & TVIF_TEXT)
1221 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1222 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1225 newItem = TREEVIEW_AllocateItem(infoPtr);
1226 if (newItem == NULL)
1227 return (LRESULT)(HTREEITEM)NULL;
1229 newItem->parent = parentItem;
1230 newItem->iIntegral = 1;
1232 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1233 return (LRESULT)(HTREEITEM)NULL;
1235 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1237 infoPtr->uNumItems++;
1239 switch ((DWORD)insertAfter)
1241 case (DWORD)TVI_FIRST:
1243 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1244 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1245 if (infoPtr->firstVisible == originalFirst)
1246 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1250 case (DWORD)TVI_LAST:
1251 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1254 /* hInsertAfter names a specific item we want to insert after */
1256 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1259 case (DWORD)TVI_SORT:
1261 TREEVIEW_ITEM *aChild;
1262 TREEVIEW_ITEM *previousChild = NULL;
1263 BOOL bItemInserted = FALSE;
1265 aChild = parentItem->firstChild;
1267 bTextUpdated = TRUE;
1268 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1270 /* Iterate the parent children to see where we fit in */
1271 while (aChild != NULL)
1275 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1276 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1278 if (comp < 0) /* we are smaller than the current one */
1280 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1281 bItemInserted = TRUE;
1284 else if (comp > 0) /* we are bigger than the current one */
1286 previousChild = aChild;
1288 /* This will help us to exit if there is no more sibling */
1289 aChild = (aChild->nextSibling == 0)
1291 : aChild->nextSibling;
1293 /* Look at the next item */
1299 * An item with this name is already existing, therefore,
1300 * we add after the one we found
1302 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1303 bItemInserted = TRUE;
1309 * we reach the end of the child list and the item has not
1310 * yet been inserted, therefore, insert it after the last child.
1312 if ((!bItemInserted) && (aChild == NULL))
1313 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1320 TRACE("new item %p; parent %p, mask %x\n", newItem,
1321 newItem->parent, tvItem->mask);
1323 newItem->iLevel = newItem->parent->iLevel + 1;
1325 if (newItem->parent->cChildren == 0)
1326 newItem->parent->cChildren = 1;
1328 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1330 if (STATEIMAGEINDEX(newItem->state) == 0)
1331 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1334 if (infoPtr->firstVisible == NULL)
1335 infoPtr->firstVisible = newItem;
1337 TREEVIEW_VerifyTree(infoPtr);
1339 if (parentItem == infoPtr->root ||
1340 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1342 TREEVIEW_ITEM *item;
1343 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1345 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1346 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1349 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1351 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1352 TREEVIEW_UpdateScrollBars(infoPtr);
1354 * if the item was inserted in a visible part of the tree,
1355 * invalidate it, as well as those after it
1357 for (item = newItem;
1359 item = TREEVIEW_GetNextListItem(infoPtr, item))
1360 TREEVIEW_Invalidate(infoPtr, item);
1364 newItem->visibleOrder = -1;
1366 /* refresh treeview if newItem is the first item inserted under parentItem */
1367 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1369 /* parent got '+' - update it */
1370 TREEVIEW_Invalidate(infoPtr, parentItem);
1374 return (LRESULT)newItem;
1377 /* Item Deletion ************************************************************/
1379 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1382 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1384 TREEVIEW_ITEM *kill = parentItem->firstChild;
1386 while (kill != NULL)
1388 TREEVIEW_ITEM *next = kill->nextSibling;
1390 TREEVIEW_RemoveItem(infoPtr, kill);
1395 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1396 assert(parentItem->firstChild == NULL);
1397 assert(parentItem->lastChild == NULL);
1401 TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1403 TREEVIEW_ITEM *parentItem = item->parent;
1405 assert(item != NULL);
1406 assert(item->parent != NULL); /* i.e. it must not be the root */
1408 if (parentItem->firstChild == item)
1409 parentItem->firstChild = item->nextSibling;
1411 if (parentItem->lastChild == item)
1412 parentItem->lastChild = item->prevSibling;
1414 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1415 && parentItem->cChildren > 0)
1416 parentItem->cChildren = 0;
1418 if (item->prevSibling)
1419 item->prevSibling->nextSibling = item->nextSibling;
1421 if (item->nextSibling)
1422 item->nextSibling->prevSibling = item->prevSibling;
1426 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1428 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1430 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1431 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1433 if (wineItem->firstChild)
1434 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1436 TREEVIEW_UnlinkItem(wineItem);
1438 infoPtr->uNumItems--;
1440 if (wineItem->pszText && wineItem->pszText != LPSTR_TEXTCALLBACKW)
1441 Free(wineItem->pszText);
1443 TREEVIEW_FreeItem(infoPtr, wineItem);
1447 /* Empty out the tree. */
1449 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1451 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1453 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1457 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1459 TREEVIEW_ITEM *newSelection = NULL;
1460 TREEVIEW_ITEM *newFirstVisible = NULL;
1461 TREEVIEW_ITEM *parent, *prev = NULL;
1462 BOOL visible = FALSE;
1464 if (wineItem == TVI_ROOT)
1466 TRACE("TVI_ROOT\n");
1467 parent = infoPtr->root;
1468 newSelection = NULL;
1470 TREEVIEW_RemoveTree(infoPtr);
1474 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1477 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1478 parent = wineItem->parent;
1480 if (ISVISIBLE(wineItem))
1482 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1486 if (infoPtr->selectedItem != NULL
1487 && (wineItem == infoPtr->selectedItem
1488 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1490 if (wineItem->nextSibling)
1491 newSelection = wineItem->nextSibling;
1492 else if (wineItem->parent != infoPtr->root)
1493 newSelection = wineItem->parent;
1495 newSelection = wineItem->prevSibling;
1496 TRACE("newSelection = %p\n", newSelection);
1499 if (infoPtr->firstVisible == wineItem)
1501 if (wineItem->nextSibling)
1502 newFirstVisible = wineItem->nextSibling;
1503 else if (wineItem->prevSibling)
1504 newFirstVisible = wineItem->prevSibling;
1505 else if (wineItem->parent != infoPtr->root)
1506 newFirstVisible = wineItem->parent;
1507 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1510 newFirstVisible = infoPtr->firstVisible;
1512 TREEVIEW_RemoveItem(infoPtr, wineItem);
1515 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1516 if (!infoPtr->selectedItem && newSelection)
1518 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1519 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1522 /* Validate insertMark dropItem.
1523 * hotItem ??? - used for comparison only.
1525 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1526 infoPtr->insertMarkItem = 0;
1528 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1529 infoPtr->dropItem = 0;
1531 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1532 newFirstVisible = infoPtr->root->firstChild;
1534 TREEVIEW_VerifyTree(infoPtr);
1539 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1540 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1541 TREEVIEW_UpdateScrollBars(infoPtr);
1542 TREEVIEW_Invalidate(infoPtr, NULL);
1544 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1546 /* parent lost '+/-' - update it */
1547 TREEVIEW_Invalidate(infoPtr, parent);
1554 /* Get/Set Messages *********************************************************/
1556 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1559 infoPtr->bRedraw = TRUE;
1561 infoPtr->bRedraw = FALSE;
1567 TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1570 return infoPtr->uIndent;
1574 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1578 if (newIndent < MINIMUM_INDENT)
1579 newIndent = MINIMUM_INDENT;
1581 if (infoPtr->uIndent != newIndent)
1583 infoPtr->uIndent = newIndent;
1584 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1585 TREEVIEW_UpdateScrollBars(infoPtr);
1586 TREEVIEW_Invalidate(infoPtr, NULL);
1594 TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1597 return (LRESULT)infoPtr->hwndToolTip;
1601 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1606 prevToolTip = infoPtr->hwndToolTip;
1607 infoPtr->hwndToolTip = hwndTT;
1609 return (LRESULT)prevToolTip;
1613 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1615 BOOL rc = infoPtr->bNtfUnicode;
1616 infoPtr->bNtfUnicode = fUnicode;
1621 TREEVIEW_GetUnicodeFormat(TREEVIEW_INFO *infoPtr)
1623 return infoPtr->bNtfUnicode;
1627 TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1629 return infoPtr->uScrollTime;
1633 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1635 UINT uOldScrollTime = infoPtr->uScrollTime;
1637 infoPtr->uScrollTime = min(uScrollTime, 100);
1639 return uOldScrollTime;
1644 TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1650 case (WPARAM)TVSIL_NORMAL:
1651 return (LRESULT)infoPtr->himlNormal;
1653 case (WPARAM)TVSIL_STATE:
1654 return (LRESULT)infoPtr->himlState;
1661 #define TVHEIGHT_MIN 16
1662 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1664 /* Compute the natural height for items. */
1666 TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1670 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1673 /* Height is the maximum of:
1674 * 16 (a hack because our fonts are tiny), and
1675 * The text height + border & margin, and
1676 * The size of the normal image list
1678 GetTextMetricsW(hdc, &tm);
1679 SelectObject(hdc, hOldFont);
1682 height = TVHEIGHT_MIN;
1683 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1684 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1685 if (height < infoPtr->normalImageHeight)
1686 height = infoPtr->normalImageHeight;
1691 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1693 HIMAGELIST himlOld = 0;
1694 int oldWidth = infoPtr->normalImageWidth;
1695 int oldHeight = infoPtr->normalImageHeight;
1698 TRACE("%x,%p\n", wParam, himlNew);
1702 case (WPARAM)TVSIL_NORMAL:
1703 himlOld = infoPtr->himlNormal;
1704 infoPtr->himlNormal = himlNew;
1706 if (himlNew != NULL)
1707 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1708 &infoPtr->normalImageHeight);
1711 infoPtr->normalImageWidth = 0;
1712 infoPtr->normalImageHeight = 0;
1717 case (WPARAM)TVSIL_STATE:
1718 himlOld = infoPtr->himlState;
1719 infoPtr->himlState = himlNew;
1721 if (himlNew != NULL)
1722 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1723 &infoPtr->stateImageHeight);
1726 infoPtr->stateImageWidth = 0;
1727 infoPtr->stateImageHeight = 0;
1733 if (oldWidth != infoPtr->normalImageWidth ||
1734 oldHeight != infoPtr->normalImageHeight)
1736 BOOL bRecalcVisible = FALSE;
1738 if (oldHeight != infoPtr->normalImageHeight &&
1739 !infoPtr->bHeightSet)
1741 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1742 bRecalcVisible = TRUE;
1745 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1746 infoPtr->normalImageWidth != infoPtr->uIndent)
1748 infoPtr->uIndent = infoPtr->normalImageWidth;
1749 bRecalcVisible = TRUE;
1753 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1755 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1756 TREEVIEW_UpdateScrollBars(infoPtr);
1759 TREEVIEW_Invalidate(infoPtr, NULL);
1761 return (LRESULT)himlOld;
1765 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1767 INT prevHeight = infoPtr->uItemHeight;
1769 TRACE("%d \n", newHeight);
1770 if (newHeight == -1)
1772 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1773 infoPtr->bHeightSet = FALSE;
1777 infoPtr->uItemHeight = newHeight;
1778 infoPtr->bHeightSet = TRUE;
1781 /* Round down, unless we support odd ("non even") heights. */
1782 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1783 infoPtr->uItemHeight &= ~1;
1785 if (infoPtr->uItemHeight != prevHeight)
1787 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1788 TREEVIEW_UpdateScrollBars(infoPtr);
1789 TREEVIEW_Invalidate(infoPtr, NULL);
1796 TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1799 return infoPtr->uItemHeight;
1804 TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1806 TRACE("%p\n", infoPtr->hFont);
1807 return (LRESULT)infoPtr->hFont;
1812 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1816 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1822 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1824 UINT uHeight = infoPtr->uItemHeight;
1826 TRACE("%p %i\n", hFont, bRedraw);
1828 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1830 DeleteObject(infoPtr->hBoldFont);
1831 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1832 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1834 if (!infoPtr->bHeightSet)
1835 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1837 if (uHeight != infoPtr->uItemHeight)
1838 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1840 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1842 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1843 TREEVIEW_UpdateScrollBars(infoPtr);
1846 TREEVIEW_Invalidate(infoPtr, NULL);
1853 TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1856 return (LRESULT)infoPtr->clrLine;
1860 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1862 COLORREF prevColor = infoPtr->clrLine;
1865 infoPtr->clrLine = color;
1866 return (LRESULT)prevColor;
1871 TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1874 return (LRESULT)infoPtr->clrText;
1878 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1880 COLORREF prevColor = infoPtr->clrText;
1883 infoPtr->clrText = color;
1885 if (infoPtr->clrText != prevColor)
1886 TREEVIEW_Invalidate(infoPtr, NULL);
1888 return (LRESULT)prevColor;
1893 TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1896 return (LRESULT)infoPtr->clrBk;
1900 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1902 COLORREF prevColor = infoPtr->clrBk;
1905 infoPtr->clrBk = newColor;
1907 if (newColor != prevColor)
1908 TREEVIEW_Invalidate(infoPtr, NULL);
1910 return (LRESULT)prevColor;
1915 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1918 return (LRESULT)infoPtr->clrInsertMark;
1922 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1924 COLORREF prevColor = infoPtr->clrInsertMark;
1926 TRACE("%lx\n", color);
1927 infoPtr->clrInsertMark = color;
1929 return (LRESULT)prevColor;
1934 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1936 TRACE("%d %p\n", wParam, item);
1938 if (!TREEVIEW_ValidItem(infoPtr, item))
1941 infoPtr->insertBeforeorAfter = wParam;
1942 infoPtr->insertMarkItem = item;
1944 TREEVIEW_Invalidate(infoPtr, NULL);
1950 /************************************************************************
1951 * Some serious braindamage here. lParam is a pointer to both the
1952 * input HTREEITEM and the output RECT.
1955 TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1957 TREEVIEW_ITEM *wineItem;
1958 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1962 * validate parameters
1968 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
1972 * If wParam is TRUE return the text size otherwise return
1973 * the whole item size
1977 /* Windows does not send TVN_GETDISPINFO here. */
1979 lpRect->top = wineItem->rect.top;
1980 lpRect->bottom = wineItem->rect.bottom;
1982 lpRect->left = wineItem->textOffset;
1983 lpRect->right = wineItem->textOffset + wineItem->textWidth;
1987 *lpRect = wineItem->rect;
1990 TRACE("%s [L:%ld R:%ld T:%ld B:%ld]\n", fTextRect ? "text" : "item",
1991 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
1996 static inline LRESULT
1997 TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
1999 /* Suprise! This does not take integral height into account. */
2000 return infoPtr->clientHeight / infoPtr->uItemHeight;
2005 TREEVIEW_GetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2007 TREEVIEW_ITEM *wineItem;
2009 wineItem = tvItem->hItem;
2010 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2013 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2015 if (tvItem->mask & TVIF_CHILDREN)
2017 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2018 FIXME("I_CHILDRENCALLBACK not supported\n");
2019 tvItem->cChildren = wineItem->cChildren;
2022 if (tvItem->mask & TVIF_HANDLE)
2023 tvItem->hItem = wineItem;
2025 if (tvItem->mask & TVIF_IMAGE)
2026 tvItem->iImage = wineItem->iImage;
2028 if (tvItem->mask & TVIF_INTEGRAL)
2029 tvItem->iIntegral = wineItem->iIntegral;
2031 /* undocumented: windows ignores TVIF_PARAM and
2032 * * always sets lParam
2034 tvItem->lParam = wineItem->lParam;
2036 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2037 tvItem->iSelectedImage = wineItem->iSelectedImage;
2039 if (tvItem->mask & TVIF_STATE)
2040 /* Careful here - Windows ignores the stateMask when you get the state
2041 That contradicts the documentation, but makes more common sense, masking
2042 retrieval in this way seems overkill */
2043 tvItem->state = wineItem->state;
2045 if (tvItem->mask & TVIF_TEXT)
2049 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2051 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2052 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2056 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2061 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2063 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2064 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2068 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2069 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2073 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2074 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2079 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2080 * which is wrong. */
2082 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2084 TREEVIEW_ITEM *wineItem;
2085 TREEVIEW_ITEM originalItem;
2087 wineItem = tvItem->hItem;
2089 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2092 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2095 /* store the orignal item values */
2096 originalItem = *wineItem;
2098 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2101 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2102 if ((tvItem->mask & TVIF_TEXT
2103 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2104 && ISVISIBLE(wineItem))
2106 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2107 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2110 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2112 /* The refresh updates everything, but we can't wait until then. */
2113 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2115 /* if any of the items values changed, redraw the item */
2116 if(memcmp(&originalItem, wineItem, sizeof(TREEVIEW_ITEM)) ||
2117 (tvItem->stateMask & TVIS_BOLD))
2119 if (tvItem->mask & TVIF_INTEGRAL)
2121 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2122 TREEVIEW_UpdateScrollBars(infoPtr);
2124 TREEVIEW_Invalidate(infoPtr, NULL);
2128 TREEVIEW_UpdateScrollBars(infoPtr);
2129 TREEVIEW_Invalidate(infoPtr, wineItem);
2138 TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2142 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2145 return (wineItem->state & mask);
2149 TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2151 TREEVIEW_ITEM *retval;
2155 /* handle all the global data here */
2158 case TVGN_CHILD: /* Special case: child of 0 is root */
2163 retval = infoPtr->root->firstChild;
2167 retval = infoPtr->selectedItem;
2170 case TVGN_FIRSTVISIBLE:
2171 retval = infoPtr->firstVisible;
2174 case TVGN_DROPHILITE:
2175 retval = infoPtr->dropItem;
2178 case TVGN_LASTVISIBLE:
2179 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2185 TRACE("flags:%x, returns %p\n", which, retval);
2186 return (LRESULT)retval;
2189 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2191 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2197 retval = wineItem->nextSibling;
2200 retval = wineItem->prevSibling;
2203 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2206 retval = wineItem->firstChild;
2208 case TVGN_NEXTVISIBLE:
2209 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2211 case TVGN_PREVIOUSVISIBLE:
2212 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2215 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2219 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2220 return (LRESULT)retval;
2225 TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2227 TRACE(" %d\n", infoPtr->uNumItems);
2228 return (LRESULT)infoPtr->uNumItems;
2232 TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2234 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2236 static const unsigned int state_table[] = { 0, 2, 1 };
2240 state = STATEIMAGEINDEX(item->state);
2241 TRACE("state:%x\n", state);
2242 item->state &= ~TVIS_STATEIMAGEMASK;
2245 state = state_table[state];
2247 item->state |= INDEXTOSTATEIMAGEMASK(state);
2249 TRACE("state:%x\n", state);
2250 TREEVIEW_Invalidate(infoPtr, item);
2255 /* Painting *************************************************************/
2257 /* Draw the lines and expand button for an item. Also draws one section
2258 * of the line from item's parent to item's parent's next sibling. */
2260 TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2262 LONG centerx, centery;
2263 BOOL lar = ((infoPtr->dwStyle
2264 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2267 if (!lar && item->iLevel == 0)
2270 centerx = (item->linesOffset + item->stateOffset) / 2;
2271 centery = (item->rect.top + item->rect.bottom) / 2;
2273 if (infoPtr->dwStyle & TVS_HASLINES)
2275 HPEN hOldPen, hNewPen;
2279 * Get a dotted grey pen
2281 hNewPen = CreatePen(PS_ALTERNATE, 0, infoPtr->clrLine);
2282 hOldPen = SelectObject(hdc, hNewPen);
2284 MoveToEx(hdc, item->stateOffset, centery, NULL);
2285 LineTo(hdc, centerx - 1, centery);
2287 if (item->prevSibling || item->parent != infoPtr->root)
2289 MoveToEx(hdc, centerx, item->rect.top, NULL);
2290 LineTo(hdc, centerx, centery);
2293 if (item->nextSibling)
2295 MoveToEx(hdc, centerx, centery, NULL);
2296 LineTo(hdc, centerx, item->rect.bottom + 1);
2299 /* Draw the line from our parent to its next sibling. */
2300 parent = item->parent;
2301 while (parent != infoPtr->root)
2303 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2305 if (parent->nextSibling
2306 /* skip top-levels unless TVS_LINESATROOT */
2307 && parent->stateOffset > parent->linesOffset)
2309 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2310 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2313 parent = parent->parent;
2316 SelectObject(hdc, hOldPen);
2317 DeleteObject(hNewPen);
2321 * Display the (+/-) signs
2324 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2326 if (item->cChildren)
2328 LONG height = item->rect.bottom - item->rect.top;
2329 LONG width = item->stateOffset - item->linesOffset;
2330 LONG rectsize = min(height, width) / 4;
2331 /* plussize = ceil(rectsize * 3/4) */
2332 LONG plussize = (rectsize + 1) * 3 / 4;
2334 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2335 HPEN hOldPen = SelectObject(hdc, hNewPen);
2336 HBRUSH hbr = CreateSolidBrush(infoPtr->clrBk);
2337 HBRUSH hbrOld = SelectObject(hdc, hbr);
2339 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2340 centerx + rectsize + 2, centery + rectsize + 2);
2342 SelectObject(hdc, hbrOld);
2345 SelectObject(hdc, hOldPen);
2346 DeleteObject(hNewPen);
2348 if (height < 18 || width < 18)
2350 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2351 LineTo(hdc, centerx + plussize, centery);
2353 if (!(item->state & TVIS_EXPANDED))
2355 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2356 LineTo(hdc, centerx, centery + plussize);
2361 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2362 centerx + plussize, centery + 2);
2364 if (!(item->state & TVIS_EXPANDED))
2366 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2367 centerx + 2, centery + plussize);
2368 SetPixel(hdc, centerx - 1, centery, infoPtr->clrBk);
2369 SetPixel(hdc, centerx + 1, centery, infoPtr->clrBk);
2377 TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2383 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2385 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2387 /* The custom draw handler can query the text rectangle,
2389 /* should already be known, set to 0 when changed */
2390 if (!wineItem->textWidth)
2391 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2395 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2397 cditem = TREEVIEW_SendCustomDrawItemNotify
2398 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT);
2399 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2401 if (cditem & CDRF_SKIPDEFAULT)
2403 SelectObject(hdc, hOldFont);
2408 if (cditem & CDRF_NEWFONT)
2409 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2411 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2413 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2416 * Display the images associated with this item
2421 /* State images are displayed to the left of the Normal image
2422 * image number is in state; zero should be `display no image'.
2424 imageIndex = STATEIMAGEINDEX(wineItem->state);
2426 if (infoPtr->himlState && imageIndex)
2428 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2429 wineItem->stateOffset,
2430 centery - infoPtr->stateImageHeight / 2,
2434 /* Now, draw the normal image; can be either selected or
2435 * non-selected image.
2438 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage))
2440 /* The item is currently selected */
2441 imageIndex = wineItem->iSelectedImage;
2445 /* The item is not selected */
2446 imageIndex = wineItem->iImage;
2449 if (infoPtr->himlNormal)
2451 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2453 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2454 wineItem->imageOffset,
2455 centery - infoPtr->normalImageHeight / 2,
2456 ILD_NORMAL | ovlIdx);
2462 * Display the text associated with this item
2465 /* Don't paint item's text if it's being edited */
2466 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2468 if (wineItem->pszText)
2470 COLORREF oldTextColor = 0;
2473 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2476 oldBkMode = SetBkMode(hdc, TRANSPARENT);
2478 /* - If item is drop target or it is selected and window is in focus -
2479 * use blue background (COLOR_HIGHLIGHT).
2480 * - If item is selected, window is not in focus, but it has style
2481 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2482 * - Otherwise - don't fill background
2484 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2485 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2486 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2488 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2490 hbrBk = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
2492 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2496 hbrBk = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
2498 if (infoPtr->clrText == -1)
2500 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2502 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2507 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2508 oldTextColor = SetTextColor(hdc, comctl32_color.clrHighlight);
2509 else if (infoPtr->clrText == -1)
2511 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2513 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2516 rcText.top = wineItem->rect.top;
2517 rcText.bottom = wineItem->rect.bottom;
2518 rcText.left = wineItem->textOffset;
2519 rcText.right = rcText.left + wineItem->textWidth + 4;
2523 FillRect(hdc, &rcText, hbrBk);
2524 DeleteObject(hbrBk);
2527 /* Draw the box around the selected item */
2528 if ((wineItem == infoPtr->selectedItem) && inFocus)
2530 DrawFocusRect(hdc,&rcText);
2533 InflateRect(&rcText, -2, -1); /* allow for the focus rect */
2535 TRACE("drawing text %s at (%ld,%ld)-(%ld,%ld)\n",
2536 debugstr_w(wineItem->pszText),
2537 rcText.left, rcText.top, rcText.right, rcText.bottom);
2542 lstrlenW(wineItem->pszText),
2544 DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
2546 /* Restore the hdc state */
2547 SetTextColor(hdc, oldTextColor);
2549 if (oldBkMode != TRANSPARENT)
2550 SetBkMode(hdc, oldBkMode);
2554 /* Draw insertion mark if necessary */
2556 if (infoPtr->insertMarkItem)
2557 TRACE("item:%d,mark:%d\n",
2558 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2559 (int)infoPtr->insertMarkItem);
2561 if (wineItem == infoPtr->insertMarkItem)
2563 HPEN hNewPen, hOldPen;
2567 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2568 hOldPen = SelectObject(hdc, hNewPen);
2570 if (infoPtr->insertBeforeorAfter)
2571 offset = wineItem->rect.bottom - 1;
2573 offset = wineItem->rect.top + 1;
2575 left = wineItem->textOffset - 2;
2576 right = wineItem->textOffset + wineItem->textWidth + 2;
2578 MoveToEx(hdc, left, offset - 3, NULL);
2579 LineTo(hdc, left, offset + 4);
2581 MoveToEx(hdc, left, offset, NULL);
2582 LineTo(hdc, right + 1, offset);
2584 MoveToEx(hdc, right, offset + 3, NULL);
2585 LineTo(hdc, right, offset - 4);
2587 SelectObject(hdc, hOldPen);
2588 DeleteObject(hNewPen);
2591 if (cditem & CDRF_NOTIFYPOSTPAINT)
2593 cditem = TREEVIEW_SendCustomDrawItemNotify
2594 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT);
2595 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2598 SelectObject(hdc, hOldFont);
2601 /* Computes treeHeight and treeWidth and updates the scroll bars.
2604 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2606 TREEVIEW_ITEM *wineItem;
2607 HWND hwnd = infoPtr->hwnd;
2611 LONG scrollX = infoPtr->scrollX;
2613 infoPtr->treeWidth = 0;
2614 infoPtr->treeHeight = 0;
2616 /* We iterate through all visible items in order to get the tree height
2618 wineItem = infoPtr->root->firstChild;
2620 while (wineItem != NULL)
2622 if (ISVISIBLE(wineItem))
2624 /* actually we draw text at textOffset + 2 */
2625 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2626 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2628 /* This is scroll-adjusted, but we fix this below. */
2629 infoPtr->treeHeight = wineItem->rect.bottom;
2632 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2635 /* Fix the scroll adjusted treeHeight and treeWidth. */
2636 if (infoPtr->root->firstChild)
2637 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2639 infoPtr->treeWidth += infoPtr->scrollX;
2641 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2643 /* Adding one scroll bar may take up enough space that it forces us
2644 * to add the other as well. */
2645 if (infoPtr->treeHeight > infoPtr->clientHeight)
2649 if (infoPtr->treeWidth
2650 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2653 else if (infoPtr->treeWidth > infoPtr->clientWidth)
2656 if (!vert && horz && infoPtr->treeHeight
2657 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2660 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2662 si.cbSize = sizeof(SCROLLINFO);
2663 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2668 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2669 if ( si.nPage && NULL != infoPtr->firstVisible)
2671 si.nPos = infoPtr->firstVisible->visibleOrder;
2672 si.nMax = infoPtr->maxVisibleOrder - 1;
2674 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2676 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2677 ShowScrollBar(hwnd, SB_VERT, TRUE);
2678 infoPtr->uInternalStatus |= TV_VSCROLL;
2682 if (infoPtr->uInternalStatus & TV_VSCROLL)
2683 ShowScrollBar(hwnd, SB_VERT, FALSE);
2684 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2689 if (infoPtr->uInternalStatus & TV_VSCROLL)
2690 ShowScrollBar(hwnd, SB_VERT, FALSE);
2691 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2696 si.nPage = infoPtr->clientWidth;
2697 si.nPos = infoPtr->scrollX;
2698 si.nMax = infoPtr->treeWidth - 1;
2700 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2702 si.nPos = si.nMax - max( si.nPage-1, 0 );
2706 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2707 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2708 infoPtr->uInternalStatus |= TV_HSCROLL;
2710 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2714 if (infoPtr->uInternalStatus & TV_HSCROLL)
2715 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2716 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2721 if (infoPtr->scrollX != scrollX)
2723 TREEVIEW_HScroll(infoPtr,
2724 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2728 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2731 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2733 TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2735 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2738 GetClientRect(infoPtr->hwnd, &rect);
2739 FillRect(hDC, &rect, hBrush);
2740 DeleteObject(hBrush);
2746 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2748 HWND hwnd = infoPtr->hwnd;
2750 TREEVIEW_ITEM *wineItem;
2752 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2754 TRACE("empty window\n");
2758 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2761 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2763 ReleaseDC(hwnd, hdc);
2767 for (wineItem = infoPtr->root->firstChild;
2769 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2771 if (ISVISIBLE(wineItem))
2773 /* Avoid unneeded calculations */
2774 if (wineItem->rect.top > rect.bottom)
2776 if (wineItem->rect.bottom < rect.top)
2779 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2783 TREEVIEW_UpdateScrollBars(infoPtr);
2785 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2787 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2791 TREEVIEW_Invalidate(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2794 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2796 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2800 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2811 if (!GetUpdateRect(infoPtr->hwnd, &rc, TRUE))
2815 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2816 if (!hbitmap) return 0;
2817 GetObjectA(hbitmap, sizeof(BITMAP), &bitmap);
2818 rc.left = 0; rc.top = 0;
2819 rc.right = bitmap.bmWidth;
2820 rc.bottom = bitmap.bmHeight;
2821 TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
2826 hdc = BeginPaint(infoPtr->hwnd, &ps);
2830 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2831 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2834 EndPaint(infoPtr->hwnd, &ps);
2840 /* Sorting **************************************************************/
2842 /***************************************************************************
2843 * Forward the DPA local callback to the treeview owner callback
2846 TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2848 /* Forward the call to the client-defined callback */
2849 return pCallBackSort->lpfnCompare(first->lParam,
2851 pCallBackSort->lParam);
2854 /***************************************************************************
2855 * Treeview native sort routine: sort on item text.
2858 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2859 TREEVIEW_INFO *infoPtr)
2861 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2862 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2864 if(first->pszText && second->pszText)
2865 return lstrcmpiW(first->pszText, second->pszText);
2866 else if(first->pszText)
2868 else if(second->pszText)
2874 /* Returns the number of physical children belonging to item. */
2876 TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2881 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2887 /* Returns a DPA containing a pointer to each physical child of item in
2888 * sibling order. If item has no children, an empty DPA is returned. */
2890 TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2892 HTREEITEM child = item->firstChild;
2894 HDPA list = DPA_Create(8);
2895 if (list == 0) return NULL;
2897 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2899 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2909 /***************************************************************************
2910 * Setup the treeview structure with regards of the sort method
2911 * and sort the children of the TV item specified in lParam
2912 * fRecurse: currently unused. Should be zero.
2913 * parent: if pSort!=NULL, should equal pSort->hParent.
2914 * otherwise, item which child items are to be sorted.
2915 * pSort: sort method info. if NULL, sort on item text.
2916 * if non-NULL, sort on item's lParam content, and let the
2917 * application decide what that means. See also TVM_SORTCHILDRENCB.
2921 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2925 PFNDPACOMPARE pfnCompare;
2928 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2929 if (parent == TVI_ROOT || parent == NULL)
2930 parent = infoPtr->root;
2932 /* Check for a valid handle to the parent item */
2933 if (!TREEVIEW_ValidItem(infoPtr, parent))
2935 ERR("invalid item hParent=%x\n", (INT)parent);
2941 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2942 lpCompare = (LPARAM)pSort;
2946 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2947 lpCompare = (LPARAM)infoPtr;
2950 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2952 /* Make sure there is something to sort */
2955 /* TREEVIEW_ITEM rechaining */
2958 HTREEITEM nextItem = 0;
2959 HTREEITEM prevItem = 0;
2961 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
2963 if (sortList == NULL)
2966 /* let DPA sort the list */
2967 DPA_Sort(sortList, pfnCompare, lpCompare);
2969 /* The order of DPA entries has been changed, so fixup the
2970 * nextSibling and prevSibling pointers. */
2972 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
2973 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
2975 /* link the two current item toghether */
2976 item->nextSibling = nextItem;
2977 nextItem->prevSibling = item;
2979 if (prevItem == NULL)
2981 /* this is the first item, update the parent */
2982 parent->firstChild = item;
2983 item->prevSibling = NULL;
2987 /* fix the back chaining */
2988 item->prevSibling = prevItem;
2991 /* get ready for the next one */
2996 /* the last item is pointed to by item and never has a sibling */
2997 item->nextSibling = NULL;
2998 parent->lastChild = item;
3000 DPA_Destroy(sortList);
3002 TREEVIEW_VerifyTree(infoPtr);
3004 if (parent->state & TVIS_EXPANDED)
3006 int visOrder = infoPtr->firstVisible->visibleOrder;
3008 if (parent == infoPtr->root)
3009 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3011 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3013 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3015 TREEVIEW_ITEM *item;
3017 for (item = infoPtr->root->firstChild; item != NULL;
3018 item = TREEVIEW_GetNextListItem(infoPtr, item))
3020 if (item->visibleOrder == visOrder)
3024 if (!item) item = parent->firstChild;
3025 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3028 TREEVIEW_Invalidate(infoPtr, NULL);
3037 /***************************************************************************
3038 * Setup the treeview structure with regards of the sort method
3039 * and sort the children of the TV item specified in lParam
3042 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3044 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3048 /***************************************************************************
3049 * Sort the children of the TV item specified in lParam.
3052 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3054 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3058 /* Expansion/Collapse ***************************************************/
3061 TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3064 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3065 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3066 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3071 TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3074 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3075 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3076 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3081 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3082 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3084 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3085 BOOL bRemoveChildren, BOOL bUser)
3087 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3088 BOOL bSetSelection, bSetFirstVisible;
3090 LONG scrollDist = 0;
3091 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3093 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3095 if (!(wineItem->state & TVIS_EXPANDED))
3098 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3099 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3101 if (wineItem->firstChild == NULL)
3104 wineItem->state &= ~TVIS_EXPANDED;
3106 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3107 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3109 bSetSelection = (infoPtr->selectedItem != NULL
3110 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3112 bSetFirstVisible = (infoPtr->firstVisible != NULL
3113 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3118 if (tmpItem->nextSibling)
3120 nextItem = tmpItem->nextSibling;
3123 tmpItem = tmpItem->parent;
3127 scrollDist = nextItem->rect.top;
3129 if (bRemoveChildren)
3131 INT old_cChildren = wineItem->cChildren;
3132 TRACE("TVE_COLLAPSERESET\n");
3133 wineItem->state &= ~TVIS_EXPANDEDONCE;
3134 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3135 wineItem->cChildren = old_cChildren;
3138 if (wineItem->firstChild)
3140 TREEVIEW_ITEM *item, *sibling;
3142 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3144 for (item = wineItem->firstChild; item != sibling;
3145 item = TREEVIEW_GetNextListItem(infoPtr, item))
3147 item->visibleOrder = -1;
3151 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3154 scrollDist = -(scrollDist - nextItem->rect.top);
3158 /* Don't call DoSelectItem, it sends notifications. */
3159 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3160 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3161 wineItem->state |= TVIS_SELECTED;
3162 infoPtr->selectedItem = wineItem;
3165 TREEVIEW_UpdateScrollBars(infoPtr);
3167 scrollRect.left = 0;
3168 scrollRect.right = infoPtr->clientWidth;
3169 scrollRect.bottom = infoPtr->clientHeight;
3173 scrollRect.top = nextItem->rect.top;
3175 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3176 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3177 TREEVIEW_Invalidate(infoPtr, wineItem);
3179 scrollRect.top = wineItem->rect.top;
3180 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3183 TREEVIEW_SetFirstVisible(infoPtr,
3184 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3191 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3192 BOOL bExpandPartial, BOOL bUser)
3195 LONG orgNextTop = 0;
3197 TREEVIEW_ITEM *nextItem, *tmpItem;
3201 if (wineItem->state & TVIS_EXPANDED)
3204 tmpItem = wineItem; nextItem = NULL;
3207 if (tmpItem->nextSibling)
3209 nextItem = tmpItem->nextSibling;
3212 tmpItem = tmpItem->parent;
3216 orgNextTop = nextItem->rect.top;
3218 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3220 if (bUser || ((wineItem->cChildren != 0) &&
3221 !(wineItem->state & TVIS_EXPANDEDONCE)))
3223 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3225 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3229 if (!wineItem->firstChild)
3232 wineItem->state |= TVIS_EXPANDED;
3233 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3234 wineItem->state |= TVIS_EXPANDEDONCE;
3238 if (!wineItem->firstChild)
3241 /* this item has already been expanded */
3242 wineItem->state |= TVIS_EXPANDED;
3246 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3248 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3249 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3250 TREEVIEW_UpdateScrollBars(infoPtr);
3252 scrollRect.left = 0;
3253 scrollRect.bottom = infoPtr->treeHeight;
3254 scrollRect.right = infoPtr->clientWidth;
3257 scrollDist = nextItem->rect.top - orgNextTop;
3258 scrollRect.top = orgNextTop;
3260 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3261 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3262 TREEVIEW_Invalidate (infoPtr, wineItem);
3264 scrollRect.top = wineItem->rect.top;
3265 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3268 /* Scroll up so that as many children as possible are visible.
3269 * This fails when expanding causes an HScroll bar to appear, but we
3270 * don't know that yet, so the last item is obscured. */
3271 if (wineItem->firstChild != NULL)
3273 int nChildren = wineItem->lastChild->visibleOrder
3274 - wineItem->firstChild->visibleOrder + 1;
3276 int visible_pos = wineItem->visibleOrder
3277 - infoPtr->firstVisible->visibleOrder;
3279 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3281 if (visible_pos > 0 && nChildren > rows_below)
3283 int scroll = nChildren - rows_below;
3285 if (scroll > visible_pos)
3286 scroll = visible_pos;
3290 TREEVIEW_ITEM *newFirstVisible
3291 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3295 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3304 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3308 if (wineItem->state & TVIS_EXPANDED)
3309 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3311 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3315 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3317 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3319 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3321 if (TREEVIEW_HasChildren(infoPtr, item))
3322 TREEVIEW_ExpandAll(infoPtr, item);
3326 /* Note:If the specified item is the child of a collapsed parent item,
3327 the parent's list of child items is (recursively) expanded to reveal the
3328 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3329 know if it also applies here.
3333 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3335 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3338 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3339 TREEVIEW_ItemName(wineItem), flag,
3340 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3342 switch (flag & TVE_TOGGLE)
3345 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3349 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3353 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3360 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3364 /* Hit-Testing **********************************************************/
3366 static TREEVIEW_ITEM *
3367 TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3369 TREEVIEW_ITEM *wineItem;
3372 if (!infoPtr->firstVisible)
3375 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3377 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3378 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3380 if (row >= wineItem->visibleOrder
3381 && row < wineItem->visibleOrder + wineItem->iIntegral)
3389 TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3391 TREEVIEW_ITEM *wineItem;
3397 GetClientRect(infoPtr->hwnd, &rect);
3404 status |= TVHT_TOLEFT;
3406 else if (x > rect.right)
3408 status |= TVHT_TORIGHT;
3413 status |= TVHT_ABOVE;
3415 else if (y > rect.bottom)
3417 status |= TVHT_BELOW;
3422 lpht->flags = status;
3423 return (LRESULT)(HTREEITEM)NULL;
3426 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3429 lpht->flags = TVHT_NOWHERE;
3430 return (LRESULT)(HTREEITEM)NULL;
3433 if (x >= wineItem->textOffset + wineItem->textWidth)
3435 lpht->flags = TVHT_ONITEMRIGHT;
3437 else if (x >= wineItem->textOffset)
3439 lpht->flags = TVHT_ONITEMLABEL;
3441 else if (x >= wineItem->imageOffset)
3443 lpht->flags = TVHT_ONITEMICON;
3445 else if (x >= wineItem->stateOffset)
3447 lpht->flags = TVHT_ONITEMSTATEICON;
3449 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3451 lpht->flags = TVHT_ONITEMBUTTON;
3455 lpht->flags = TVHT_ONITEMINDENT;
3458 lpht->hItem = wineItem;
3459 TRACE("(%ld,%ld):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3461 return (LRESULT)wineItem;
3464 /* Item Label Editing ***************************************************/
3467 TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3469 return (LRESULT)infoPtr->hwndEdit;
3472 static LRESULT CALLBACK
3473 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3475 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3476 BOOL bCancel = FALSE;
3482 TRACE("WM_PAINT start\n");
3483 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3485 TRACE("WM_PAINT done\n");
3489 if (infoPtr->bIgnoreEditKillFocus)
3494 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3497 if (wParam == (WPARAM)VK_ESCAPE)
3502 else if (wParam == (WPARAM)VK_RETURN)
3509 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3512 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3513 /* eg. Using a messagebox */
3515 infoPtr->bIgnoreEditKillFocus = TRUE;
3516 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3517 infoPtr->bIgnoreEditKillFocus = FALSE;
3523 /* should handle edit control messages here */
3526 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3528 TRACE("%x %ld\n", wParam, lParam);
3530 switch (HIWORD(wParam))
3535 * Adjust the edit window size
3538 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3539 HDC hdc = GetDC(infoPtr->hwndEdit);
3542 HFONT hFont, hOldFont = 0;
3544 infoPtr->bLabelChanged = TRUE;
3546 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer));
3548 /* Select font to get the right dimension of the string */
3549 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3553 hOldFont = SelectObject(hdc, hFont);
3556 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3558 TEXTMETRICW textMetric;
3560 /* Add Extra spacing for the next character */
3561 GetTextMetricsW(hdc, &textMetric);
3562 sz.cx += (textMetric.tmMaxCharWidth * 2);
3564 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3566 infoPtr->clientWidth - editItem->textOffset + 2);
3568 SetWindowPos(infoPtr->hwndEdit,
3573 editItem->rect.bottom - editItem->rect.top + 3,
3574 SWP_NOMOVE | SWP_DRAWFRAME);
3579 SelectObject(hdc, hOldFont);
3582 ReleaseDC(infoPtr->hwnd, hdc);
3587 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3594 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3596 HWND hwnd = infoPtr->hwnd;
3599 TREEVIEW_ITEM *editItem = hItem;
3600 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3603 TEXTMETRICW textMetric;
3604 static const WCHAR EditW[] = {'E','d','i','t',0};
3606 TRACE("%x %p\n", (unsigned)hwnd, hItem);
3607 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3610 if (infoPtr->hwndEdit)
3611 return infoPtr->hwndEdit;
3613 infoPtr->bLabelChanged = FALSE;
3615 /* Make sure that edit item is selected */
3616 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3617 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3619 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3622 /* Select the font to get appropriate metric dimensions */
3623 if (infoPtr->hFont != 0)
3625 hOldFont = SelectObject(hdc, infoPtr->hFont);
3628 /* Get string length in pixels */
3629 GetTextExtentPoint32W(hdc, editItem->pszText, strlenW(editItem->pszText),
3632 /* Add Extra spacing for the next character */
3633 GetTextMetricsW(hdc, &textMetric);
3634 sz.cx += (textMetric.tmMaxCharWidth * 2);
3636 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3637 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3639 if (infoPtr->hFont != 0)
3641 SelectObject(hdc, hOldFont);
3644 ReleaseDC(hwnd, hdc);
3645 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3648 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3649 WS_CLIPSIBLINGS | ES_WANTRETURN |
3650 ES_LEFT, editItem->textOffset - 2,
3651 editItem->rect.top - 1, sz.cx + 3,
3652 editItem->rect.bottom -
3653 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3654 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3656 infoPtr->hwndEdit = hwndEdit;
3658 /* Get a 2D border. */
3659 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3660 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3661 SetWindowLongW(hwndEdit, GWL_STYLE,
3662 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3664 SendMessageW(hwndEdit, WM_SETFONT,
3665 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3667 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3669 TREEVIEW_Edit_SubclassProc);
3671 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3673 DestroyWindow(hwndEdit);
3674 infoPtr->hwndEdit = 0;
3678 infoPtr->selectedItem = hItem;
3679 SetWindowTextW(hwndEdit, editItem->pszText);
3681 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3682 ShowWindow(hwndEdit, SW_SHOW);
3689 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3691 HWND hwnd = infoPtr->hwnd;
3692 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3695 WCHAR tmpText[1024] = { '\0' };
3696 WCHAR *newText = tmpText;
3699 if (!infoPtr->hwndEdit)
3702 tvdi.hdr.hwndFrom = hwnd;
3703 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3704 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3706 tvdi.item.hItem = editedItem;
3707 tvdi.item.state = editedItem->state;
3708 tvdi.item.lParam = editedItem->lParam;
3712 if (!infoPtr->bNtfUnicode)
3713 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3715 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3717 if (iLength >= 1023)
3719 ERR("Insufficient space to retrieve new item label\n");
3722 tvdi.item.mask = TVIF_TEXT;
3723 tvdi.item.pszText = tmpText;
3724 tvdi.item.cchTextMax = iLength + 1;
3728 tvdi.item.pszText = NULL;
3729 tvdi.item.cchTextMax = 0;
3732 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
3733 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3735 if (!bCancel && bCommit) /* Apply the changes */
3737 if (!infoPtr->bNtfUnicode)
3739 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3740 newText = Alloc(len * sizeof(WCHAR));
3741 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3745 if (strcmpW(newText, editedItem->pszText) != 0)
3747 if (NULL == ReAlloc(editedItem->pszText, iLength + 1))
3749 ERR("OutOfMemory, cannot allocate space for label\n");
3750 DestroyWindow(infoPtr->hwndEdit);
3751 infoPtr->hwndEdit = 0;
3756 editedItem->cchTextMax = iLength + 1;
3757 strcpyW(editedItem->pszText, newText);
3760 if(newText != tmpText) Free(newText);
3763 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3764 DestroyWindow(infoPtr->hwndEdit);
3765 infoPtr->hwndEdit = 0;
3770 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3772 if (wParam != TV_EDIT_TIMER)
3774 ERR("got unknown timer\n");
3778 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3779 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3781 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3787 /* Mouse Tracking/Drag **************************************************/
3789 /***************************************************************************
3790 * This is quite unusual piece of code, but that's how it's implemented in
3794 TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3796 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3797 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3801 r.top = pt.y - cyDrag;
3802 r.left = pt.x - cxDrag;
3803 r.bottom = pt.y + cyDrag;
3804 r.right = pt.x + cxDrag;
3806 SetCapture(infoPtr->hwnd);
3810 if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3812 if (msg.message == WM_MOUSEMOVE)
3814 pt.x = (short)LOWORD(msg.lParam);
3815 pt.y = (short)HIWORD(msg.lParam);
3816 if (PtInRect(&r, pt))
3824 else if (msg.message >= WM_LBUTTONDOWN &&
3825 msg.message <= WM_RBUTTONDBLCLK)
3827 if (msg.message == WM_RBUTTONUP)
3828 TREEVIEW_RButtonUp(infoPtr, &pt);
3832 DispatchMessageA(&msg);
3835 if (GetCapture() != infoPtr->hwnd)
3845 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3847 TREEVIEW_ITEM *wineItem;
3851 SetFocus(infoPtr->hwnd);
3853 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3855 /* If there is pending 'edit label' event - kill it now */
3856 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3859 hit.pt.x = (short)LOWORD(lParam);
3860 hit.pt.y = (short)HIWORD(lParam);
3862 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3865 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3867 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3871 case TVHT_ONITEMRIGHT:
3872 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3875 case TVHT_ONITEMINDENT:
3876 if (!(infoPtr->dwStyle & TVS_HASLINES))
3882 int level = hit.pt.x / infoPtr->uIndent;
3883 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3885 while (wineItem->iLevel > level)
3887 wineItem = wineItem->parent;
3893 case TVHT_ONITEMLABEL:
3894 case TVHT_ONITEMICON:
3895 case TVHT_ONITEMBUTTON:
3896 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3899 case TVHT_ONITEMSTATEICON:
3900 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3901 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3903 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3912 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3914 HWND hwnd = infoPtr->hwnd;
3916 BOOL bTrack, bDoLabelEdit;
3919 /* If Edit control is active - kill it and return.
3920 * The best way to do it is to set focus to itself.
3921 * Edit control subclassed procedure will automatically call
3924 if (infoPtr->hwndEdit)
3930 ht.pt.x = (short)LOWORD(lParam);
3931 ht.pt.y = (short)HIWORD(lParam);
3933 TREEVIEW_HitTest(infoPtr, &ht);
3934 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3936 /* update focusedItem and redraw both items */
3937 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3939 infoPtr->focusedItem = ht.hItem;
3940 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3942 if(infoPtr->selectedItem)
3943 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3946 bTrack = (ht.flags & TVHT_ONITEM)
3947 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3950 * If the style allows editing and the node is already selected
3951 * and the click occurred on the item label...
3953 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
3954 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
3956 /* Send NM_CLICK right away */
3958 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3961 if (ht.flags & TVHT_ONITEMBUTTON)
3963 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
3967 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
3968 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3970 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
3971 infoPtr->dropItem = ht.hItem;
3973 /* clean up focusedItem as we dragged and won't select this item */
3974 if(infoPtr->focusedItem)
3976 /* refresh the item that was focused */
3977 tempItem = infoPtr->focusedItem;
3978 infoPtr->focusedItem = 0;
3979 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
3981 /* refresh the selected item to return the filled background */
3982 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
3989 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3994 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3995 KillTimer(hwnd, TV_EDIT_TIMER);
3997 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
3998 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4000 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4003 * if we are TVS_SINGLEEXPAND then we want this single click to
4004 * do a bunch of things.
4006 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4007 (infoPtr->hwndEdit == 0))
4009 TREEVIEW_ITEM *SelItem;
4012 * Send the notification
4014 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4017 * Close the previous selection all the way to the root
4018 * as long as the new selection is not a child
4020 if((infoPtr->selectedItem)
4021 && (infoPtr->selectedItem != ht.hItem))
4023 BOOL closeit = TRUE;
4026 /* determine if the hitItem is a child of the currently selected item */
4027 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4029 closeit = (SelItem != infoPtr->selectedItem);
4030 SelItem = SelItem->parent;
4035 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4036 SelItem = infoPtr->selectedItem;
4038 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4040 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4041 SelItem = SelItem->parent;
4047 * Expand the current item
4049 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4052 /* Select the current item */
4053 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4055 else if (ht.flags & TVHT_ONITEMSTATEICON)
4057 /* TVS_CHECKBOXES requires us to toggle the current state */
4058 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4059 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4069 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4073 if (infoPtr->hwndEdit)
4075 SetFocus(infoPtr->hwnd);
4079 ht.pt.x = (short)LOWORD(lParam);
4080 ht.pt.y = (short)HIWORD(lParam);
4082 TREEVIEW_HitTest(infoPtr, &ht);
4084 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4088 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4089 infoPtr->dropItem = ht.hItem;
4094 SetFocus(infoPtr->hwnd);
4095 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4102 TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
4109 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4111 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4115 HBITMAP hbmp, hOldbmp;
4122 if (!(infoPtr->himlNormal))
4125 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4128 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4130 hwtop = GetDesktopWindow();
4131 htopdc = GetDC(hwtop);
4132 hdc = CreateCompatibleDC(htopdc);
4134 hOldFont = SelectObject(hdc, infoPtr->hFont);
4135 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4137 TRACE("%ld %ld %s %d\n", size.cx, size.cy, debugstr_w(dragItem->pszText),
4138 strlenW(dragItem->pszText));
4139 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4140 hOldbmp = SelectObject(hdc, hbmp);
4142 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4147 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4148 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4152 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4153 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4156 /* draw item text */
4158 SetRect(&rc, cx, 0, size.cx, size.cy);
4159 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4161 SelectObject(hdc, hOldFont);
4162 SelectObject(hdc, hOldbmp);
4164 ImageList_Add(infoPtr->dragList, hbmp, 0);
4168 ReleaseDC(hwtop, htopdc);
4170 return (LRESULT)infoPtr->dragList;
4173 /* Selection ************************************************************/
4176 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4179 TREEVIEW_ITEM *prevSelect;
4182 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4184 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4185 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4186 newSelect ? newSelect->state : 0);
4188 /* reset and redraw focusedItem if focusedItem was set so we don't */
4189 /* have to worry about the previously focused item when we set a new one */
4190 if(infoPtr->focusedItem)
4192 rcFocused = (infoPtr->focusedItem)->rect;
4193 infoPtr->focusedItem = 0;
4194 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4200 prevSelect = infoPtr->selectedItem;
4202 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4205 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4211 prevSelect->state &= ~TVIS_SELECTED;
4213 newSelect->state |= TVIS_SELECTED;
4215 infoPtr->selectedItem = newSelect;
4217 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4219 TREEVIEW_SendTreeviewNotify(infoPtr,
4222 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4225 TREEVIEW_Invalidate(infoPtr, prevSelect);
4226 TREEVIEW_Invalidate(infoPtr, newSelect);
4229 case TVGN_DROPHILITE:
4230 prevSelect = infoPtr->dropItem;
4233 prevSelect->state &= ~TVIS_DROPHILITED;
4235 infoPtr->dropItem = newSelect;
4238 newSelect->state |= TVIS_DROPHILITED;
4240 TREEVIEW_Invalidate(infoPtr, prevSelect);
4241 TREEVIEW_Invalidate(infoPtr, newSelect);
4244 case TVGN_FIRSTVISIBLE:
4245 if (newSelect != NULL)
4247 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4248 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4249 TREEVIEW_Invalidate(infoPtr, NULL);
4254 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4258 /* FIXME: handle NM_KILLFOCUS etc */
4260 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4262 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4265 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4267 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4273 /*************************************************************************
4274 * TREEVIEW_ProcessLetterKeys
4276 * Processes keyboard messages generated by pressing the letter keys
4278 * What this does is perform a case insensitive search from the
4279 * current position with the following quirks:
4280 * - If two chars or more are pressed in quick succession we search
4281 * for the corresponding string (e.g. 'abc').
4282 * - If there is a delay we wipe away the current search string and
4283 * restart with just that char.
4284 * - If the user keeps pressing the same character, whether slowly or
4285 * fast, so that the search string is entirely composed of this
4286 * character ('aaaaa' for instance), then we search for first item
4287 * that starting with that character.
4288 * - If the user types the above character in quick succession, then
4289 * we must also search for the corresponding string ('aaaaa'), and
4290 * go to that string if there is a match.
4298 * - The current implementation has a list of characters it will
4299 * accept and it ignores averything else. In particular it will
4300 * ignore accentuated characters which seems to match what
4301 * Windows does. But I'm not sure it makes sense to follow
4303 * - We don't sound a beep when the search fails.
4304 * - The search should start from the focused item, not from the selected
4305 * item. One reason for this is to allow for multiple selections in trees.
4306 * But currently infoPtr->focusedItem does not seem very usable.
4310 * TREEVIEW_ProcessLetterKeys
4312 static INT TREEVIEW_ProcessLetterKeys(
4313 HWND hwnd, /* handle to the window */
4314 WPARAM charCode, /* the character code, the actual character */
4315 LPARAM keyData /* key data */
4318 TREEVIEW_INFO *infoPtr;
4320 HTREEITEM endidx,idx;
4322 WCHAR buffer[MAX_PATH];
4323 DWORD timestamp,elapsed;
4325 /* simple parameter checking */
4326 if (!hwnd || !charCode || !keyData)
4329 infoPtr=(TREEVIEW_INFO*)GetWindowLongPtrW(hwnd, 0);
4333 /* only allow the valid WM_CHARs through */
4334 if (!isalnum(charCode) &&
4335 charCode != '.' && charCode != '`' && charCode != '!' &&
4336 charCode != '@' && charCode != '#' && charCode != '$' &&
4337 charCode != '%' && charCode != '^' && charCode != '&' &&
4338 charCode != '*' && charCode != '(' && charCode != ')' &&
4339 charCode != '-' && charCode != '_' && charCode != '+' &&
4340 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4341 charCode != '}' && charCode != '[' && charCode != '{' &&
4342 charCode != '/' && charCode != '?' && charCode != '>' &&
4343 charCode != '<' && charCode != ',' && charCode != '~')
4346 /* compute how much time elapsed since last keypress */
4347 timestamp = GetTickCount();
4348 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4349 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4351 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4354 /* update the search parameters */
4355 infoPtr->lastKeyPressTimestamp=timestamp;
4356 if (elapsed < KEY_DELAY) {
4357 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4358 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4360 if (infoPtr->charCode != charCode) {
4361 infoPtr->charCode=charCode=0;
4364 infoPtr->charCode=charCode;
4365 infoPtr->szSearchParam[0]=charCode;
4366 infoPtr->nSearchParamLength=1;
4367 /* Redundant with the 1 char string */
4371 /* and search from the current position */
4373 if (infoPtr->selectedItem != NULL) {
4374 endidx=infoPtr->selectedItem;
4375 /* if looking for single character match,
4376 * then we must always move forward
4378 if (infoPtr->nSearchParamLength == 1)
4379 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4384 idx=infoPtr->root->firstChild;
4390 idx=infoPtr->root->firstChild;
4394 ZeroMemory(&item, sizeof(item));
4395 item.mask = TVIF_TEXT;
4397 item.pszText = buffer;
4398 item.cchTextMax = sizeof(buffer);
4399 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4401 /* check for a match */
4402 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4405 } else if ( (charCode != 0) && (nItem == NULL) &&
4406 (nItem != infoPtr->selectedItem) &&
4407 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4408 /* This would work but we must keep looking for a longer match */
4411 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4412 } while (idx != endidx);
4414 if (nItem != NULL) {
4415 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4416 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4423 /* Scrolling ************************************************************/
4426 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4429 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4430 HTREEITEM newFirstVisible = NULL;
4431 int visible_pos = -1;
4433 if (!TREEVIEW_ValidItem(infoPtr, item))
4436 if (!ISVISIBLE(item))
4438 /* Expand parents as necessary. */
4441 /* see if we are trying to ensure that root is vislble */
4442 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4443 parent = item->parent;
4445 parent = item; /* this item is the topmost item */
4447 while (parent != infoPtr->root)
4449 if (!(parent->state & TVIS_EXPANDED))
4450 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4452 parent = parent->parent;
4456 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4458 TRACE("%p (%s) %ld - %ld viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4459 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4461 if (hasFirstVisible)
4462 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4464 if (visible_pos < 0)
4466 /* item is before the start of the list: put it at the top. */
4467 newFirstVisible = item;
4469 else if (visible_pos >= viscount
4470 /* Sometimes, before we are displayed, GVC is 0, causing us to
4471 * spuriously scroll up. */
4472 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4474 /* item is past the end of the list. */
4475 int scroll = visible_pos - viscount;
4477 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4483 /* Scroll window so item's text is visible as much as possible */
4484 /* Calculation of amount of extra space is taken from EditLabel code */
4486 TEXTMETRICW textMetric;
4487 HDC hdc = GetWindowDC(infoPtr->hwnd);
4489 x = item->textWidth;
4491 GetTextMetricsW(hdc, &textMetric);
4492 ReleaseDC(infoPtr->hwnd, hdc);
4494 x += (textMetric.tmMaxCharWidth * 2);
4495 x = max(x, textMetric.tmMaxCharWidth * 3);
4497 if (item->textOffset < 0)
4498 pos = item->textOffset;
4499 else if (item->textOffset + x > infoPtr->clientWidth)
4501 if (x > infoPtr->clientWidth)
4502 pos = item->textOffset;
4504 pos = item->textOffset + x - infoPtr->clientWidth;
4509 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4512 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4514 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4523 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4524 TREEVIEW_ITEM *newFirstVisible,
4525 BOOL bUpdateScrollPos)
4529 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4531 if (newFirstVisible != NULL)
4533 /* Prevent an empty gap from appearing at the bottom... */
4534 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4535 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4539 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4542 /* ... unless we just don't have enough items. */
4543 if (newFirstVisible == NULL)
4544 newFirstVisible = infoPtr->root->firstChild;
4548 if (infoPtr->firstVisible != newFirstVisible)
4550 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4552 infoPtr->firstVisible = newFirstVisible;
4553 TREEVIEW_Invalidate(infoPtr, NULL);
4557 TREEVIEW_ITEM *item;
4558 int scroll = infoPtr->uItemHeight *
4559 (infoPtr->firstVisible->visibleOrder
4560 - newFirstVisible->visibleOrder);
4562 infoPtr->firstVisible = newFirstVisible;
4564 for (item = infoPtr->root->firstChild; item != NULL;
4565 item = TREEVIEW_GetNextListItem(infoPtr, item))
4567 item->rect.top += scroll;
4568 item->rect.bottom += scroll;
4571 if (bUpdateScrollPos)
4572 SetScrollPos(infoPtr->hwnd, SB_VERT,
4573 newFirstVisible->visibleOrder, TRUE);
4575 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4580 /************************************************************************
4581 * VScroll is always in units of visible items. i.e. we always have a
4582 * visible item aligned to the top of the control. (Unless we have no
4586 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4588 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4589 TREEVIEW_ITEM *newFirstVisible = NULL;
4591 int nScrollCode = LOWORD(wParam);
4593 TRACE("wp %x\n", wParam);
4595 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4598 if (infoPtr->hwndEdit)
4599 SetFocus(infoPtr->hwnd);
4601 if (!oldFirstVisible)
4603 assert(infoPtr->root->firstChild == NULL);
4607 switch (nScrollCode)
4610 newFirstVisible = infoPtr->root->firstChild;
4614 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4618 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4622 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4626 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4627 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4631 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4632 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4636 case SB_THUMBPOSITION:
4637 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4638 infoPtr->root->firstChild,
4639 (LONG)(SHORT)HIWORD(wParam));
4646 if (newFirstVisible != NULL)
4648 if (newFirstVisible != oldFirstVisible)
4649 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4650 nScrollCode != SB_THUMBTRACK);
4651 else if (nScrollCode == SB_THUMBPOSITION)
4652 SetScrollPos(infoPtr->hwnd, SB_VERT,
4653 newFirstVisible->visibleOrder, TRUE);
4660 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4663 int scrollX = infoPtr->scrollX;
4664 int nScrollCode = LOWORD(wParam);
4666 TRACE("wp %x\n", wParam);
4668 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4671 if (infoPtr->hwndEdit)
4672 SetFocus(infoPtr->hwnd);
4674 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4675 /* shall never occur */
4682 switch (nScrollCode)
4685 scrollX -= infoPtr->uItemHeight;
4688 scrollX += infoPtr->uItemHeight;
4691 scrollX -= infoPtr->clientWidth;
4694 scrollX += infoPtr->clientWidth;
4698 case SB_THUMBPOSITION:
4699 scrollX = (int)(SHORT)HIWORD(wParam);
4706 if (scrollX > maxWidth)
4708 else if (scrollX < 0)
4712 if (scrollX != infoPtr->scrollX)
4714 TREEVIEW_ITEM *item;
4715 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4717 for (item = infoPtr->root->firstChild; item != NULL;
4718 item = TREEVIEW_GetNextListItem(infoPtr, item))
4720 item->linesOffset += scroll_pixels;
4721 item->stateOffset += scroll_pixels;
4722 item->imageOffset += scroll_pixels;
4723 item->textOffset += scroll_pixels;
4726 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4727 infoPtr->scrollX = scrollX;
4728 UpdateWindow(infoPtr->hwnd);
4731 if (nScrollCode != SB_THUMBTRACK)
4732 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4738 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4741 UINT pulScrollLines = 3;
4743 if (infoPtr->firstVisible == NULL)
4746 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4748 gcWheelDelta = -(short)HIWORD(wParam);
4749 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4751 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4753 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4754 int maxDy = infoPtr->maxVisibleOrder;
4762 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4767 /* Create/Destroy *******************************************************/
4770 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4772 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
4774 TREEVIEW_INFO *infoPtr;
4777 TRACE("wnd %p, style %lx\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4779 infoPtr = (TREEVIEW_INFO *)Alloc(sizeof(TREEVIEW_INFO));
4781 if (infoPtr == NULL)
4783 ERR("could not allocate info memory!\n");
4787 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4789 infoPtr->hwnd = hwnd;
4790 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4792 infoPtr->uNumItems = 0;
4793 infoPtr->cdmode = 0;
4794 infoPtr->uScrollTime = 300; /* milliseconds */
4795 infoPtr->bRedraw = TRUE;
4797 GetClientRect(hwnd, &rcClient);
4799 /* No scroll bars yet. */
4800 infoPtr->clientWidth = rcClient.right;
4801 infoPtr->clientHeight = rcClient.bottom;
4802 infoPtr->uInternalStatus = 0;
4804 infoPtr->treeWidth = 0;
4805 infoPtr->treeHeight = 0;
4807 infoPtr->uIndent = MINIMUM_INDENT;
4808 infoPtr->selectedItem = 0;
4809 infoPtr->focusedItem = 0;
4810 infoPtr->hotItem = 0;
4811 infoPtr->firstVisible = 0;
4812 infoPtr->maxVisibleOrder = 0;
4813 infoPtr->dropItem = 0;
4814 infoPtr->insertMarkItem = 0;
4815 infoPtr->insertBeforeorAfter = 0;
4818 infoPtr->scrollX = 0;
4820 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4821 infoPtr->clrText = -1; /* use system color */
4822 infoPtr->clrLine = RGB(128, 128, 128);
4823 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4827 infoPtr->hwndEdit = 0;
4828 infoPtr->wpEditOrig = NULL;
4829 infoPtr->bIgnoreEditKillFocus = FALSE;
4830 infoPtr->bLabelChanged = FALSE;
4832 infoPtr->himlNormal = NULL;
4833 infoPtr->himlState = NULL;
4834 infoPtr->normalImageWidth = 0;
4835 infoPtr->normalImageHeight = 0;
4836 infoPtr->stateImageWidth = 0;
4837 infoPtr->stateImageHeight = 0;
4839 infoPtr->items = DPA_Create(16);
4841 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
4842 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
4843 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4844 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
4845 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
4847 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4849 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4850 infoPtr->root->state = TVIS_EXPANDED;
4851 infoPtr->root->iLevel = -1;
4852 infoPtr->root->visibleOrder = -1;
4854 infoPtr->hwndNotify = lpcs->hwndParent;
4856 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4859 infoPtr->hwndToolTip = 0;
4861 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
4863 /* Determine what type of notify should be issued */
4864 /* sets infoPtr->bNtfUnicode */
4865 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
4867 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4868 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4870 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4873 HBITMAP hbm, hbmOld;
4877 infoPtr->himlState =
4878 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4880 hdcScreen = CreateDCW(szDisplayW, NULL, NULL, NULL);
4882 /* Create a coloured bitmap compatible with the screen depth
4883 because checkboxes are not black&white */
4884 hdc = CreateCompatibleDC(hdcScreen);
4885 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4886 hbmOld = SelectObject(hdc, hbm);
4888 rc.left = 0; rc.top = 0;
4889 rc.right = 48; rc.bottom = 16;
4890 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4892 rc.left = 18; rc.top = 2;
4893 rc.right = 30; rc.bottom = 14;
4894 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4895 DFCS_BUTTONCHECK|DFCS_FLAT);
4897 rc.left = 34; rc.right = 46;
4898 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4899 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4901 SelectObject(hdc, hbmOld);
4902 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4903 GetSysColor(COLOR_WINDOW));
4904 TRACE("checkbox index %d\n", nIndex);
4908 DeleteDC(hdcScreen);
4910 infoPtr->stateImageWidth = 16;
4911 infoPtr->stateImageHeight = 16;
4914 /* Make sure actual scrollbar state is consistent with uInternalStatus */
4915 ShowScrollBar(hwnd, SB_VERT, FALSE);
4916 ShowScrollBar(hwnd, SB_HORZ, FALSE);
4923 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4927 TREEVIEW_RemoveTree(infoPtr);
4929 /* tool tip is automatically destroyed: we are its owner */
4931 /* Restore original wndproc */
4932 if (infoPtr->hwndEdit)
4933 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
4934 (DWORD_PTR)infoPtr->wpEditOrig);
4936 /* Deassociate treeview from the window before doing anything drastic. */
4937 SetWindowLongPtrW(infoPtr->hwnd, 0, (DWORD_PTR)NULL);
4939 DeleteObject(infoPtr->hDefaultFont);
4940 DeleteObject(infoPtr->hBoldFont);
4941 DeleteObject(infoPtr->hUnderlineFont);
4947 /* Miscellaneous Messages ***********************************************/
4950 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
4958 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4959 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
4960 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
4961 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
4962 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
4963 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
4964 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
4965 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
4966 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
4970 if (key >= VK_PRIOR && key <= VK_DOWN)
4972 unsigned char code = scroll[key - VK_PRIOR].code;
4974 (((code & (1 << 7)) == (SB_HORZ << 7))
4976 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
4982 /************************************************************************
4985 * VK_UP Move selection to the previous non-hidden item.
4986 * VK_DOWN Move selection to the next non-hidden item.
4987 * VK_HOME Move selection to the first item.
4988 * VK_END Move selection to the last item.
4989 * VK_LEFT If expanded then collapse, otherwise move to parent.
4990 * VK_RIGHT If collapsed then expand, otherwise move to first child.
4992 * VK_SUBTRACT Collapse.
4993 * VK_MULTIPLY Expand all.
4994 * VK_PRIOR Move up GetVisibleCount items.
4995 * VK_NEXT Move down GetVisibleCount items.
4996 * VK_BACK Move to parent.
4997 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5000 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5002 /* If it is non-NULL and different, it will be selected and visible. */
5003 TREEVIEW_ITEM *newSelection = NULL;
5005 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5007 TRACE("%x\n", wParam);
5009 if (prevItem == NULL)
5012 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5013 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5018 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5020 newSelection = infoPtr->root->firstChild;
5024 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5028 newSelection = infoPtr->root->firstChild;
5032 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5036 if (prevItem->state & TVIS_EXPANDED)
5038 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5040 else if (prevItem->parent != infoPtr->root)
5042 newSelection = prevItem->parent;
5047 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5049 if (!(prevItem->state & TVIS_EXPANDED))
5050 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5053 newSelection = prevItem->firstChild;
5060 TREEVIEW_ExpandAll(infoPtr, prevItem);
5064 if (!(prevItem->state & TVIS_EXPANDED))
5065 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5069 if (prevItem->state & TVIS_EXPANDED)
5070 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5075 = TREEVIEW_GetListItem(infoPtr, prevItem,
5076 -TREEVIEW_GetVisibleCount(infoPtr));
5081 = TREEVIEW_GetListItem(infoPtr, prevItem,
5082 TREEVIEW_GetVisibleCount(infoPtr));
5086 newSelection = prevItem->parent;
5087 if (newSelection == infoPtr->root)
5088 newSelection = NULL;
5092 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5093 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5097 if (newSelection && newSelection != prevItem)
5099 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5102 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5110 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5112 if (infoPtr->hotItem)
5114 /* remove hot effect from item */
5115 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5116 infoPtr->hotItem = NULL;
5122 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, WPARAM wParam, LPARAM lParam)
5125 TRACKMOUSEEVENT trackinfo;
5126 TREEVIEW_ITEM * item;
5128 /* fill in the TRACKMOUSEEVENT struct */
5129 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5130 trackinfo.dwFlags = TME_QUERY;
5131 trackinfo.hwndTrack = infoPtr->hwnd;
5132 trackinfo.dwHoverTime = HOVER_DEFAULT;
5134 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5135 _TrackMouseEvent(&trackinfo);
5137 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5138 if(!(trackinfo.dwFlags & TME_LEAVE))
5140 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5142 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5143 /* and can properly deactivate the hot item */
5144 _TrackMouseEvent(&trackinfo);
5147 pt.x = (INT)LOWORD(lParam);
5148 pt.y = (INT)HIWORD(lParam);
5150 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5152 if (item != infoPtr->hotItem)
5154 /* redraw old hot item */
5155 if (infoPtr->hotItem)
5156 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5157 infoPtr->hotItem = item;
5158 /* redraw new hot item */
5159 if (infoPtr->hotItem)
5160 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5167 TREEVIEW_Notify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5169 LPNMHDR lpnmh = (LPNMHDR)lParam;
5171 if (lpnmh->code == PGN_CALCSIZE) {
5172 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5174 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5175 lppgc->iWidth = infoPtr->treeWidth;
5176 TRACE("got PGN_CALCSIZE, returning horz size = %ld, client=%ld\n",
5177 infoPtr->treeWidth, infoPtr->clientWidth);
5180 lppgc->iHeight = infoPtr->treeHeight;
5181 TRACE("got PGN_CALCSIZE, returning vert size = %ld, client=%ld\n",
5182 infoPtr->treeHeight, infoPtr->clientHeight);
5186 return DefWindowProcA(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5189 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5193 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5195 if (nCommand != NF_REQUERY) return 0;
5197 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5198 TRACE("format=%d\n", format);
5200 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5202 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5208 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5210 if (wParam == SIZE_RESTORED)
5212 infoPtr->clientWidth = (short)LOWORD(lParam);
5213 infoPtr->clientHeight = (short)HIWORD(lParam);
5215 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5216 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5217 TREEVIEW_UpdateScrollBars(infoPtr);
5221 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
5224 TREEVIEW_Invalidate(infoPtr, NULL);
5229 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5231 TRACE("(%x %lx)\n", wParam, lParam);
5233 if (wParam == GWL_STYLE)
5235 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5237 /* we have to take special care about tooltips */
5238 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5240 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5242 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5247 DestroyWindow(infoPtr->hwndToolTip);
5248 infoPtr->hwndToolTip = 0;
5252 infoPtr->dwStyle = dwNewStyle;
5255 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5256 TREEVIEW_UpdateScrollBars(infoPtr);
5257 TREEVIEW_Invalidate(infoPtr, NULL);
5263 TREEVIEW_SetCursor(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5266 TREEVIEW_ITEM * item;
5269 ScreenToClient(infoPtr->hwnd, &pt);
5271 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5273 /* FIXME: send NM_SETCURSOR */
5275 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5277 SetCursor(infoPtr->hcurHand);
5281 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5285 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5289 if (!infoPtr->selectedItem)
5291 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5295 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5296 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5301 TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
5305 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5306 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5311 static LRESULT WINAPI
5312 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5314 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5316 TRACE("hwnd %p msg %04x wp=%08x lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5318 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5321 if (uMsg == WM_CREATE)
5322 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5329 case TVM_CREATEDRAGIMAGE:
5330 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5332 case TVM_DELETEITEM:
5333 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5335 case TVM_EDITLABELA:
5336 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5338 case TVM_EDITLABELW:
5339 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5341 case TVM_ENDEDITLABELNOW:
5342 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5344 case TVM_ENSUREVISIBLE:
5345 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5348 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5350 case TVM_GETBKCOLOR:
5351 return TREEVIEW_GetBkColor(infoPtr);
5354 return TREEVIEW_GetCount(infoPtr);
5356 case TVM_GETEDITCONTROL:
5357 return TREEVIEW_GetEditControl(infoPtr);
5359 case TVM_GETIMAGELIST:
5360 return TREEVIEW_GetImageList(infoPtr, wParam);
5363 return TREEVIEW_GetIndent(infoPtr);
5365 case TVM_GETINSERTMARKCOLOR:
5366 return TREEVIEW_GetInsertMarkColor(infoPtr);
5368 case TVM_GETISEARCHSTRINGA:
5369 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5372 case TVM_GETISEARCHSTRINGW:
5373 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5377 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5380 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5382 case TVM_GETITEMHEIGHT:
5383 return TREEVIEW_GetItemHeight(infoPtr);
5385 case TVM_GETITEMRECT:
5386 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5388 case TVM_GETITEMSTATE:
5389 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5391 case TVM_GETLINECOLOR:
5392 return TREEVIEW_GetLineColor(infoPtr);
5394 case TVM_GETNEXTITEM:
5395 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5397 case TVM_GETSCROLLTIME:
5398 return TREEVIEW_GetScrollTime(infoPtr);
5400 case TVM_GETTEXTCOLOR:
5401 return TREEVIEW_GetTextColor(infoPtr);
5403 case TVM_GETTOOLTIPS:
5404 return TREEVIEW_GetToolTips(infoPtr);
5406 case TVM_GETUNICODEFORMAT:
5407 return TREEVIEW_GetUnicodeFormat(infoPtr);
5409 case TVM_GETVISIBLECOUNT:
5410 return TREEVIEW_GetVisibleCount(infoPtr);
5413 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5415 case TVM_INSERTITEMA:
5416 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, FALSE);
5418 case TVM_INSERTITEMW:
5419 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, TRUE);
5421 case TVM_SELECTITEM:
5422 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5424 case TVM_SETBKCOLOR:
5425 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5427 case TVM_SETIMAGELIST:
5428 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5431 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5433 case TVM_SETINSERTMARK:
5434 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5436 case TVM_SETINSERTMARKCOLOR:
5437 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5440 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5443 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5445 case TVM_SETLINECOLOR:
5446 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5448 case TVM_SETITEMHEIGHT:
5449 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5451 case TVM_SETSCROLLTIME:
5452 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5454 case TVM_SETTEXTCOLOR:
5455 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5457 case TVM_SETTOOLTIPS:
5458 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5460 case TVM_SETUNICODEFORMAT:
5461 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5463 case TVM_SORTCHILDREN:
5464 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5466 case TVM_SORTCHILDRENCB:
5467 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5470 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5473 return TREEVIEW_Command(infoPtr, wParam, lParam);
5476 return TREEVIEW_Destroy(infoPtr);
5481 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5484 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5487 return TREEVIEW_GetFont(infoPtr);
5490 return TREEVIEW_HScroll(infoPtr, wParam);
5493 return TREEVIEW_KeyDown(infoPtr, wParam);
5496 return TREEVIEW_KillFocus(infoPtr);
5498 case WM_LBUTTONDBLCLK:
5499 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5501 case WM_LBUTTONDOWN:
5502 return TREEVIEW_LButtonDown(infoPtr, lParam);
5504 /* WM_MBUTTONDOWN */
5507 return TREEVIEW_MouseLeave(infoPtr);
5510 if (infoPtr->dwStyle & TVS_TRACKSELECT)
5511 return TREEVIEW_MouseMove(infoPtr, wParam, lParam);
5516 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5518 case WM_NOTIFYFORMAT:
5519 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5522 return TREEVIEW_Paint(infoPtr, wParam);
5524 /* WM_PRINTCLIENT */
5526 case WM_RBUTTONDOWN:
5527 return TREEVIEW_RButtonDown(infoPtr, lParam);
5530 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5533 return TREEVIEW_SetFocus(infoPtr);
5536 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5539 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5542 return TREEVIEW_Size(infoPtr, wParam, lParam);
5544 case WM_STYLECHANGED:
5545 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5547 /* WM_SYSCOLORCHANGE */
5552 return TREEVIEW_HandleTimer(infoPtr, wParam);
5555 return TREEVIEW_VScroll(infoPtr, wParam);
5557 /* WM_WININICHANGE */
5560 if (wParam & (MK_SHIFT | MK_CONTROL))
5562 return TREEVIEW_MouseWheel(infoPtr, wParam);
5565 TRACE("drawItem\n");
5569 /* This mostly catches MFC and Delphi messages. :( */
5570 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5571 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
5573 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
5578 /* Class Registration ***************************************************/
5581 TREEVIEW_Register(void)
5587 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
5588 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5589 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5590 wndClass.cbClsExtra = 0;
5591 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5593 wndClass.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
5594 wndClass.hbrBackground = 0;
5595 wndClass.lpszClassName = WC_TREEVIEWA;
5597 RegisterClassA(&wndClass);
5602 TREEVIEW_Unregister(void)
5604 UnregisterClassA(WC_TREEVIEWA, NULL);
5608 /* Tree Verification ****************************************************/
5612 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5614 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5615 TREEVIEW_ITEM *item)
5617 assert(infoPtr != NULL);
5618 assert(item != NULL);
5620 /* both NULL, or both non-null */
5621 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5623 assert(item->firstChild != item);
5624 assert(item->lastChild != item);
5626 if (item->firstChild)
5628 assert(item->firstChild->parent == item);
5629 assert(item->firstChild->prevSibling == NULL);
5632 if (item->lastChild)
5634 assert(item->lastChild->parent == item);
5635 assert(item->lastChild->nextSibling == NULL);
5638 assert(item->nextSibling != item);
5639 if (item->nextSibling)
5641 assert(item->nextSibling->parent == item->parent);
5642 assert(item->nextSibling->prevSibling == item);
5645 assert(item->prevSibling != item);
5646 if (item->prevSibling)
5648 assert(item->prevSibling->parent == item->parent);
5649 assert(item->prevSibling->nextSibling == item);
5654 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5656 assert(item != NULL);
5658 assert(item->parent != NULL);
5659 assert(item->parent != item);
5660 assert(item->iLevel == item->parent->iLevel + 1);
5662 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5664 TREEVIEW_VerifyItemCommon(infoPtr, item);
5666 TREEVIEW_VerifyChildren(infoPtr, item);
5670 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5672 TREEVIEW_ITEM *child;
5673 assert(item != NULL);
5675 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5676 TREEVIEW_VerifyItem(infoPtr, child);
5680 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5682 TREEVIEW_ITEM *root = infoPtr->root;
5684 assert(root != NULL);
5685 assert(root->iLevel == -1);
5686 assert(root->parent == NULL);
5687 assert(root->prevSibling == NULL);
5689 TREEVIEW_VerifyItemCommon(infoPtr, root);
5691 TREEVIEW_VerifyChildren(infoPtr, root);
5695 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5697 assert(infoPtr != NULL);
5699 TREEVIEW_VerifyRoot(infoPtr);