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
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
26 * Note2: All items always! have valid (allocated) pszText field.
27 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
28 * of size TEXT_CALLBACK_SIZE in DoSetItem.
29 * We use callbackMask to keep track of fields to be updated.
32 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
33 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
35 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
37 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
44 #include "wine/port.h"
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
65 /* internal structures */
67 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
78 int iIntegral; /* item height multiplier (1 is normal) */
79 int iLevel; /* indentation level:0=root level */
80 HTREEITEM parent; /* handle to parent or 0 if at root */
81 HTREEITEM firstChild; /* handle to first child or 0 if no child */
83 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
84 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
90 LONG textWidth; /* horizontal text extent for pszText */
91 LONG visibleOrder; /* visible ordering, 0 is first visible item */
95 typedef struct tagTREEVIEW_INFO
98 HWND hwndNotify; /* Owner window to send notifications to */
101 UINT uInternalStatus;
103 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
104 INT cdmode; /* last custom draw setting */
105 UINT uScrollTime; /* max. time for scrolling in milliseconds */
106 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
108 UINT uItemHeight; /* item height */
111 LONG clientWidth; /* width of control window */
112 LONG clientHeight; /* height of control window */
114 LONG treeWidth; /* width of visible tree items */
115 LONG treeHeight; /* height of visible tree items */
117 UINT uIndent; /* indentation in pixels */
118 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
119 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
120 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
122 HTREEITEM firstVisible; /* handle to first visible item */
123 LONG maxVisibleOrder;
124 HTREEITEM dropItem; /* handle to item selected by drag cursor */
125 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
126 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
127 HIMAGELIST dragList; /* Bitmap of dragged item */
132 COLORREF clrInsertMark;
136 HFONT hUnderlineFont;
141 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
142 BOOL bIgnoreEditKillFocus;
145 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
146 HIMAGELIST himlNormal;
147 int normalImageHeight;
148 int normalImageWidth;
149 HIMAGELIST himlState;
150 int stateImageHeight;
154 DWORD lastKeyPressTimestamp; /* Added */
155 WPARAM charCode; /* Added */
156 INT nSearchParamLength; /* Added */
157 WCHAR szSearchParam[ MAX_PATH ]; /* Added */
161 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
162 #define KEY_DELAY 450
164 /* bitflags for infoPtr->uInternalStatus */
166 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
167 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
168 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
169 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
170 #define TV_RDRAG 0x10 /* dito Rbutton */
171 #define TV_RDRAGGING 0x20
173 /* bitflags for infoPtr->timer */
175 #define TV_EDIT_TIMER 2
176 #define TV_EDIT_TIMER_SET 2
179 VOID TREEVIEW_Register (VOID);
180 VOID TREEVIEW_Unregister (VOID);
183 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
186 #define TEXT_CALLBACK_SIZE 260
188 #define TREEVIEW_LEFT_MARGIN 8
190 #define MINIMUM_INDENT 19
192 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
194 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
195 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
196 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
199 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
202 static VOID TREEVIEW_Invalidate(TREEVIEW_INFO *, TREEVIEW_ITEM *);
204 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
205 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
206 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
207 static LRESULT TREEVIEW_RButtonUp(TREEVIEW_INFO *, LPPOINT);
208 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
209 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
210 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
211 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND wParam, UINT lParam);
214 /* Random Utilities *****************************************************/
218 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
223 /* The definition is at the end of the file. */
224 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
227 /* Returns the treeview private data if hwnd is a treeview.
228 * Otherwise returns an undefined value. */
229 static TREEVIEW_INFO *
230 TREEVIEW_GetInfoPtr(HWND hwnd)
232 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
235 /* Don't call this. Nothing wants an item index. */
237 TREEVIEW_GetItemIndex(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
239 assert(infoPtr != NULL);
241 return DPA_GetPtrIndex(infoPtr->items, handle);
244 /***************************************************************************
245 * This method checks that handle is an item for this tree.
248 TREEVIEW_ValidItem(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
250 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
252 TRACE("invalid item %p\n", handle);
260 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
264 GetObjectW(hOrigFont, sizeof(font), &font);
265 font.lfWeight = FW_BOLD;
266 return CreateFontIndirectW(&font);
270 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
274 GetObjectW(hOrigFont, sizeof(font), &font);
275 font.lfUnderline = TRUE;
276 return CreateFontIndirectW(&font);
280 TREEVIEW_FontForItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
282 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
283 return infoPtr->hUnderlineFont;
284 if (item->state & TVIS_BOLD)
285 return infoPtr->hBoldFont;
286 return infoPtr->hFont;
289 /* for trace/debugging purposes only */
291 TREEVIEW_ItemName(TREEVIEW_ITEM *item)
293 if (item == NULL) return "<null item>";
294 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
295 if (item->pszText == NULL) return "<null>";
296 return debugstr_w(item->pszText);
299 /* An item is not a child of itself. */
301 TREEVIEW_IsChildOf(TREEVIEW_ITEM *parent, TREEVIEW_ITEM *child)
305 child = child->parent;
306 if (child == parent) return TRUE;
307 } while (child != NULL);
313 /* Tree Traversal *******************************************************/
315 /***************************************************************************
316 * This method returns the last expanded sibling or child child item
319 static TREEVIEW_ITEM *
320 TREEVIEW_GetLastListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
325 while (wineItem->lastChild)
327 if (wineItem->state & TVIS_EXPANDED)
328 wineItem = wineItem->lastChild;
333 if (wineItem == infoPtr->root)
339 /***************************************************************************
340 * This method returns the previous non-hidden item in the list not
341 * considering the tree hierarchy.
343 static TREEVIEW_ITEM *
344 TREEVIEW_GetPrevListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
346 if (tvItem->prevSibling)
348 /* This item has a prevSibling, get the last item in the sibling's tree. */
349 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
351 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
352 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
358 /* this item does not have a prevSibling, get the parent */
359 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
364 /***************************************************************************
365 * This method returns the next physical item in the treeview not
366 * considering the tree hierarchy.
368 static TREEVIEW_ITEM *
369 TREEVIEW_GetNextListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
371 assert(tvItem != NULL);
374 * If this item has children and is expanded, return the first child
376 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
378 return tvItem->firstChild;
383 * try to get the sibling
385 if (tvItem->nextSibling)
386 return tvItem->nextSibling;
389 * Otherwise, get the parent's sibling.
391 while (tvItem->parent)
393 tvItem = tvItem->parent;
395 if (tvItem->nextSibling)
396 return tvItem->nextSibling;
402 /***************************************************************************
403 * This method returns the nth item starting at the given item. It returns
404 * the last item (or first) we we run out of items.
406 * Will scroll backward if count is <0.
407 * forward if count is >0.
409 static TREEVIEW_ITEM *
410 TREEVIEW_GetListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
413 TREEVIEW_ITEM *(*next_item)(TREEVIEW_INFO *, TREEVIEW_ITEM *);
414 TREEVIEW_ITEM *previousItem;
416 assert(wineItem != NULL);
420 next_item = TREEVIEW_GetNextListItem;
425 next_item = TREEVIEW_GetPrevListItem;
432 previousItem = wineItem;
433 wineItem = next_item(infoPtr, wineItem);
435 } while (--count && wineItem != NULL);
438 return wineItem ? wineItem : previousItem;
441 /* Notifications ************************************************************/
443 static INT get_notifycode(TREEVIEW_INFO *infoPtr, INT code)
445 if (!infoPtr->bNtfUnicode) {
447 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
448 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
449 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
450 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
451 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
452 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
453 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
454 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
455 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
456 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
457 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
458 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
465 TREEVIEW_SendRealNotify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
467 TRACE("wParam=%d, lParam=%ld\n", wParam, lParam);
468 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
472 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO *infoPtr, UINT code)
475 HWND hwnd = infoPtr->hwnd;
478 nmhdr.hwndFrom = hwnd;
479 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
480 nmhdr.code = get_notifycode(infoPtr, code);
482 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
483 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
487 TREEVIEW_TVItemFromItem(TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
490 tvItem->hItem = item;
491 tvItem->state = item->state;
492 tvItem->stateMask = 0;
493 tvItem->iImage = item->iImage;
494 tvItem->iSelectedImage = item->iSelectedImage;
495 tvItem->cChildren = item->cChildren;
496 tvItem->lParam = item->lParam;
500 if (!infoPtr->bNtfUnicode)
502 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
503 tvItem->pszText = Alloc (tvItem->cchTextMax);
504 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
508 tvItem->cchTextMax = item->cchTextMax;
509 tvItem->pszText = item->pszText;
514 tvItem->cchTextMax = 0;
515 tvItem->pszText = NULL;
520 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO *infoPtr, UINT code, UINT action,
521 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
523 HWND hwnd = infoPtr->hwnd;
527 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
528 code, action, oldItem, newItem);
530 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWA));
532 nmhdr.hdr.hwndFrom = hwnd;
533 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
534 nmhdr.hdr.code = get_notifycode(infoPtr, code);
535 nmhdr.action = action;
538 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
541 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
546 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
547 (WPARAM)nmhdr.hdr.idFrom,
549 if (!infoPtr->bNtfUnicode)
551 Free(nmhdr.itemOld.pszText);
552 Free(nmhdr.itemNew.pszText);
558 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO *infoPtr, UINT code,
559 HTREEITEM dragItem, POINT pt)
561 HWND hwnd = infoPtr->hwnd;
564 TRACE("code:%d dragitem:%p\n", code, dragItem);
566 nmhdr.hdr.hwndFrom = hwnd;
567 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
568 nmhdr.hdr.code = get_notifycode(infoPtr, code);
570 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
571 nmhdr.itemNew.hItem = dragItem;
572 nmhdr.itemNew.state = dragItem->state;
573 nmhdr.itemNew.lParam = dragItem->lParam;
575 nmhdr.ptDrag.x = pt.x;
576 nmhdr.ptDrag.y = pt.y;
578 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
579 (WPARAM)nmhdr.hdr.idFrom,
585 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
588 HWND hwnd = infoPtr->hwnd;
589 NMTVCUSTOMDRAW nmcdhdr;
592 TRACE("drawstage:%lx hdc:%p\n", dwDrawStage, hdc);
594 nmcd = &nmcdhdr.nmcd;
595 nmcd->hdr.hwndFrom = hwnd;
596 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
597 nmcd->hdr.code = NM_CUSTOMDRAW;
598 nmcd->dwDrawStage = dwDrawStage;
601 nmcd->dwItemSpec = 0;
602 nmcd->uItemState = 0;
603 nmcd->lItemlParam = 0;
604 nmcdhdr.clrText = infoPtr->clrText;
605 nmcdhdr.clrTextBk = infoPtr->clrBk;
608 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
609 (WPARAM)nmcd->hdr.idFrom,
615 /* FIXME: need to find out when the flags in uItemState need to be set */
618 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO *infoPtr, HDC hdc,
619 TREEVIEW_ITEM *wineItem, UINT uItemDrawState)
621 HWND hwnd = infoPtr->hwnd;
622 NMTVCUSTOMDRAW nmcdhdr;
624 DWORD dwDrawStage, dwItemSpec;
628 dwDrawStage = CDDS_ITEM | uItemDrawState;
629 dwItemSpec = (DWORD)wineItem;
631 if (wineItem->state & TVIS_SELECTED)
632 uItemState |= CDIS_SELECTED;
633 if (wineItem == infoPtr->selectedItem)
634 uItemState |= CDIS_FOCUS;
635 if (wineItem == infoPtr->hotItem)
636 uItemState |= CDIS_HOT;
638 nmcd = &nmcdhdr.nmcd;
639 nmcd->hdr.hwndFrom = hwnd;
640 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
641 nmcd->hdr.code = NM_CUSTOMDRAW;
642 nmcd->dwDrawStage = dwDrawStage;
644 nmcd->rc = wineItem->rect;
645 nmcd->dwItemSpec = dwItemSpec;
646 nmcd->uItemState = uItemState;
647 nmcd->lItemlParam = wineItem->lParam;
648 nmcdhdr.clrText = infoPtr->clrText;
649 nmcdhdr.clrTextBk = infoPtr->clrBk;
650 nmcdhdr.iLevel = wineItem->iLevel;
652 TRACE("drawstage:%lx hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
653 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
654 nmcd->uItemState, nmcd->lItemlParam);
656 retval = TREEVIEW_SendRealNotify(infoPtr,
657 (WPARAM)nmcd->hdr.idFrom,
660 infoPtr->clrText = nmcdhdr.clrText;
661 infoPtr->clrBk = nmcdhdr.clrTextBk;
666 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
668 HWND hwnd = infoPtr->hwnd;
672 tvdi.hdr.hwndFrom = hwnd;
673 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
674 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
676 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
677 &tvdi.item, editItem);
679 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
681 if (!infoPtr->bNtfUnicode)
682 Free(tvdi.item.pszText);
688 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
691 NMTVDISPINFOW callback;
692 HWND hwnd = infoPtr->hwnd;
694 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
695 mask &= wineItem->callbackMask;
697 if (mask == 0) return;
699 callback.hdr.hwndFrom = hwnd;
700 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
701 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
703 /* 'state' always contains valid value, as well as 'lParam'.
704 * All other parameters are uninitialized.
706 callback.item.pszText = wineItem->pszText;
707 callback.item.cchTextMax = wineItem->cchTextMax;
708 callback.item.mask = mask;
709 callback.item.hItem = wineItem;
710 callback.item.state = wineItem->state;
711 callback.item.lParam = wineItem->lParam;
713 /* If text is changed we need to recalculate textWidth */
714 if (mask & TVIF_TEXT)
715 wineItem->textWidth = 0;
717 TREEVIEW_SendRealNotify(infoPtr,
718 (WPARAM)callback.hdr.idFrom, (LPARAM)&callback);
720 /* It may have changed due to a call to SetItem. */
721 mask &= wineItem->callbackMask;
723 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
725 /* Instead of copying text into our buffer user specified its own */
726 if (!infoPtr->bNtfUnicode) {
729 int len = MultiByteToWideChar( CP_ACP, 0,
730 (LPSTR)callback.item.pszText, -1,
732 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
733 newText = (LPWSTR)ReAlloc(wineItem->pszText, buflen);
735 TRACE("returned str %s, len=%d, buflen=%d\n",
736 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
740 wineItem->pszText = newText;
741 MultiByteToWideChar( CP_ACP, 0,
742 (LPSTR)callback.item.pszText, -1,
743 wineItem->pszText, buflen);
744 wineItem->cchTextMax = buflen;
746 /* If ReAlloc fails we have nothing to do, but keep original text */
749 int len = max(lstrlenW(callback.item.pszText) + 1,
751 LPWSTR newText = ReAlloc(wineItem->pszText, len);
753 TRACE("returned wstr %s, len=%d\n",
754 debugstr_w(callback.item.pszText), len);
758 wineItem->pszText = newText;
759 strcpyW(wineItem->pszText, callback.item.pszText);
760 wineItem->cchTextMax = len;
762 /* If ReAlloc fails we have nothing to do, but keep original text */
765 else if (mask & TVIF_TEXT) {
766 /* User put text into our buffer, that is ok unless A string */
767 if (!infoPtr->bNtfUnicode) {
769 LPWSTR oldText = NULL;
771 int len = MultiByteToWideChar( CP_ACP, 0,
772 (LPSTR)callback.item.pszText, -1,
774 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
775 newText = (LPWSTR)Alloc(buflen);
777 TRACE("same buffer str %s, len=%d, buflen=%d\n",
778 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
782 oldText = wineItem->pszText;
783 wineItem->pszText = newText;
784 MultiByteToWideChar( CP_ACP, 0,
785 (LPSTR)callback.item.pszText, -1,
786 wineItem->pszText, buflen);
787 wineItem->cchTextMax = buflen;
794 if (mask & TVIF_IMAGE)
795 wineItem->iImage = callback.item.iImage;
797 if (mask & TVIF_SELECTEDIMAGE)
798 wineItem->iSelectedImage = callback.item.iSelectedImage;
800 if (mask & TVIF_CHILDREN)
801 wineItem->cChildren = callback.item.cChildren;
803 /* These members are now permanently set. */
804 if (callback.item.mask & TVIF_DI_SETITEM)
805 wineItem->callbackMask &= ~callback.item.mask;
808 /***************************************************************************
809 * This function uses cChildren field to decide whether the item has
811 * Note: if this returns TRUE, the child items may not actually exist,
812 * they could be virtual.
814 * Just use wineItem->firstChild to check for physical children.
817 TREEVIEW_HasChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
819 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
821 return wineItem->cChildren > 0;
825 /* Item Position ********************************************************/
827 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
829 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO *infoPtr,
832 /* Same effect, different optimisation. */
834 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
835 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
837 BOOL lar = ((infoPtr->dwStyle
838 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
842 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
844 item->stateOffset = item->linesOffset + infoPtr->uIndent;
845 item->imageOffset = item->stateOffset
846 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
847 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
851 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
857 /* DRAW's OM docker creates items like this */
858 if (item->pszText == NULL)
870 hdc = GetDC(infoPtr->hwnd);
871 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
874 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
875 item->textWidth = sz.cx;
879 SelectObject(hdc, hOldFont);
885 TREEVIEW_ComputeItemRect(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
887 item->rect.top = infoPtr->uItemHeight *
888 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
890 item->rect.bottom = item->rect.top
891 + infoPtr->uItemHeight * item->iIntegral - 1;
894 item->rect.right = infoPtr->clientWidth;
897 /* We know that only items after start need their order updated. */
899 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
906 start = infoPtr->root->firstChild;
910 order = start->visibleOrder;
912 for (item = start; item != NULL;
913 item = TREEVIEW_GetNextListItem(infoPtr, item))
915 item->visibleOrder = order;
916 order += item->iIntegral;
919 infoPtr->maxVisibleOrder = order;
921 for (item = start; item != NULL;
922 item = TREEVIEW_GetNextListItem(infoPtr, item))
924 TREEVIEW_ComputeItemRect(infoPtr, item);
929 /* Update metrics of all items in selected subtree.
930 * root must be expanded
933 TREEVIEW_UpdateSubTree(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
935 TREEVIEW_ITEM *sibling;
939 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
942 root->state &= ~TVIS_EXPANDED;
943 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
944 root->state |= TVIS_EXPANDED;
946 hdc = GetDC(infoPtr->hwnd);
947 hOldFont = SelectObject(hdc, infoPtr->hFont);
949 for (; root != sibling;
950 root = TREEVIEW_GetNextListItem(infoPtr, root))
952 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
954 if (root->callbackMask & TVIF_TEXT)
955 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
957 if (root->textWidth == 0)
959 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
960 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
964 SelectObject(hdc, hOldFont);
965 ReleaseDC(infoPtr->hwnd, hdc);
968 /* Item Allocation **********************************************************/
970 static TREEVIEW_ITEM *
971 TREEVIEW_AllocateItem(TREEVIEW_INFO *infoPtr)
973 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
978 newItem->iImage = -1;
979 newItem->iSelectedImage = -1;
981 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
990 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
991 * free item->pszText. */
993 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
995 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
997 if (infoPtr->selectedItem == item)
998 infoPtr->selectedItem = NULL;
999 if (infoPtr->hotItem == item)
1000 infoPtr->hotItem = NULL;
1001 if (infoPtr->focusedItem == item)
1002 infoPtr->focusedItem = NULL;
1003 if (infoPtr->firstVisible == item)
1004 infoPtr->firstVisible = NULL;
1005 if (infoPtr->dropItem == item)
1006 infoPtr->dropItem = NULL;
1007 if (infoPtr->insertMarkItem == item)
1008 infoPtr->insertMarkItem = NULL;
1012 /* Item Insertion *******************************************************/
1014 /***************************************************************************
1015 * This method inserts newItem before sibling as a child of parent.
1016 * sibling can be NULL, but only if parent has no children.
1019 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1020 TREEVIEW_ITEM *parent)
1022 assert(newItem != NULL);
1023 assert(parent != NULL);
1025 if (sibling != NULL)
1027 assert(sibling->parent == parent);
1029 if (sibling->prevSibling != NULL)
1030 sibling->prevSibling->nextSibling = newItem;
1032 newItem->prevSibling = sibling->prevSibling;
1033 sibling->prevSibling = newItem;
1036 newItem->prevSibling = NULL;
1038 newItem->nextSibling = sibling;
1040 if (parent->firstChild == sibling)
1041 parent->firstChild = newItem;
1043 if (parent->lastChild == NULL)
1044 parent->lastChild = newItem;
1047 /***************************************************************************
1048 * This method inserts newItem after sibling as a child of parent.
1049 * sibling can be NULL, but only if parent has no children.
1052 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1053 TREEVIEW_ITEM *parent)
1055 assert(newItem != NULL);
1056 assert(parent != NULL);
1058 if (sibling != NULL)
1060 assert(sibling->parent == parent);
1062 if (sibling->nextSibling != NULL)
1063 sibling->nextSibling->prevSibling = newItem;
1065 newItem->nextSibling = sibling->nextSibling;
1066 sibling->nextSibling = newItem;
1069 newItem->nextSibling = NULL;
1071 newItem->prevSibling = sibling;
1073 if (parent->lastChild == sibling)
1074 parent->lastChild = newItem;
1076 if (parent->firstChild == NULL)
1077 parent->firstChild = newItem;
1081 TREEVIEW_DoSetItemT(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1082 const TVITEMEXW *tvItem, BOOL isW)
1084 UINT callbackClear = 0;
1085 UINT callbackSet = 0;
1087 TRACE("item %p\n", wineItem);
1088 /* Do this first in case it fails. */
1089 if (tvItem->mask & TVIF_TEXT)
1091 wineItem->textWidth = 0; /* force width recalculation */
1092 if (tvItem->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
1097 len = lstrlenW(tvItem->pszText) + 1;
1099 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1101 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1103 if (newText == NULL) return FALSE;
1105 callbackClear |= TVIF_TEXT;
1107 wineItem->pszText = newText;
1108 wineItem->cchTextMax = len;
1110 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1112 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1113 wineItem->pszText, len);
1115 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1119 callbackSet |= TVIF_TEXT;
1121 wineItem->pszText = ReAlloc(wineItem->pszText,
1122 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1123 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1124 TRACE("setting callback, item %p\n", wineItem);
1128 if (tvItem->mask & TVIF_CHILDREN)
1130 wineItem->cChildren = tvItem->cChildren;
1132 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1133 callbackSet |= TVIF_CHILDREN;
1135 callbackClear |= TVIF_CHILDREN;
1138 if (tvItem->mask & TVIF_IMAGE)
1140 wineItem->iImage = tvItem->iImage;
1142 if (wineItem->iImage == I_IMAGECALLBACK)
1143 callbackSet |= TVIF_IMAGE;
1145 callbackClear |= TVIF_IMAGE;
1148 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1150 wineItem->iSelectedImage = tvItem->iSelectedImage;
1152 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1153 callbackSet |= TVIF_SELECTEDIMAGE;
1155 callbackClear |= TVIF_SELECTEDIMAGE;
1158 if (tvItem->mask & TVIF_PARAM)
1159 wineItem->lParam = tvItem->lParam;
1161 /* If the application sets TVIF_INTEGRAL without
1162 * supplying a TVITEMEX structure, it's toast. */
1163 if (tvItem->mask & TVIF_INTEGRAL)
1164 wineItem->iIntegral = tvItem->iIntegral;
1166 if (tvItem->mask & TVIF_STATE)
1168 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1170 wineItem->state &= ~tvItem->stateMask;
1171 wineItem->state |= (tvItem->state & tvItem->stateMask);
1174 wineItem->callbackMask |= callbackSet;
1175 wineItem->callbackMask &= ~callbackClear;
1180 /* Note that the new item is pre-zeroed. */
1182 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1184 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1185 HTREEITEM insertAfter;
1186 TREEVIEW_ITEM *newItem, *parentItem;
1187 BOOL bTextUpdated = FALSE;
1189 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1191 parentItem = infoPtr->root;
1195 parentItem = ptdi->hParent;
1197 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1199 WARN("invalid parent %p\n", parentItem);
1200 return (LRESULT)(HTREEITEM)NULL;
1204 insertAfter = ptdi->hInsertAfter;
1206 /* Validate this now for convenience. */
1207 switch ((DWORD)insertAfter)
1209 case (DWORD)TVI_FIRST:
1210 case (DWORD)TVI_LAST:
1211 case (DWORD)TVI_SORT:
1215 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1216 insertAfter->parent != parentItem)
1218 WARN("invalid insert after %p\n", insertAfter);
1219 insertAfter = TVI_LAST;
1223 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1224 (tvItem->mask & TVIF_TEXT)
1225 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1226 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1229 newItem = TREEVIEW_AllocateItem(infoPtr);
1230 if (newItem == NULL)
1231 return (LRESULT)(HTREEITEM)NULL;
1233 newItem->parent = parentItem;
1234 newItem->iIntegral = 1;
1236 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1237 return (LRESULT)(HTREEITEM)NULL;
1239 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1241 infoPtr->uNumItems++;
1243 switch ((DWORD)insertAfter)
1245 case (DWORD)TVI_FIRST:
1247 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1248 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1249 if (infoPtr->firstVisible == originalFirst)
1250 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1254 case (DWORD)TVI_LAST:
1255 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1258 /* hInsertAfter names a specific item we want to insert after */
1260 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1263 case (DWORD)TVI_SORT:
1265 TREEVIEW_ITEM *aChild;
1266 TREEVIEW_ITEM *previousChild = NULL;
1267 BOOL bItemInserted = FALSE;
1269 aChild = parentItem->firstChild;
1271 bTextUpdated = TRUE;
1272 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1274 /* Iterate the parent children to see where we fit in */
1275 while (aChild != NULL)
1279 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1280 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1282 if (comp < 0) /* we are smaller than the current one */
1284 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1285 bItemInserted = TRUE;
1288 else if (comp > 0) /* we are bigger than the current one */
1290 previousChild = aChild;
1292 /* This will help us to exit if there is no more sibling */
1293 aChild = (aChild->nextSibling == 0)
1295 : aChild->nextSibling;
1297 /* Look at the next item */
1303 * An item with this name is already existing, therefore,
1304 * we add after the one we found
1306 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1307 bItemInserted = TRUE;
1313 * we reach the end of the child list and the item has not
1314 * yet been inserted, therefore, insert it after the last child.
1316 if ((!bItemInserted) && (aChild == NULL))
1317 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1324 TRACE("new item %p; parent %p, mask %x\n", newItem,
1325 newItem->parent, tvItem->mask);
1327 newItem->iLevel = newItem->parent->iLevel + 1;
1329 if (newItem->parent->cChildren == 0)
1330 newItem->parent->cChildren = 1;
1332 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1334 if (STATEIMAGEINDEX(newItem->state) == 0)
1335 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1338 if (infoPtr->firstVisible == NULL)
1339 infoPtr->firstVisible = newItem;
1341 TREEVIEW_VerifyTree(infoPtr);
1343 if (parentItem == infoPtr->root ||
1344 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1346 TREEVIEW_ITEM *item;
1347 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1349 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1350 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1353 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1355 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1356 TREEVIEW_UpdateScrollBars(infoPtr);
1358 * if the item was inserted in a visible part of the tree,
1359 * invalidate it, as well as those after it
1361 for (item = newItem;
1363 item = TREEVIEW_GetNextListItem(infoPtr, item))
1364 TREEVIEW_Invalidate(infoPtr, item);
1368 newItem->visibleOrder = -1;
1370 /* refresh treeview if newItem is the first item inserted under parentItem */
1371 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1373 /* parent got '+' - update it */
1374 TREEVIEW_Invalidate(infoPtr, parentItem);
1378 return (LRESULT)newItem;
1381 /* Item Deletion ************************************************************/
1383 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1386 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1388 TREEVIEW_ITEM *kill = parentItem->firstChild;
1390 while (kill != NULL)
1392 TREEVIEW_ITEM *next = kill->nextSibling;
1394 TREEVIEW_RemoveItem(infoPtr, kill);
1399 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1400 assert(parentItem->firstChild == NULL);
1401 assert(parentItem->lastChild == NULL);
1405 TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1407 TREEVIEW_ITEM *parentItem = item->parent;
1409 assert(item != NULL);
1410 assert(item->parent != NULL); /* i.e. it must not be the root */
1412 if (parentItem->firstChild == item)
1413 parentItem->firstChild = item->nextSibling;
1415 if (parentItem->lastChild == item)
1416 parentItem->lastChild = item->prevSibling;
1418 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1419 && parentItem->cChildren > 0)
1420 parentItem->cChildren = 0;
1422 if (item->prevSibling)
1423 item->prevSibling->nextSibling = item->nextSibling;
1425 if (item->nextSibling)
1426 item->nextSibling->prevSibling = item->prevSibling;
1430 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1432 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1434 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1435 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1437 if (wineItem->firstChild)
1438 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1440 TREEVIEW_UnlinkItem(wineItem);
1442 infoPtr->uNumItems--;
1444 if (wineItem->pszText && wineItem->pszText != LPSTR_TEXTCALLBACKW)
1445 Free(wineItem->pszText);
1447 TREEVIEW_FreeItem(infoPtr, wineItem);
1451 /* Empty out the tree. */
1453 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1455 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1457 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1461 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1463 TREEVIEW_ITEM *newSelection = NULL;
1464 TREEVIEW_ITEM *newFirstVisible = NULL;
1465 TREEVIEW_ITEM *parent, *prev = NULL;
1466 BOOL visible = FALSE;
1468 if (wineItem == TVI_ROOT)
1470 TRACE("TVI_ROOT\n");
1471 parent = infoPtr->root;
1472 newSelection = NULL;
1474 TREEVIEW_RemoveTree(infoPtr);
1478 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1481 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1482 parent = wineItem->parent;
1484 if (ISVISIBLE(wineItem))
1486 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1490 if (infoPtr->selectedItem != NULL
1491 && (wineItem == infoPtr->selectedItem
1492 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1494 if (wineItem->nextSibling)
1495 newSelection = wineItem->nextSibling;
1496 else if (wineItem->parent != infoPtr->root)
1497 newSelection = wineItem->parent;
1499 newSelection = wineItem->prevSibling;
1500 TRACE("newSelection = %p\n", newSelection);
1503 if (infoPtr->firstVisible == wineItem)
1505 if (wineItem->nextSibling)
1506 newFirstVisible = wineItem->nextSibling;
1507 else if (wineItem->prevSibling)
1508 newFirstVisible = wineItem->prevSibling;
1509 else if (wineItem->parent != infoPtr->root)
1510 newFirstVisible = wineItem->parent;
1511 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1514 newFirstVisible = infoPtr->firstVisible;
1516 TREEVIEW_RemoveItem(infoPtr, wineItem);
1519 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1520 if (!infoPtr->selectedItem && newSelection)
1522 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1523 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1526 /* Validate insertMark dropItem.
1527 * hotItem ??? - used for comparison only.
1529 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1530 infoPtr->insertMarkItem = 0;
1532 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1533 infoPtr->dropItem = 0;
1535 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1536 newFirstVisible = infoPtr->root->firstChild;
1538 TREEVIEW_VerifyTree(infoPtr);
1543 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1544 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1545 TREEVIEW_UpdateScrollBars(infoPtr);
1546 TREEVIEW_Invalidate(infoPtr, NULL);
1548 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1550 /* parent lost '+/-' - update it */
1551 TREEVIEW_Invalidate(infoPtr, parent);
1558 /* Get/Set Messages *********************************************************/
1560 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1563 infoPtr->bRedraw = TRUE;
1565 infoPtr->bRedraw = FALSE;
1571 TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1574 return infoPtr->uIndent;
1578 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1582 if (newIndent < MINIMUM_INDENT)
1583 newIndent = MINIMUM_INDENT;
1585 if (infoPtr->uIndent != newIndent)
1587 infoPtr->uIndent = newIndent;
1588 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1589 TREEVIEW_UpdateScrollBars(infoPtr);
1590 TREEVIEW_Invalidate(infoPtr, NULL);
1598 TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1601 return (LRESULT)infoPtr->hwndToolTip;
1605 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1610 prevToolTip = infoPtr->hwndToolTip;
1611 infoPtr->hwndToolTip = hwndTT;
1613 return (LRESULT)prevToolTip;
1617 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1619 BOOL rc = infoPtr->bNtfUnicode;
1620 infoPtr->bNtfUnicode = fUnicode;
1625 TREEVIEW_GetUnicodeFormat(TREEVIEW_INFO *infoPtr)
1627 return infoPtr->bNtfUnicode;
1631 TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1633 return infoPtr->uScrollTime;
1637 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1639 UINT uOldScrollTime = infoPtr->uScrollTime;
1641 infoPtr->uScrollTime = min(uScrollTime, 100);
1643 return uOldScrollTime;
1648 TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1654 case (WPARAM)TVSIL_NORMAL:
1655 return (LRESULT)infoPtr->himlNormal;
1657 case (WPARAM)TVSIL_STATE:
1658 return (LRESULT)infoPtr->himlState;
1665 #define TVHEIGHT_MIN 16
1666 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1668 /* Compute the natural height for items. */
1670 TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1674 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1677 /* Height is the maximum of:
1678 * 16 (a hack because our fonts are tiny), and
1679 * The text height + border & margin, and
1680 * The size of the normal image list
1682 GetTextMetricsW(hdc, &tm);
1683 SelectObject(hdc, hOldFont);
1686 height = TVHEIGHT_MIN;
1687 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1688 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1689 if (height < infoPtr->normalImageHeight)
1690 height = infoPtr->normalImageHeight;
1695 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1697 HIMAGELIST himlOld = 0;
1698 int oldWidth = infoPtr->normalImageWidth;
1699 int oldHeight = infoPtr->normalImageHeight;
1702 TRACE("%x,%p\n", wParam, himlNew);
1706 case (WPARAM)TVSIL_NORMAL:
1707 himlOld = infoPtr->himlNormal;
1708 infoPtr->himlNormal = himlNew;
1710 if (himlNew != NULL)
1711 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1712 &infoPtr->normalImageHeight);
1715 infoPtr->normalImageWidth = 0;
1716 infoPtr->normalImageHeight = 0;
1721 case (WPARAM)TVSIL_STATE:
1722 himlOld = infoPtr->himlState;
1723 infoPtr->himlState = himlNew;
1725 if (himlNew != NULL)
1726 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1727 &infoPtr->stateImageHeight);
1730 infoPtr->stateImageWidth = 0;
1731 infoPtr->stateImageHeight = 0;
1737 if (oldWidth != infoPtr->normalImageWidth ||
1738 oldHeight != infoPtr->normalImageHeight)
1740 BOOL bRecalcVisible = FALSE;
1742 if (oldHeight != infoPtr->normalImageHeight &&
1743 !infoPtr->bHeightSet)
1745 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1746 bRecalcVisible = TRUE;
1749 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1750 infoPtr->normalImageWidth != infoPtr->uIndent)
1752 infoPtr->uIndent = infoPtr->normalImageWidth;
1753 bRecalcVisible = TRUE;
1757 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1759 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1760 TREEVIEW_UpdateScrollBars(infoPtr);
1763 TREEVIEW_Invalidate(infoPtr, NULL);
1765 return (LRESULT)himlOld;
1769 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1771 INT prevHeight = infoPtr->uItemHeight;
1773 TRACE("%d \n", newHeight);
1774 if (newHeight == -1)
1776 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1777 infoPtr->bHeightSet = FALSE;
1781 infoPtr->uItemHeight = newHeight;
1782 infoPtr->bHeightSet = TRUE;
1785 /* Round down, unless we support odd ("non even") heights. */
1786 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1787 infoPtr->uItemHeight &= ~1;
1789 if (infoPtr->uItemHeight != prevHeight)
1791 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1792 TREEVIEW_UpdateScrollBars(infoPtr);
1793 TREEVIEW_Invalidate(infoPtr, NULL);
1800 TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1803 return infoPtr->uItemHeight;
1808 TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1810 TRACE("%p\n", infoPtr->hFont);
1811 return (LRESULT)infoPtr->hFont;
1816 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1820 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1826 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1828 UINT uHeight = infoPtr->uItemHeight;
1830 TRACE("%p %i\n", hFont, bRedraw);
1832 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1834 DeleteObject(infoPtr->hBoldFont);
1835 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1836 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1838 if (!infoPtr->bHeightSet)
1839 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1841 if (uHeight != infoPtr->uItemHeight)
1842 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1844 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1846 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1847 TREEVIEW_UpdateScrollBars(infoPtr);
1850 TREEVIEW_Invalidate(infoPtr, NULL);
1857 TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1860 return (LRESULT)infoPtr->clrLine;
1864 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1866 COLORREF prevColor = infoPtr->clrLine;
1869 infoPtr->clrLine = color;
1870 return (LRESULT)prevColor;
1875 TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1878 return (LRESULT)infoPtr->clrText;
1882 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1884 COLORREF prevColor = infoPtr->clrText;
1887 infoPtr->clrText = color;
1889 if (infoPtr->clrText != prevColor)
1890 TREEVIEW_Invalidate(infoPtr, NULL);
1892 return (LRESULT)prevColor;
1897 TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1900 return (LRESULT)infoPtr->clrBk;
1904 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1906 COLORREF prevColor = infoPtr->clrBk;
1909 infoPtr->clrBk = newColor;
1911 if (newColor != prevColor)
1912 TREEVIEW_Invalidate(infoPtr, NULL);
1914 return (LRESULT)prevColor;
1919 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1922 return (LRESULT)infoPtr->clrInsertMark;
1926 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1928 COLORREF prevColor = infoPtr->clrInsertMark;
1930 TRACE("%lx\n", color);
1931 infoPtr->clrInsertMark = color;
1933 return (LRESULT)prevColor;
1938 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1940 TRACE("%d %p\n", wParam, item);
1942 if (!TREEVIEW_ValidItem(infoPtr, item))
1945 infoPtr->insertBeforeorAfter = wParam;
1946 infoPtr->insertMarkItem = item;
1948 TREEVIEW_Invalidate(infoPtr, NULL);
1954 /************************************************************************
1955 * Some serious braindamage here. lParam is a pointer to both the
1956 * input HTREEITEM and the output RECT.
1959 TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1961 TREEVIEW_ITEM *wineItem;
1962 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1966 * validate parameters
1972 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
1976 * If wParam is TRUE return the text size otherwise return
1977 * the whole item size
1981 /* Windows does not send TVN_GETDISPINFO here. */
1983 lpRect->top = wineItem->rect.top;
1984 lpRect->bottom = wineItem->rect.bottom;
1986 lpRect->left = wineItem->textOffset;
1987 lpRect->right = wineItem->textOffset + wineItem->textWidth;
1991 *lpRect = wineItem->rect;
1994 TRACE("%s [L:%ld R:%ld T:%ld B:%ld]\n", fTextRect ? "text" : "item",
1995 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
2000 static inline LRESULT
2001 TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
2003 /* Suprise! This does not take integral height into account. */
2004 return infoPtr->clientHeight / infoPtr->uItemHeight;
2009 TREEVIEW_GetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2011 TREEVIEW_ITEM *wineItem;
2013 wineItem = tvItem->hItem;
2014 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2017 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2019 if (tvItem->mask & TVIF_CHILDREN)
2021 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2022 FIXME("I_CHILDRENCALLBACK not supported\n");
2023 tvItem->cChildren = wineItem->cChildren;
2026 if (tvItem->mask & TVIF_HANDLE)
2027 tvItem->hItem = wineItem;
2029 if (tvItem->mask & TVIF_IMAGE)
2030 tvItem->iImage = wineItem->iImage;
2032 if (tvItem->mask & TVIF_INTEGRAL)
2033 tvItem->iIntegral = wineItem->iIntegral;
2035 /* undocumented: windows ignores TVIF_PARAM and
2036 * * always sets lParam
2038 tvItem->lParam = wineItem->lParam;
2040 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2041 tvItem->iSelectedImage = wineItem->iSelectedImage;
2043 if (tvItem->mask & TVIF_STATE)
2044 /* Careful here - Windows ignores the stateMask when you get the state
2045 That contradicts the documentation, but makes more common sense, masking
2046 retrieval in this way seems overkill */
2047 tvItem->state = wineItem->state;
2049 if (tvItem->mask & TVIF_TEXT)
2053 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2055 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2056 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2060 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2065 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2067 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2068 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2072 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2073 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2077 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2078 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2083 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2084 * which is wrong. */
2086 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2088 TREEVIEW_ITEM *wineItem;
2089 TREEVIEW_ITEM originalItem;
2091 wineItem = tvItem->hItem;
2093 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2096 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2099 /* store the orignal item values */
2100 originalItem = *wineItem;
2102 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2105 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2106 if ((tvItem->mask & TVIF_TEXT
2107 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2108 && ISVISIBLE(wineItem))
2110 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2111 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2114 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2116 /* The refresh updates everything, but we can't wait until then. */
2117 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2119 /* if any of the items values changed, redraw the item */
2120 if(memcmp(&originalItem, wineItem, sizeof(TREEVIEW_ITEM)) ||
2121 (tvItem->stateMask & TVIS_BOLD))
2123 if (tvItem->mask & TVIF_INTEGRAL)
2125 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2126 TREEVIEW_UpdateScrollBars(infoPtr);
2128 TREEVIEW_Invalidate(infoPtr, NULL);
2132 TREEVIEW_UpdateScrollBars(infoPtr);
2133 TREEVIEW_Invalidate(infoPtr, wineItem);
2142 TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2146 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2149 return (wineItem->state & mask);
2153 TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2155 TREEVIEW_ITEM *retval;
2159 /* handle all the global data here */
2162 case TVGN_CHILD: /* Special case: child of 0 is root */
2167 retval = infoPtr->root->firstChild;
2171 retval = infoPtr->selectedItem;
2174 case TVGN_FIRSTVISIBLE:
2175 retval = infoPtr->firstVisible;
2178 case TVGN_DROPHILITE:
2179 retval = infoPtr->dropItem;
2182 case TVGN_LASTVISIBLE:
2183 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2189 TRACE("flags:%x, returns %p\n", which, retval);
2190 return (LRESULT)retval;
2193 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2195 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2201 retval = wineItem->nextSibling;
2204 retval = wineItem->prevSibling;
2207 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2210 retval = wineItem->firstChild;
2212 case TVGN_NEXTVISIBLE:
2213 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2215 case TVGN_PREVIOUSVISIBLE:
2216 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2219 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2223 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2224 return (LRESULT)retval;
2229 TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2231 TRACE(" %d\n", infoPtr->uNumItems);
2232 return (LRESULT)infoPtr->uNumItems;
2236 TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2238 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2240 static const unsigned int state_table[] = { 0, 2, 1 };
2244 state = STATEIMAGEINDEX(item->state);
2245 TRACE("state:%x\n", state);
2246 item->state &= ~TVIS_STATEIMAGEMASK;
2249 state = state_table[state];
2251 item->state |= INDEXTOSTATEIMAGEMASK(state);
2253 TRACE("state:%x\n", state);
2254 TREEVIEW_Invalidate(infoPtr, item);
2259 /* Painting *************************************************************/
2261 /* Draw the lines and expand button for an item. Also draws one section
2262 * of the line from item's parent to item's parent's next sibling. */
2264 TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2266 LONG centerx, centery;
2267 BOOL lar = ((infoPtr->dwStyle
2268 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2271 if (!lar && item->iLevel == 0)
2274 centerx = (item->linesOffset + item->stateOffset) / 2;
2275 centery = (item->rect.top + item->rect.bottom) / 2;
2277 if (infoPtr->dwStyle & TVS_HASLINES)
2279 HPEN hOldPen, hNewPen;
2283 * Get a dotted grey pen
2285 hNewPen = CreatePen(PS_ALTERNATE, 0, infoPtr->clrLine);
2286 hOldPen = SelectObject(hdc, hNewPen);
2288 MoveToEx(hdc, item->stateOffset, centery, NULL);
2289 LineTo(hdc, centerx - 1, centery);
2291 if (item->prevSibling || item->parent != infoPtr->root)
2293 MoveToEx(hdc, centerx, item->rect.top, NULL);
2294 LineTo(hdc, centerx, centery);
2297 if (item->nextSibling)
2299 MoveToEx(hdc, centerx, centery, NULL);
2300 LineTo(hdc, centerx, item->rect.bottom + 1);
2303 /* Draw the line from our parent to its next sibling. */
2304 parent = item->parent;
2305 while (parent != infoPtr->root)
2307 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2309 if (parent->nextSibling
2310 /* skip top-levels unless TVS_LINESATROOT */
2311 && parent->stateOffset > parent->linesOffset)
2313 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2314 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2317 parent = parent->parent;
2320 SelectObject(hdc, hOldPen);
2321 DeleteObject(hNewPen);
2325 * Display the (+/-) signs
2328 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2330 if (item->cChildren)
2332 LONG height = item->rect.bottom - item->rect.top;
2333 LONG width = item->stateOffset - item->linesOffset;
2334 LONG rectsize = min(height, width) / 4;
2335 /* plussize = ceil(rectsize * 3/4) */
2336 LONG plussize = (rectsize + 1) * 3 / 4;
2338 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2339 HPEN hOldPen = SelectObject(hdc, hNewPen);
2340 HBRUSH hbr = CreateSolidBrush(infoPtr->clrBk);
2341 HBRUSH hbrOld = SelectObject(hdc, hbr);
2343 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2344 centerx + rectsize + 2, centery + rectsize + 2);
2346 SelectObject(hdc, hbrOld);
2349 SelectObject(hdc, hOldPen);
2350 DeleteObject(hNewPen);
2352 if (height < 18 || width < 18)
2354 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2355 LineTo(hdc, centerx + plussize, centery);
2357 if (!(item->state & TVIS_EXPANDED))
2359 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2360 LineTo(hdc, centerx, centery + plussize);
2365 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2366 centerx + plussize, centery + 2);
2368 if (!(item->state & TVIS_EXPANDED))
2370 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2371 centerx + 2, centery + plussize);
2372 SetPixel(hdc, centerx - 1, centery, infoPtr->clrBk);
2373 SetPixel(hdc, centerx + 1, centery, infoPtr->clrBk);
2381 TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2387 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2389 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2391 /* The custom draw handler can query the text rectangle,
2393 /* should already be known, set to 0 when changed */
2394 if (!wineItem->textWidth)
2395 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2399 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2401 cditem = TREEVIEW_SendCustomDrawItemNotify
2402 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT);
2403 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2405 if (cditem & CDRF_SKIPDEFAULT)
2407 SelectObject(hdc, hOldFont);
2412 if (cditem & CDRF_NEWFONT)
2413 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2415 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2417 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2420 * Display the images associated with this item
2425 /* State images are displayed to the left of the Normal image
2426 * image number is in state; zero should be `display no image'.
2428 imageIndex = STATEIMAGEINDEX(wineItem->state);
2430 if (infoPtr->himlState && imageIndex)
2432 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2433 wineItem->stateOffset,
2434 centery - infoPtr->stateImageHeight / 2,
2438 /* Now, draw the normal image; can be either selected or
2439 * non-selected image.
2442 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2444 /* The item is currently selected */
2445 imageIndex = wineItem->iSelectedImage;
2449 /* The item is not selected */
2450 imageIndex = wineItem->iImage;
2453 if (infoPtr->himlNormal)
2455 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2457 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2458 wineItem->imageOffset,
2459 centery - infoPtr->normalImageHeight / 2,
2460 ILD_NORMAL | ovlIdx);
2466 * Display the text associated with this item
2469 /* Don't paint item's text if it's being edited */
2470 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2472 if (wineItem->pszText)
2474 COLORREF oldTextColor = 0;
2477 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2480 oldBkMode = SetBkMode(hdc, TRANSPARENT);
2482 /* - If item is drop target or it is selected and window is in focus -
2483 * use blue background (COLOR_HIGHLIGHT).
2484 * - If item is selected, window is not in focus, but it has style
2485 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2486 * - Otherwise - don't fill background
2488 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2489 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2490 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2492 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2494 hbrBk = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
2496 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2500 hbrBk = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
2502 if (infoPtr->clrText == -1)
2504 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2506 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2511 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2512 oldTextColor = SetTextColor(hdc, comctl32_color.clrHighlight);
2513 else if (infoPtr->clrText == -1)
2515 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2517 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2520 rcText.top = wineItem->rect.top;
2521 rcText.bottom = wineItem->rect.bottom;
2522 rcText.left = wineItem->textOffset;
2523 rcText.right = rcText.left + wineItem->textWidth + 4;
2527 FillRect(hdc, &rcText, hbrBk);
2528 DeleteObject(hbrBk);
2531 /* Draw the box around the selected item */
2532 if ((wineItem == infoPtr->selectedItem) && inFocus)
2534 DrawFocusRect(hdc,&rcText);
2537 InflateRect(&rcText, -2, -1); /* allow for the focus rect */
2539 TRACE("drawing text %s at (%ld,%ld)-(%ld,%ld)\n",
2540 debugstr_w(wineItem->pszText),
2541 rcText.left, rcText.top, rcText.right, rcText.bottom);
2546 lstrlenW(wineItem->pszText),
2548 DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
2550 /* Restore the hdc state */
2551 SetTextColor(hdc, oldTextColor);
2553 if (oldBkMode != TRANSPARENT)
2554 SetBkMode(hdc, oldBkMode);
2558 /* Draw insertion mark if necessary */
2560 if (infoPtr->insertMarkItem)
2561 TRACE("item:%d,mark:%d\n",
2562 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2563 (int)infoPtr->insertMarkItem);
2565 if (wineItem == infoPtr->insertMarkItem)
2567 HPEN hNewPen, hOldPen;
2571 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2572 hOldPen = SelectObject(hdc, hNewPen);
2574 if (infoPtr->insertBeforeorAfter)
2575 offset = wineItem->rect.bottom - 1;
2577 offset = wineItem->rect.top + 1;
2579 left = wineItem->textOffset - 2;
2580 right = wineItem->textOffset + wineItem->textWidth + 2;
2582 MoveToEx(hdc, left, offset - 3, NULL);
2583 LineTo(hdc, left, offset + 4);
2585 MoveToEx(hdc, left, offset, NULL);
2586 LineTo(hdc, right + 1, offset);
2588 MoveToEx(hdc, right, offset + 3, NULL);
2589 LineTo(hdc, right, offset - 4);
2591 SelectObject(hdc, hOldPen);
2592 DeleteObject(hNewPen);
2595 if (cditem & CDRF_NOTIFYPOSTPAINT)
2597 cditem = TREEVIEW_SendCustomDrawItemNotify
2598 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT);
2599 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2602 SelectObject(hdc, hOldFont);
2605 /* Computes treeHeight and treeWidth and updates the scroll bars.
2608 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2610 TREEVIEW_ITEM *wineItem;
2611 HWND hwnd = infoPtr->hwnd;
2615 LONG scrollX = infoPtr->scrollX;
2617 infoPtr->treeWidth = 0;
2618 infoPtr->treeHeight = 0;
2620 /* We iterate through all visible items in order to get the tree height
2622 wineItem = infoPtr->root->firstChild;
2624 while (wineItem != NULL)
2626 if (ISVISIBLE(wineItem))
2628 /* actually we draw text at textOffset + 2 */
2629 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2630 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2632 /* This is scroll-adjusted, but we fix this below. */
2633 infoPtr->treeHeight = wineItem->rect.bottom;
2636 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2639 /* Fix the scroll adjusted treeHeight and treeWidth. */
2640 if (infoPtr->root->firstChild)
2641 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2643 infoPtr->treeWidth += infoPtr->scrollX;
2645 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2647 /* Adding one scroll bar may take up enough space that it forces us
2648 * to add the other as well. */
2649 if (infoPtr->treeHeight > infoPtr->clientHeight)
2653 if (infoPtr->treeWidth
2654 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2657 else if (infoPtr->treeWidth > infoPtr->clientWidth)
2660 if (!vert && horz && infoPtr->treeHeight
2661 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2664 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2666 si.cbSize = sizeof(SCROLLINFO);
2667 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2672 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2673 if ( si.nPage && NULL != infoPtr->firstVisible)
2675 si.nPos = infoPtr->firstVisible->visibleOrder;
2676 si.nMax = infoPtr->maxVisibleOrder - 1;
2678 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2680 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2681 ShowScrollBar(hwnd, SB_VERT, TRUE);
2682 infoPtr->uInternalStatus |= TV_VSCROLL;
2686 if (infoPtr->uInternalStatus & TV_VSCROLL)
2687 ShowScrollBar(hwnd, SB_VERT, FALSE);
2688 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2693 if (infoPtr->uInternalStatus & TV_VSCROLL)
2694 ShowScrollBar(hwnd, SB_VERT, FALSE);
2695 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2700 si.nPage = infoPtr->clientWidth;
2701 si.nPos = infoPtr->scrollX;
2702 si.nMax = infoPtr->treeWidth - 1;
2704 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2706 si.nPos = si.nMax - max( si.nPage-1, 0 );
2710 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2711 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2712 infoPtr->uInternalStatus |= TV_HSCROLL;
2714 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2718 if (infoPtr->uInternalStatus & TV_HSCROLL)
2719 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2720 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2725 if (infoPtr->scrollX != scrollX)
2727 TREEVIEW_HScroll(infoPtr,
2728 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2732 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2735 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2737 TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2739 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2742 GetClientRect(infoPtr->hwnd, &rect);
2743 FillRect(hDC, &rect, hBrush);
2744 DeleteObject(hBrush);
2750 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2752 HWND hwnd = infoPtr->hwnd;
2754 TREEVIEW_ITEM *wineItem;
2756 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2758 TRACE("empty window\n");
2762 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2765 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2767 ReleaseDC(hwnd, hdc);
2771 for (wineItem = infoPtr->root->firstChild;
2773 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2775 if (ISVISIBLE(wineItem))
2777 /* Avoid unneeded calculations */
2778 if (wineItem->rect.top > rect.bottom)
2780 if (wineItem->rect.bottom < rect.top)
2783 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2787 TREEVIEW_UpdateScrollBars(infoPtr);
2789 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2791 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2795 TREEVIEW_Invalidate(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2798 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2800 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2804 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2815 if (!GetUpdateRect(infoPtr->hwnd, &rc, TRUE))
2819 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2820 if (!hbitmap) return 0;
2821 GetObjectW(hbitmap, sizeof(BITMAP), &bitmap);
2822 rc.left = 0; rc.top = 0;
2823 rc.right = bitmap.bmWidth;
2824 rc.bottom = bitmap.bmHeight;
2825 TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
2830 hdc = BeginPaint(infoPtr->hwnd, &ps);
2834 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2835 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2838 EndPaint(infoPtr->hwnd, &ps);
2844 /* Sorting **************************************************************/
2846 /***************************************************************************
2847 * Forward the DPA local callback to the treeview owner callback
2850 TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2852 /* Forward the call to the client-defined callback */
2853 return pCallBackSort->lpfnCompare(first->lParam,
2855 pCallBackSort->lParam);
2858 /***************************************************************************
2859 * Treeview native sort routine: sort on item text.
2862 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2863 TREEVIEW_INFO *infoPtr)
2865 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2866 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2868 if(first->pszText && second->pszText)
2869 return lstrcmpiW(first->pszText, second->pszText);
2870 else if(first->pszText)
2872 else if(second->pszText)
2878 /* Returns the number of physical children belonging to item. */
2880 TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2885 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2891 /* Returns a DPA containing a pointer to each physical child of item in
2892 * sibling order. If item has no children, an empty DPA is returned. */
2894 TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2896 HTREEITEM child = item->firstChild;
2898 HDPA list = DPA_Create(8);
2899 if (list == 0) return NULL;
2901 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2903 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2913 /***************************************************************************
2914 * Setup the treeview structure with regards of the sort method
2915 * and sort the children of the TV item specified in lParam
2916 * fRecurse: currently unused. Should be zero.
2917 * parent: if pSort!=NULL, should equal pSort->hParent.
2918 * otherwise, item which child items are to be sorted.
2919 * pSort: sort method info. if NULL, sort on item text.
2920 * if non-NULL, sort on item's lParam content, and let the
2921 * application decide what that means. See also TVM_SORTCHILDRENCB.
2925 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2929 PFNDPACOMPARE pfnCompare;
2932 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2933 if (parent == TVI_ROOT || parent == NULL)
2934 parent = infoPtr->root;
2936 /* Check for a valid handle to the parent item */
2937 if (!TREEVIEW_ValidItem(infoPtr, parent))
2939 ERR("invalid item hParent=%x\n", (INT)parent);
2945 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2946 lpCompare = (LPARAM)pSort;
2950 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2951 lpCompare = (LPARAM)infoPtr;
2954 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2956 /* Make sure there is something to sort */
2959 /* TREEVIEW_ITEM rechaining */
2962 HTREEITEM nextItem = 0;
2963 HTREEITEM prevItem = 0;
2965 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
2967 if (sortList == NULL)
2970 /* let DPA sort the list */
2971 DPA_Sort(sortList, pfnCompare, lpCompare);
2973 /* The order of DPA entries has been changed, so fixup the
2974 * nextSibling and prevSibling pointers. */
2976 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
2977 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
2979 /* link the two current item toghether */
2980 item->nextSibling = nextItem;
2981 nextItem->prevSibling = item;
2983 if (prevItem == NULL)
2985 /* this is the first item, update the parent */
2986 parent->firstChild = item;
2987 item->prevSibling = NULL;
2991 /* fix the back chaining */
2992 item->prevSibling = prevItem;
2995 /* get ready for the next one */
3000 /* the last item is pointed to by item and never has a sibling */
3001 item->nextSibling = NULL;
3002 parent->lastChild = item;
3004 DPA_Destroy(sortList);
3006 TREEVIEW_VerifyTree(infoPtr);
3008 if (parent->state & TVIS_EXPANDED)
3010 int visOrder = infoPtr->firstVisible->visibleOrder;
3012 if (parent == infoPtr->root)
3013 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3015 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3017 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3019 TREEVIEW_ITEM *item;
3021 for (item = infoPtr->root->firstChild; item != NULL;
3022 item = TREEVIEW_GetNextListItem(infoPtr, item))
3024 if (item->visibleOrder == visOrder)
3028 if (!item) item = parent->firstChild;
3029 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3032 TREEVIEW_Invalidate(infoPtr, NULL);
3041 /***************************************************************************
3042 * Setup the treeview structure with regards of the sort method
3043 * and sort the children of the TV item specified in lParam
3046 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3048 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3052 /***************************************************************************
3053 * Sort the children of the TV item specified in lParam.
3056 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3058 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3062 /* Expansion/Collapse ***************************************************/
3065 TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3068 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3069 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3070 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3075 TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3078 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3079 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3080 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3085 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3086 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3088 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3089 BOOL bRemoveChildren, BOOL bUser)
3091 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3092 BOOL bSetSelection, bSetFirstVisible;
3094 LONG scrollDist = 0;
3095 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3097 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3099 if (!(wineItem->state & TVIS_EXPANDED))
3102 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3103 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3105 if (wineItem->firstChild == NULL)
3108 wineItem->state &= ~TVIS_EXPANDED;
3110 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3111 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3113 bSetSelection = (infoPtr->selectedItem != NULL
3114 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3116 bSetFirstVisible = (infoPtr->firstVisible != NULL
3117 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3122 if (tmpItem->nextSibling)
3124 nextItem = tmpItem->nextSibling;
3127 tmpItem = tmpItem->parent;
3131 scrollDist = nextItem->rect.top;
3133 if (bRemoveChildren)
3135 INT old_cChildren = wineItem->cChildren;
3136 TRACE("TVE_COLLAPSERESET\n");
3137 wineItem->state &= ~TVIS_EXPANDEDONCE;
3138 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3139 wineItem->cChildren = old_cChildren;
3142 if (wineItem->firstChild)
3144 TREEVIEW_ITEM *item, *sibling;
3146 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3148 for (item = wineItem->firstChild; item != sibling;
3149 item = TREEVIEW_GetNextListItem(infoPtr, item))
3151 item->visibleOrder = -1;
3155 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3158 scrollDist = -(scrollDist - nextItem->rect.top);
3162 /* Don't call DoSelectItem, it sends notifications. */
3163 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3164 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3165 wineItem->state |= TVIS_SELECTED;
3166 infoPtr->selectedItem = wineItem;
3169 TREEVIEW_UpdateScrollBars(infoPtr);
3171 scrollRect.left = 0;
3172 scrollRect.right = infoPtr->clientWidth;
3173 scrollRect.bottom = infoPtr->clientHeight;
3177 scrollRect.top = nextItem->rect.top;
3179 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3180 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3181 TREEVIEW_Invalidate(infoPtr, wineItem);
3183 scrollRect.top = wineItem->rect.top;
3184 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3187 TREEVIEW_SetFirstVisible(infoPtr,
3188 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3195 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3196 BOOL bExpandPartial, BOOL bUser)
3199 LONG orgNextTop = 0;
3201 TREEVIEW_ITEM *nextItem, *tmpItem;
3205 if (wineItem->state & TVIS_EXPANDED)
3208 tmpItem = wineItem; nextItem = NULL;
3211 if (tmpItem->nextSibling)
3213 nextItem = tmpItem->nextSibling;
3216 tmpItem = tmpItem->parent;
3220 orgNextTop = nextItem->rect.top;
3222 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3224 if (bUser || ((wineItem->cChildren != 0) &&
3225 !(wineItem->state & TVIS_EXPANDEDONCE)))
3227 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3229 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3233 if (!wineItem->firstChild)
3236 wineItem->state |= TVIS_EXPANDED;
3237 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3238 wineItem->state |= TVIS_EXPANDEDONCE;
3242 if (!wineItem->firstChild)
3245 /* this item has already been expanded */
3246 wineItem->state |= TVIS_EXPANDED;
3250 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3252 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3253 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3254 TREEVIEW_UpdateScrollBars(infoPtr);
3256 scrollRect.left = 0;
3257 scrollRect.bottom = infoPtr->treeHeight;
3258 scrollRect.right = infoPtr->clientWidth;
3261 scrollDist = nextItem->rect.top - orgNextTop;
3262 scrollRect.top = orgNextTop;
3264 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3265 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3266 TREEVIEW_Invalidate (infoPtr, wineItem);
3268 scrollRect.top = wineItem->rect.top;
3269 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3272 /* Scroll up so that as many children as possible are visible.
3273 * This fails when expanding causes an HScroll bar to appear, but we
3274 * don't know that yet, so the last item is obscured. */
3275 if (wineItem->firstChild != NULL)
3277 int nChildren = wineItem->lastChild->visibleOrder
3278 - wineItem->firstChild->visibleOrder + 1;
3280 int visible_pos = wineItem->visibleOrder
3281 - infoPtr->firstVisible->visibleOrder;
3283 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3285 if (visible_pos > 0 && nChildren > rows_below)
3287 int scroll = nChildren - rows_below;
3289 if (scroll > visible_pos)
3290 scroll = visible_pos;
3294 TREEVIEW_ITEM *newFirstVisible
3295 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3299 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3308 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3312 if (wineItem->state & TVIS_EXPANDED)
3313 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3315 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3319 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3321 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3323 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3325 if (TREEVIEW_HasChildren(infoPtr, item))
3326 TREEVIEW_ExpandAll(infoPtr, item);
3330 /* Note:If the specified item is the child of a collapsed parent item,
3331 the parent's list of child items is (recursively) expanded to reveal the
3332 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3333 know if it also applies here.
3337 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3339 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3342 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3343 TREEVIEW_ItemName(wineItem), flag,
3344 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3346 switch (flag & TVE_TOGGLE)
3349 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3353 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3357 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3364 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3368 /* Hit-Testing **********************************************************/
3370 static TREEVIEW_ITEM *
3371 TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3373 TREEVIEW_ITEM *wineItem;
3376 if (!infoPtr->firstVisible)
3379 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3381 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3382 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3384 if (row >= wineItem->visibleOrder
3385 && row < wineItem->visibleOrder + wineItem->iIntegral)
3393 TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3395 TREEVIEW_ITEM *wineItem;
3401 GetClientRect(infoPtr->hwnd, &rect);
3408 status |= TVHT_TOLEFT;
3410 else if (x > rect.right)
3412 status |= TVHT_TORIGHT;
3417 status |= TVHT_ABOVE;
3419 else if (y > rect.bottom)
3421 status |= TVHT_BELOW;
3426 lpht->flags = status;
3427 return (LRESULT)(HTREEITEM)NULL;
3430 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3433 lpht->flags = TVHT_NOWHERE;
3434 return (LRESULT)(HTREEITEM)NULL;
3437 if (x >= wineItem->textOffset + wineItem->textWidth)
3439 lpht->flags = TVHT_ONITEMRIGHT;
3441 else if (x >= wineItem->textOffset)
3443 lpht->flags = TVHT_ONITEMLABEL;
3445 else if (x >= wineItem->imageOffset)
3447 lpht->flags = TVHT_ONITEMICON;
3449 else if (x >= wineItem->stateOffset)
3451 lpht->flags = TVHT_ONITEMSTATEICON;
3453 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3455 lpht->flags = TVHT_ONITEMBUTTON;
3459 lpht->flags = TVHT_ONITEMINDENT;
3462 lpht->hItem = wineItem;
3463 TRACE("(%ld,%ld):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3465 return (LRESULT)wineItem;
3468 /* Item Label Editing ***************************************************/
3471 TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3473 return (LRESULT)infoPtr->hwndEdit;
3476 static LRESULT CALLBACK
3477 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3479 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3480 BOOL bCancel = FALSE;
3486 TRACE("WM_PAINT start\n");
3487 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3489 TRACE("WM_PAINT done\n");
3493 if (infoPtr->bIgnoreEditKillFocus)
3498 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3501 if (wParam == (WPARAM)VK_ESCAPE)
3506 else if (wParam == (WPARAM)VK_RETURN)
3513 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3516 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3517 /* eg. Using a messagebox */
3519 infoPtr->bIgnoreEditKillFocus = TRUE;
3520 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3521 infoPtr->bIgnoreEditKillFocus = FALSE;
3527 /* should handle edit control messages here */
3530 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3532 TRACE("%x %ld\n", wParam, lParam);
3534 switch (HIWORD(wParam))
3539 * Adjust the edit window size
3542 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3543 HDC hdc = GetDC(infoPtr->hwndEdit);
3546 HFONT hFont, hOldFont = 0;
3548 infoPtr->bLabelChanged = TRUE;
3550 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer));
3552 /* Select font to get the right dimension of the string */
3553 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3557 hOldFont = SelectObject(hdc, hFont);
3560 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3562 TEXTMETRICW textMetric;
3564 /* Add Extra spacing for the next character */
3565 GetTextMetricsW(hdc, &textMetric);
3566 sz.cx += (textMetric.tmMaxCharWidth * 2);
3568 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3570 infoPtr->clientWidth - editItem->textOffset + 2);
3572 SetWindowPos(infoPtr->hwndEdit,
3577 editItem->rect.bottom - editItem->rect.top + 3,
3578 SWP_NOMOVE | SWP_DRAWFRAME);
3583 SelectObject(hdc, hOldFont);
3586 ReleaseDC(infoPtr->hwnd, hdc);
3591 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3598 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3600 HWND hwnd = infoPtr->hwnd;
3603 TREEVIEW_ITEM *editItem = hItem;
3604 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3607 TEXTMETRICW textMetric;
3608 static const WCHAR EditW[] = {'E','d','i','t',0};
3610 TRACE("%x %p\n", (unsigned)hwnd, hItem);
3611 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3614 if (infoPtr->hwndEdit)
3615 return infoPtr->hwndEdit;
3617 infoPtr->bLabelChanged = FALSE;
3619 /* Make sure that edit item is selected */
3620 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3621 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3623 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3626 /* Select the font to get appropriate metric dimensions */
3627 if (infoPtr->hFont != 0)
3629 hOldFont = SelectObject(hdc, infoPtr->hFont);
3632 /* Get string length in pixels */
3633 GetTextExtentPoint32W(hdc, editItem->pszText, strlenW(editItem->pszText),
3636 /* Add Extra spacing for the next character */
3637 GetTextMetricsW(hdc, &textMetric);
3638 sz.cx += (textMetric.tmMaxCharWidth * 2);
3640 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3641 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3643 if (infoPtr->hFont != 0)
3645 SelectObject(hdc, hOldFont);
3648 ReleaseDC(hwnd, hdc);
3649 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3652 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3653 WS_CLIPSIBLINGS | ES_WANTRETURN |
3654 ES_LEFT, editItem->textOffset - 2,
3655 editItem->rect.top - 1, sz.cx + 3,
3656 editItem->rect.bottom -
3657 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3658 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3660 infoPtr->hwndEdit = hwndEdit;
3662 /* Get a 2D border. */
3663 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3664 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3665 SetWindowLongW(hwndEdit, GWL_STYLE,
3666 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3668 SendMessageW(hwndEdit, WM_SETFONT,
3669 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3671 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3673 TREEVIEW_Edit_SubclassProc);
3675 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3677 DestroyWindow(hwndEdit);
3678 infoPtr->hwndEdit = 0;
3682 infoPtr->selectedItem = hItem;
3683 SetWindowTextW(hwndEdit, editItem->pszText);
3685 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3686 ShowWindow(hwndEdit, SW_SHOW);
3693 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3695 HWND hwnd = infoPtr->hwnd;
3696 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3699 WCHAR tmpText[1024] = { '\0' };
3700 WCHAR *newText = tmpText;
3703 if (!infoPtr->hwndEdit)
3706 tvdi.hdr.hwndFrom = hwnd;
3707 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3708 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3710 tvdi.item.hItem = editedItem;
3711 tvdi.item.state = editedItem->state;
3712 tvdi.item.lParam = editedItem->lParam;
3716 if (!infoPtr->bNtfUnicode)
3717 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3719 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3721 if (iLength >= 1023)
3723 ERR("Insufficient space to retrieve new item label\n");
3726 tvdi.item.mask = TVIF_TEXT;
3727 tvdi.item.pszText = tmpText;
3728 tvdi.item.cchTextMax = iLength + 1;
3732 tvdi.item.pszText = NULL;
3733 tvdi.item.cchTextMax = 0;
3736 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
3737 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3739 if (!bCancel && bCommit) /* Apply the changes */
3741 if (!infoPtr->bNtfUnicode)
3743 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3744 newText = Alloc(len * sizeof(WCHAR));
3745 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3749 if (strcmpW(newText, editedItem->pszText) != 0)
3751 if (NULL == ReAlloc(editedItem->pszText, iLength + 1))
3753 ERR("OutOfMemory, cannot allocate space for label\n");
3754 DestroyWindow(infoPtr->hwndEdit);
3755 infoPtr->hwndEdit = 0;
3760 editedItem->cchTextMax = iLength + 1;
3761 strcpyW(editedItem->pszText, newText);
3764 if(newText != tmpText) Free(newText);
3767 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3768 DestroyWindow(infoPtr->hwndEdit);
3769 infoPtr->hwndEdit = 0;
3774 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3776 if (wParam != TV_EDIT_TIMER)
3778 ERR("got unknown timer\n");
3782 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3783 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3785 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3791 /* Mouse Tracking/Drag **************************************************/
3793 /***************************************************************************
3794 * This is quite unusual piece of code, but that's how it's implemented in
3798 TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3800 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3801 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3805 r.top = pt.y - cyDrag;
3806 r.left = pt.x - cxDrag;
3807 r.bottom = pt.y + cyDrag;
3808 r.right = pt.x + cxDrag;
3810 SetCapture(infoPtr->hwnd);
3814 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3816 if (msg.message == WM_MOUSEMOVE)
3818 pt.x = (short)LOWORD(msg.lParam);
3819 pt.y = (short)HIWORD(msg.lParam);
3820 if (PtInRect(&r, pt))
3828 else if (msg.message >= WM_LBUTTONDOWN &&
3829 msg.message <= WM_RBUTTONDBLCLK)
3831 if (msg.message == WM_RBUTTONUP)
3832 TREEVIEW_RButtonUp(infoPtr, &pt);
3836 DispatchMessageW(&msg);
3839 if (GetCapture() != infoPtr->hwnd)
3849 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3851 TREEVIEW_ITEM *wineItem;
3855 SetFocus(infoPtr->hwnd);
3857 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3859 /* If there is pending 'edit label' event - kill it now */
3860 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3863 hit.pt.x = (short)LOWORD(lParam);
3864 hit.pt.y = (short)HIWORD(lParam);
3866 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3869 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3871 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3875 case TVHT_ONITEMRIGHT:
3876 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3879 case TVHT_ONITEMINDENT:
3880 if (!(infoPtr->dwStyle & TVS_HASLINES))
3886 int level = hit.pt.x / infoPtr->uIndent;
3887 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3889 while (wineItem->iLevel > level)
3891 wineItem = wineItem->parent;
3897 case TVHT_ONITEMLABEL:
3898 case TVHT_ONITEMICON:
3899 case TVHT_ONITEMBUTTON:
3900 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3903 case TVHT_ONITEMSTATEICON:
3904 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3905 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3907 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3916 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3918 HWND hwnd = infoPtr->hwnd;
3920 BOOL bTrack, bDoLabelEdit;
3923 /* If Edit control is active - kill it and return.
3924 * The best way to do it is to set focus to itself.
3925 * Edit control subclassed procedure will automatically call
3928 if (infoPtr->hwndEdit)
3934 ht.pt.x = (short)LOWORD(lParam);
3935 ht.pt.y = (short)HIWORD(lParam);
3937 TREEVIEW_HitTest(infoPtr, &ht);
3938 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3940 /* update focusedItem and redraw both items */
3941 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3943 infoPtr->focusedItem = ht.hItem;
3944 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3946 if(infoPtr->selectedItem)
3947 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3950 bTrack = (ht.flags & TVHT_ONITEM)
3951 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3954 * If the style allows editing and the node is already selected
3955 * and the click occurred on the item label...
3957 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
3958 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
3960 /* Send NM_CLICK right away */
3962 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3965 if (ht.flags & TVHT_ONITEMBUTTON)
3967 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
3971 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
3972 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3974 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
3975 infoPtr->dropItem = ht.hItem;
3977 /* clean up focusedItem as we dragged and won't select this item */
3978 if(infoPtr->focusedItem)
3980 /* refresh the item that was focused */
3981 tempItem = infoPtr->focusedItem;
3982 infoPtr->focusedItem = 0;
3983 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
3985 /* refresh the selected item to return the filled background */
3986 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
3993 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3998 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3999 KillTimer(hwnd, TV_EDIT_TIMER);
4001 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4002 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4004 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4007 * if we are TVS_SINGLEEXPAND then we want this single click to
4008 * do a bunch of things.
4010 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4011 (infoPtr->hwndEdit == 0))
4013 TREEVIEW_ITEM *SelItem;
4016 * Send the notification
4018 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4021 * Close the previous selection all the way to the root
4022 * as long as the new selection is not a child
4024 if((infoPtr->selectedItem)
4025 && (infoPtr->selectedItem != ht.hItem))
4027 BOOL closeit = TRUE;
4030 /* determine if the hitItem is a child of the currently selected item */
4031 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4033 closeit = (SelItem != infoPtr->selectedItem);
4034 SelItem = SelItem->parent;
4039 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4040 SelItem = infoPtr->selectedItem;
4042 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4044 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4045 SelItem = SelItem->parent;
4051 * Expand the current item
4053 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4056 /* Select the current item */
4057 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4059 else if (ht.flags & TVHT_ONITEMSTATEICON)
4061 /* TVS_CHECKBOXES requires us to toggle the current state */
4062 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4063 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4073 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4077 if (infoPtr->hwndEdit)
4079 SetFocus(infoPtr->hwnd);
4083 ht.pt.x = (short)LOWORD(lParam);
4084 ht.pt.y = (short)HIWORD(lParam);
4086 TREEVIEW_HitTest(infoPtr, &ht);
4088 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4092 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4093 infoPtr->dropItem = ht.hItem;
4098 SetFocus(infoPtr->hwnd);
4099 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4106 TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
4113 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4115 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4119 HBITMAP hbmp, hOldbmp;
4126 if (!(infoPtr->himlNormal))
4129 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4132 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4134 hwtop = GetDesktopWindow();
4135 htopdc = GetDC(hwtop);
4136 hdc = CreateCompatibleDC(htopdc);
4138 hOldFont = SelectObject(hdc, infoPtr->hFont);
4139 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4141 TRACE("%ld %ld %s %d\n", size.cx, size.cy, debugstr_w(dragItem->pszText),
4142 strlenW(dragItem->pszText));
4143 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4144 hOldbmp = SelectObject(hdc, hbmp);
4146 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4151 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4152 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4156 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4157 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4160 /* draw item text */
4162 SetRect(&rc, cx, 0, size.cx, size.cy);
4163 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4165 SelectObject(hdc, hOldFont);
4166 SelectObject(hdc, hOldbmp);
4168 ImageList_Add(infoPtr->dragList, hbmp, 0);
4172 ReleaseDC(hwtop, htopdc);
4174 return (LRESULT)infoPtr->dragList;
4177 /* Selection ************************************************************/
4180 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4183 TREEVIEW_ITEM *prevSelect;
4186 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4188 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4189 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4190 newSelect ? newSelect->state : 0);
4192 /* reset and redraw focusedItem if focusedItem was set so we don't */
4193 /* have to worry about the previously focused item when we set a new one */
4194 if(infoPtr->focusedItem)
4196 rcFocused = (infoPtr->focusedItem)->rect;
4197 infoPtr->focusedItem = 0;
4198 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4204 prevSelect = infoPtr->selectedItem;
4206 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4209 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4215 prevSelect->state &= ~TVIS_SELECTED;
4217 newSelect->state |= TVIS_SELECTED;
4219 infoPtr->selectedItem = newSelect;
4221 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4223 TREEVIEW_SendTreeviewNotify(infoPtr,
4226 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4229 TREEVIEW_Invalidate(infoPtr, prevSelect);
4230 TREEVIEW_Invalidate(infoPtr, newSelect);
4233 case TVGN_DROPHILITE:
4234 prevSelect = infoPtr->dropItem;
4237 prevSelect->state &= ~TVIS_DROPHILITED;
4239 infoPtr->dropItem = newSelect;
4242 newSelect->state |= TVIS_DROPHILITED;
4244 TREEVIEW_Invalidate(infoPtr, prevSelect);
4245 TREEVIEW_Invalidate(infoPtr, newSelect);
4248 case TVGN_FIRSTVISIBLE:
4249 if (newSelect != NULL)
4251 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4252 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4253 TREEVIEW_Invalidate(infoPtr, NULL);
4258 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4262 /* FIXME: handle NM_KILLFOCUS etc */
4264 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4266 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4269 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4271 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4277 /*************************************************************************
4278 * TREEVIEW_ProcessLetterKeys
4280 * Processes keyboard messages generated by pressing the letter keys
4282 * What this does is perform a case insensitive search from the
4283 * current position with the following quirks:
4284 * - If two chars or more are pressed in quick succession we search
4285 * for the corresponding string (e.g. 'abc').
4286 * - If there is a delay we wipe away the current search string and
4287 * restart with just that char.
4288 * - If the user keeps pressing the same character, whether slowly or
4289 * fast, so that the search string is entirely composed of this
4290 * character ('aaaaa' for instance), then we search for first item
4291 * that starting with that character.
4292 * - If the user types the above character in quick succession, then
4293 * we must also search for the corresponding string ('aaaaa'), and
4294 * go to that string if there is a match.
4302 * - The current implementation has a list of characters it will
4303 * accept and it ignores averything else. In particular it will
4304 * ignore accentuated characters which seems to match what
4305 * Windows does. But I'm not sure it makes sense to follow
4307 * - We don't sound a beep when the search fails.
4308 * - The search should start from the focused item, not from the selected
4309 * item. One reason for this is to allow for multiple selections in trees.
4310 * But currently infoPtr->focusedItem does not seem very usable.
4314 * TREEVIEW_ProcessLetterKeys
4316 static INT TREEVIEW_ProcessLetterKeys(
4317 HWND hwnd, /* handle to the window */
4318 WPARAM charCode, /* the character code, the actual character */
4319 LPARAM keyData /* key data */
4322 TREEVIEW_INFO *infoPtr;
4324 HTREEITEM endidx,idx;
4326 WCHAR buffer[MAX_PATH];
4327 DWORD timestamp,elapsed;
4329 /* simple parameter checking */
4330 if (!hwnd || !charCode || !keyData)
4333 infoPtr=(TREEVIEW_INFO*)GetWindowLongPtrW(hwnd, 0);
4337 /* only allow the valid WM_CHARs through */
4338 if (!isalnum(charCode) &&
4339 charCode != '.' && charCode != '`' && charCode != '!' &&
4340 charCode != '@' && charCode != '#' && charCode != '$' &&
4341 charCode != '%' && charCode != '^' && charCode != '&' &&
4342 charCode != '*' && charCode != '(' && charCode != ')' &&
4343 charCode != '-' && charCode != '_' && charCode != '+' &&
4344 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4345 charCode != '}' && charCode != '[' && charCode != '{' &&
4346 charCode != '/' && charCode != '?' && charCode != '>' &&
4347 charCode != '<' && charCode != ',' && charCode != '~')
4350 /* compute how much time elapsed since last keypress */
4351 timestamp = GetTickCount();
4352 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4353 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4355 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4358 /* update the search parameters */
4359 infoPtr->lastKeyPressTimestamp=timestamp;
4360 if (elapsed < KEY_DELAY) {
4361 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4362 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4364 if (infoPtr->charCode != charCode) {
4365 infoPtr->charCode=charCode=0;
4368 infoPtr->charCode=charCode;
4369 infoPtr->szSearchParam[0]=charCode;
4370 infoPtr->nSearchParamLength=1;
4371 /* Redundant with the 1 char string */
4375 /* and search from the current position */
4377 if (infoPtr->selectedItem != NULL) {
4378 endidx=infoPtr->selectedItem;
4379 /* if looking for single character match,
4380 * then we must always move forward
4382 if (infoPtr->nSearchParamLength == 1)
4383 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4388 idx=infoPtr->root->firstChild;
4391 /* At the end point, sort out wrapping */
4394 /* If endidx is null, stop at the last item (ie top to bottom) */
4398 /* Otherwise, start again at the very beginning */
4399 idx=infoPtr->root->firstChild;
4401 /* But if we are stopping on the first child, end now! */
4402 if (idx == endidx) break;
4406 ZeroMemory(&item, sizeof(item));
4407 item.mask = TVIF_TEXT;
4409 item.pszText = buffer;
4410 item.cchTextMax = sizeof(buffer);
4411 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4413 /* check for a match */
4414 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4417 } else if ( (charCode != 0) && (nItem == NULL) &&
4418 (nItem != infoPtr->selectedItem) &&
4419 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4420 /* This would work but we must keep looking for a longer match */
4423 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4424 } while (idx != endidx);
4426 if (nItem != NULL) {
4427 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4428 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4435 /* Scrolling ************************************************************/
4438 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4441 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4442 HTREEITEM newFirstVisible = NULL;
4443 int visible_pos = -1;
4445 if (!TREEVIEW_ValidItem(infoPtr, item))
4448 if (!ISVISIBLE(item))
4450 /* Expand parents as necessary. */
4453 /* see if we are trying to ensure that root is vislble */
4454 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4455 parent = item->parent;
4457 parent = item; /* this item is the topmost item */
4459 while (parent != infoPtr->root)
4461 if (!(parent->state & TVIS_EXPANDED))
4462 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4464 parent = parent->parent;
4468 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4470 TRACE("%p (%s) %ld - %ld viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4471 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4473 if (hasFirstVisible)
4474 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4476 if (visible_pos < 0)
4478 /* item is before the start of the list: put it at the top. */
4479 newFirstVisible = item;
4481 else if (visible_pos >= viscount
4482 /* Sometimes, before we are displayed, GVC is 0, causing us to
4483 * spuriously scroll up. */
4484 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4486 /* item is past the end of the list. */
4487 int scroll = visible_pos - viscount;
4489 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4495 /* Scroll window so item's text is visible as much as possible */
4496 /* Calculation of amount of extra space is taken from EditLabel code */
4498 TEXTMETRICW textMetric;
4499 HDC hdc = GetWindowDC(infoPtr->hwnd);
4501 x = item->textWidth;
4503 GetTextMetricsW(hdc, &textMetric);
4504 ReleaseDC(infoPtr->hwnd, hdc);
4506 x += (textMetric.tmMaxCharWidth * 2);
4507 x = max(x, textMetric.tmMaxCharWidth * 3);
4509 if (item->textOffset < 0)
4510 pos = item->textOffset;
4511 else if (item->textOffset + x > infoPtr->clientWidth)
4513 if (x > infoPtr->clientWidth)
4514 pos = item->textOffset;
4516 pos = item->textOffset + x - infoPtr->clientWidth;
4521 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4524 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4526 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4535 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4536 TREEVIEW_ITEM *newFirstVisible,
4537 BOOL bUpdateScrollPos)
4541 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4543 if (newFirstVisible != NULL)
4545 /* Prevent an empty gap from appearing at the bottom... */
4546 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4547 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4551 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4554 /* ... unless we just don't have enough items. */
4555 if (newFirstVisible == NULL)
4556 newFirstVisible = infoPtr->root->firstChild;
4560 if (infoPtr->firstVisible != newFirstVisible)
4562 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4564 infoPtr->firstVisible = newFirstVisible;
4565 TREEVIEW_Invalidate(infoPtr, NULL);
4569 TREEVIEW_ITEM *item;
4570 int scroll = infoPtr->uItemHeight *
4571 (infoPtr->firstVisible->visibleOrder
4572 - newFirstVisible->visibleOrder);
4574 infoPtr->firstVisible = newFirstVisible;
4576 for (item = infoPtr->root->firstChild; item != NULL;
4577 item = TREEVIEW_GetNextListItem(infoPtr, item))
4579 item->rect.top += scroll;
4580 item->rect.bottom += scroll;
4583 if (bUpdateScrollPos)
4584 SetScrollPos(infoPtr->hwnd, SB_VERT,
4585 newFirstVisible->visibleOrder, TRUE);
4587 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4592 /************************************************************************
4593 * VScroll is always in units of visible items. i.e. we always have a
4594 * visible item aligned to the top of the control. (Unless we have no
4598 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4600 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4601 TREEVIEW_ITEM *newFirstVisible = NULL;
4603 int nScrollCode = LOWORD(wParam);
4605 TRACE("wp %x\n", wParam);
4607 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4610 if (infoPtr->hwndEdit)
4611 SetFocus(infoPtr->hwnd);
4613 if (!oldFirstVisible)
4615 assert(infoPtr->root->firstChild == NULL);
4619 switch (nScrollCode)
4622 newFirstVisible = infoPtr->root->firstChild;
4626 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4630 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4634 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4638 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4639 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4643 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4644 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4648 case SB_THUMBPOSITION:
4649 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4650 infoPtr->root->firstChild,
4651 (LONG)(SHORT)HIWORD(wParam));
4658 if (newFirstVisible != NULL)
4660 if (newFirstVisible != oldFirstVisible)
4661 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4662 nScrollCode != SB_THUMBTRACK);
4663 else if (nScrollCode == SB_THUMBPOSITION)
4664 SetScrollPos(infoPtr->hwnd, SB_VERT,
4665 newFirstVisible->visibleOrder, TRUE);
4672 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4675 int scrollX = infoPtr->scrollX;
4676 int nScrollCode = LOWORD(wParam);
4678 TRACE("wp %x\n", wParam);
4680 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4683 if (infoPtr->hwndEdit)
4684 SetFocus(infoPtr->hwnd);
4686 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4687 /* shall never occur */
4694 switch (nScrollCode)
4697 scrollX -= infoPtr->uItemHeight;
4700 scrollX += infoPtr->uItemHeight;
4703 scrollX -= infoPtr->clientWidth;
4706 scrollX += infoPtr->clientWidth;
4710 case SB_THUMBPOSITION:
4711 scrollX = (int)(SHORT)HIWORD(wParam);
4718 if (scrollX > maxWidth)
4720 else if (scrollX < 0)
4724 if (scrollX != infoPtr->scrollX)
4726 TREEVIEW_ITEM *item;
4727 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4729 for (item = infoPtr->root->firstChild; item != NULL;
4730 item = TREEVIEW_GetNextListItem(infoPtr, item))
4732 item->linesOffset += scroll_pixels;
4733 item->stateOffset += scroll_pixels;
4734 item->imageOffset += scroll_pixels;
4735 item->textOffset += scroll_pixels;
4738 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4739 infoPtr->scrollX = scrollX;
4740 UpdateWindow(infoPtr->hwnd);
4743 if (nScrollCode != SB_THUMBTRACK)
4744 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4750 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4753 UINT pulScrollLines = 3;
4755 if (infoPtr->firstVisible == NULL)
4758 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4760 gcWheelDelta = -(short)HIWORD(wParam);
4761 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4763 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4765 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4766 int maxDy = infoPtr->maxVisibleOrder;
4774 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4779 /* Create/Destroy *******************************************************/
4782 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4784 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
4786 TREEVIEW_INFO *infoPtr;
4789 TRACE("wnd %p, style %lx\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4791 infoPtr = (TREEVIEW_INFO *)Alloc(sizeof(TREEVIEW_INFO));
4793 if (infoPtr == NULL)
4795 ERR("could not allocate info memory!\n");
4799 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4801 infoPtr->hwnd = hwnd;
4802 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4804 infoPtr->uNumItems = 0;
4805 infoPtr->cdmode = 0;
4806 infoPtr->uScrollTime = 300; /* milliseconds */
4807 infoPtr->bRedraw = TRUE;
4809 GetClientRect(hwnd, &rcClient);
4811 /* No scroll bars yet. */
4812 infoPtr->clientWidth = rcClient.right;
4813 infoPtr->clientHeight = rcClient.bottom;
4814 infoPtr->uInternalStatus = 0;
4816 infoPtr->treeWidth = 0;
4817 infoPtr->treeHeight = 0;
4819 infoPtr->uIndent = MINIMUM_INDENT;
4820 infoPtr->selectedItem = 0;
4821 infoPtr->focusedItem = 0;
4822 infoPtr->hotItem = 0;
4823 infoPtr->firstVisible = 0;
4824 infoPtr->maxVisibleOrder = 0;
4825 infoPtr->dropItem = 0;
4826 infoPtr->insertMarkItem = 0;
4827 infoPtr->insertBeforeorAfter = 0;
4830 infoPtr->scrollX = 0;
4832 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4833 infoPtr->clrText = -1; /* use system color */
4834 infoPtr->clrLine = RGB(128, 128, 128);
4835 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4839 infoPtr->hwndEdit = 0;
4840 infoPtr->wpEditOrig = NULL;
4841 infoPtr->bIgnoreEditKillFocus = FALSE;
4842 infoPtr->bLabelChanged = FALSE;
4844 infoPtr->himlNormal = NULL;
4845 infoPtr->himlState = NULL;
4846 infoPtr->normalImageWidth = 0;
4847 infoPtr->normalImageHeight = 0;
4848 infoPtr->stateImageWidth = 0;
4849 infoPtr->stateImageHeight = 0;
4851 infoPtr->items = DPA_Create(16);
4853 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
4854 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
4855 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4856 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
4857 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
4859 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4861 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4862 infoPtr->root->state = TVIS_EXPANDED;
4863 infoPtr->root->iLevel = -1;
4864 infoPtr->root->visibleOrder = -1;
4866 infoPtr->hwndNotify = lpcs->hwndParent;
4868 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4871 infoPtr->hwndToolTip = 0;
4873 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
4875 /* Determine what type of notify should be issued */
4876 /* sets infoPtr->bNtfUnicode */
4877 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
4879 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4880 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4882 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4885 HBITMAP hbm, hbmOld;
4889 infoPtr->himlState =
4890 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4892 hdcScreen = CreateDCW(szDisplayW, NULL, NULL, NULL);
4894 /* Create a coloured bitmap compatible with the screen depth
4895 because checkboxes are not black&white */
4896 hdc = CreateCompatibleDC(hdcScreen);
4897 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4898 hbmOld = SelectObject(hdc, hbm);
4900 rc.left = 0; rc.top = 0;
4901 rc.right = 48; rc.bottom = 16;
4902 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4904 rc.left = 18; rc.top = 2;
4905 rc.right = 30; rc.bottom = 14;
4906 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4907 DFCS_BUTTONCHECK|DFCS_FLAT);
4909 rc.left = 34; rc.right = 46;
4910 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4911 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4913 SelectObject(hdc, hbmOld);
4914 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4915 GetSysColor(COLOR_WINDOW));
4916 TRACE("checkbox index %d\n", nIndex);
4920 DeleteDC(hdcScreen);
4922 infoPtr->stateImageWidth = 16;
4923 infoPtr->stateImageHeight = 16;
4926 /* Make sure actual scrollbar state is consistent with uInternalStatus */
4927 ShowScrollBar(hwnd, SB_VERT, FALSE);
4928 ShowScrollBar(hwnd, SB_HORZ, FALSE);
4935 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4939 TREEVIEW_RemoveTree(infoPtr);
4941 /* tool tip is automatically destroyed: we are its owner */
4943 /* Restore original wndproc */
4944 if (infoPtr->hwndEdit)
4945 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
4946 (DWORD_PTR)infoPtr->wpEditOrig);
4948 /* Deassociate treeview from the window before doing anything drastic. */
4949 SetWindowLongPtrW(infoPtr->hwnd, 0, (DWORD_PTR)NULL);
4951 DeleteObject(infoPtr->hDefaultFont);
4952 DeleteObject(infoPtr->hBoldFont);
4953 DeleteObject(infoPtr->hUnderlineFont);
4959 /* Miscellaneous Messages ***********************************************/
4962 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
4970 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4971 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
4972 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
4973 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
4974 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
4975 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
4976 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
4977 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
4978 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
4982 if (key >= VK_PRIOR && key <= VK_DOWN)
4984 unsigned char code = scroll[key - VK_PRIOR].code;
4986 (((code & (1 << 7)) == (SB_HORZ << 7))
4988 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
4994 /************************************************************************
4997 * VK_UP Move selection to the previous non-hidden item.
4998 * VK_DOWN Move selection to the next non-hidden item.
4999 * VK_HOME Move selection to the first item.
5000 * VK_END Move selection to the last item.
5001 * VK_LEFT If expanded then collapse, otherwise move to parent.
5002 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5004 * VK_SUBTRACT Collapse.
5005 * VK_MULTIPLY Expand all.
5006 * VK_PRIOR Move up GetVisibleCount items.
5007 * VK_NEXT Move down GetVisibleCount items.
5008 * VK_BACK Move to parent.
5009 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5012 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5014 /* If it is non-NULL and different, it will be selected and visible. */
5015 TREEVIEW_ITEM *newSelection = NULL;
5017 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5019 TRACE("%x\n", wParam);
5021 if (prevItem == NULL)
5024 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5025 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5030 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5032 newSelection = infoPtr->root->firstChild;
5036 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5040 newSelection = infoPtr->root->firstChild;
5044 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5048 if (prevItem->state & TVIS_EXPANDED)
5050 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5052 else if (prevItem->parent != infoPtr->root)
5054 newSelection = prevItem->parent;
5059 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5061 if (!(prevItem->state & TVIS_EXPANDED))
5062 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5065 newSelection = prevItem->firstChild;
5072 TREEVIEW_ExpandAll(infoPtr, prevItem);
5076 if (!(prevItem->state & TVIS_EXPANDED))
5077 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5081 if (prevItem->state & TVIS_EXPANDED)
5082 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5087 = TREEVIEW_GetListItem(infoPtr, prevItem,
5088 -TREEVIEW_GetVisibleCount(infoPtr));
5093 = TREEVIEW_GetListItem(infoPtr, prevItem,
5094 TREEVIEW_GetVisibleCount(infoPtr));
5098 newSelection = prevItem->parent;
5099 if (newSelection == infoPtr->root)
5100 newSelection = NULL;
5104 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5105 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5109 if (newSelection && newSelection != prevItem)
5111 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5114 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5122 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5124 if (infoPtr->hotItem)
5126 /* remove hot effect from item */
5127 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5128 infoPtr->hotItem = NULL;
5134 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, WPARAM wParam, LPARAM lParam)
5137 TRACKMOUSEEVENT trackinfo;
5138 TREEVIEW_ITEM * item;
5140 /* fill in the TRACKMOUSEEVENT struct */
5141 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5142 trackinfo.dwFlags = TME_QUERY;
5143 trackinfo.hwndTrack = infoPtr->hwnd;
5144 trackinfo.dwHoverTime = HOVER_DEFAULT;
5146 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5147 _TrackMouseEvent(&trackinfo);
5149 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5150 if(!(trackinfo.dwFlags & TME_LEAVE))
5152 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5154 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5155 /* and can properly deactivate the hot item */
5156 _TrackMouseEvent(&trackinfo);
5159 pt.x = (INT)LOWORD(lParam);
5160 pt.y = (INT)HIWORD(lParam);
5162 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5164 if (item != infoPtr->hotItem)
5166 /* redraw old hot item */
5167 if (infoPtr->hotItem)
5168 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5169 infoPtr->hotItem = item;
5170 /* redraw new hot item */
5171 if (infoPtr->hotItem)
5172 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5179 TREEVIEW_Notify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5181 LPNMHDR lpnmh = (LPNMHDR)lParam;
5183 if (lpnmh->code == PGN_CALCSIZE) {
5184 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5186 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5187 lppgc->iWidth = infoPtr->treeWidth;
5188 TRACE("got PGN_CALCSIZE, returning horz size = %ld, client=%ld\n",
5189 infoPtr->treeWidth, infoPtr->clientWidth);
5192 lppgc->iHeight = infoPtr->treeHeight;
5193 TRACE("got PGN_CALCSIZE, returning vert size = %ld, client=%ld\n",
5194 infoPtr->treeHeight, infoPtr->clientHeight);
5198 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5201 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5205 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5207 if (nCommand != NF_REQUERY) return 0;
5209 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5210 TRACE("format=%d\n", format);
5212 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5214 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5220 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5222 if (wParam == SIZE_RESTORED)
5224 infoPtr->clientWidth = (short)LOWORD(lParam);
5225 infoPtr->clientHeight = (short)HIWORD(lParam);
5227 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5228 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5229 TREEVIEW_UpdateScrollBars(infoPtr);
5233 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
5236 TREEVIEW_Invalidate(infoPtr, NULL);
5241 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5243 TRACE("(%x %lx)\n", wParam, lParam);
5245 if (wParam == GWL_STYLE)
5247 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5249 /* we have to take special care about tooltips */
5250 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5252 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5254 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5259 DestroyWindow(infoPtr->hwndToolTip);
5260 infoPtr->hwndToolTip = 0;
5264 infoPtr->dwStyle = dwNewStyle;
5267 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5268 TREEVIEW_UpdateScrollBars(infoPtr);
5269 TREEVIEW_Invalidate(infoPtr, NULL);
5275 TREEVIEW_SetCursor(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5278 TREEVIEW_ITEM * item;
5281 ScreenToClient(infoPtr->hwnd, &pt);
5283 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5285 /* FIXME: send NM_SETCURSOR */
5287 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5289 SetCursor(infoPtr->hcurHand);
5293 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5297 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5301 if (!infoPtr->selectedItem)
5303 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5307 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5308 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5313 TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
5317 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5318 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5323 static LRESULT WINAPI
5324 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5326 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5328 TRACE("hwnd %p msg %04x wp=%08x lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5330 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5333 if (uMsg == WM_CREATE)
5334 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5341 case TVM_CREATEDRAGIMAGE:
5342 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5344 case TVM_DELETEITEM:
5345 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5347 case TVM_EDITLABELA:
5348 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5350 case TVM_EDITLABELW:
5351 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5353 case TVM_ENDEDITLABELNOW:
5354 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5356 case TVM_ENSUREVISIBLE:
5357 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5360 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5362 case TVM_GETBKCOLOR:
5363 return TREEVIEW_GetBkColor(infoPtr);
5366 return TREEVIEW_GetCount(infoPtr);
5368 case TVM_GETEDITCONTROL:
5369 return TREEVIEW_GetEditControl(infoPtr);
5371 case TVM_GETIMAGELIST:
5372 return TREEVIEW_GetImageList(infoPtr, wParam);
5375 return TREEVIEW_GetIndent(infoPtr);
5377 case TVM_GETINSERTMARKCOLOR:
5378 return TREEVIEW_GetInsertMarkColor(infoPtr);
5380 case TVM_GETISEARCHSTRINGA:
5381 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5384 case TVM_GETISEARCHSTRINGW:
5385 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5389 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5392 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5394 case TVM_GETITEMHEIGHT:
5395 return TREEVIEW_GetItemHeight(infoPtr);
5397 case TVM_GETITEMRECT:
5398 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5400 case TVM_GETITEMSTATE:
5401 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5403 case TVM_GETLINECOLOR:
5404 return TREEVIEW_GetLineColor(infoPtr);
5406 case TVM_GETNEXTITEM:
5407 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5409 case TVM_GETSCROLLTIME:
5410 return TREEVIEW_GetScrollTime(infoPtr);
5412 case TVM_GETTEXTCOLOR:
5413 return TREEVIEW_GetTextColor(infoPtr);
5415 case TVM_GETTOOLTIPS:
5416 return TREEVIEW_GetToolTips(infoPtr);
5418 case TVM_GETUNICODEFORMAT:
5419 return TREEVIEW_GetUnicodeFormat(infoPtr);
5421 case TVM_GETVISIBLECOUNT:
5422 return TREEVIEW_GetVisibleCount(infoPtr);
5425 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5427 case TVM_INSERTITEMA:
5428 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, FALSE);
5430 case TVM_INSERTITEMW:
5431 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, TRUE);
5433 case TVM_SELECTITEM:
5434 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5436 case TVM_SETBKCOLOR:
5437 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5439 case TVM_SETIMAGELIST:
5440 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5443 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5445 case TVM_SETINSERTMARK:
5446 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5448 case TVM_SETINSERTMARKCOLOR:
5449 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5452 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5455 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5457 case TVM_SETLINECOLOR:
5458 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5460 case TVM_SETITEMHEIGHT:
5461 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5463 case TVM_SETSCROLLTIME:
5464 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5466 case TVM_SETTEXTCOLOR:
5467 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5469 case TVM_SETTOOLTIPS:
5470 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5472 case TVM_SETUNICODEFORMAT:
5473 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5475 case TVM_SORTCHILDREN:
5476 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5478 case TVM_SORTCHILDRENCB:
5479 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5482 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5485 return TREEVIEW_Command(infoPtr, wParam, lParam);
5488 return TREEVIEW_Destroy(infoPtr);
5493 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5496 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5499 return TREEVIEW_GetFont(infoPtr);
5502 return TREEVIEW_HScroll(infoPtr, wParam);
5505 return TREEVIEW_KeyDown(infoPtr, wParam);
5508 return TREEVIEW_KillFocus(infoPtr);
5510 case WM_LBUTTONDBLCLK:
5511 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5513 case WM_LBUTTONDOWN:
5514 return TREEVIEW_LButtonDown(infoPtr, lParam);
5516 /* WM_MBUTTONDOWN */
5519 return TREEVIEW_MouseLeave(infoPtr);
5522 if (infoPtr->dwStyle & TVS_TRACKSELECT)
5523 return TREEVIEW_MouseMove(infoPtr, wParam, lParam);
5528 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5530 case WM_NOTIFYFORMAT:
5531 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5534 return TREEVIEW_Paint(infoPtr, wParam);
5536 /* WM_PRINTCLIENT */
5538 case WM_RBUTTONDOWN:
5539 return TREEVIEW_RButtonDown(infoPtr, lParam);
5542 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5545 return TREEVIEW_SetFocus(infoPtr);
5548 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5551 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5554 return TREEVIEW_Size(infoPtr, wParam, lParam);
5556 case WM_STYLECHANGED:
5557 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5559 /* WM_SYSCOLORCHANGE */
5564 return TREEVIEW_HandleTimer(infoPtr, wParam);
5567 return TREEVIEW_VScroll(infoPtr, wParam);
5569 /* WM_WININICHANGE */
5572 if (wParam & (MK_SHIFT | MK_CONTROL))
5574 return TREEVIEW_MouseWheel(infoPtr, wParam);
5577 TRACE("drawItem\n");
5581 /* This mostly catches MFC and Delphi messages. :( */
5582 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5583 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
5585 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5590 /* Class Registration ***************************************************/
5593 TREEVIEW_Register(void)
5599 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5600 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5601 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5602 wndClass.cbClsExtra = 0;
5603 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5605 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5606 wndClass.hbrBackground = 0;
5607 wndClass.lpszClassName = WC_TREEVIEWW;
5609 RegisterClassW(&wndClass);
5614 TREEVIEW_Unregister(void)
5616 UnregisterClassW(WC_TREEVIEWW, NULL);
5620 /* Tree Verification ****************************************************/
5624 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5626 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5627 TREEVIEW_ITEM *item)
5629 assert(infoPtr != NULL);
5630 assert(item != NULL);
5632 /* both NULL, or both non-null */
5633 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5635 assert(item->firstChild != item);
5636 assert(item->lastChild != item);
5638 if (item->firstChild)
5640 assert(item->firstChild->parent == item);
5641 assert(item->firstChild->prevSibling == NULL);
5644 if (item->lastChild)
5646 assert(item->lastChild->parent == item);
5647 assert(item->lastChild->nextSibling == NULL);
5650 assert(item->nextSibling != item);
5651 if (item->nextSibling)
5653 assert(item->nextSibling->parent == item->parent);
5654 assert(item->nextSibling->prevSibling == item);
5657 assert(item->prevSibling != item);
5658 if (item->prevSibling)
5660 assert(item->prevSibling->parent == item->parent);
5661 assert(item->prevSibling->nextSibling == item);
5666 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5668 assert(item != NULL);
5670 assert(item->parent != NULL);
5671 assert(item->parent != item);
5672 assert(item->iLevel == item->parent->iLevel + 1);
5674 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5676 TREEVIEW_VerifyItemCommon(infoPtr, item);
5678 TREEVIEW_VerifyChildren(infoPtr, item);
5682 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5684 TREEVIEW_ITEM *child;
5685 assert(item != NULL);
5687 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5688 TREEVIEW_VerifyItem(infoPtr, child);
5692 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5694 TREEVIEW_ITEM *root = infoPtr->root;
5696 assert(root != NULL);
5697 assert(root->iLevel == -1);
5698 assert(root->parent == NULL);
5699 assert(root->prevSibling == NULL);
5701 TREEVIEW_VerifyItemCommon(infoPtr, root);
5703 TREEVIEW_VerifyChildren(infoPtr, root);
5707 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5709 assert(infoPtr != NULL);
5711 TREEVIEW_VerifyRoot(infoPtr);