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
7 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
9 * Note2: All items always! have valid (allocated) pszText field.
10 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
11 * of size TEXT_CALLBACK_SIZE in DoSetItem.
12 * We use callbackMask to keep track of fields to be updated.
15 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
16 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
18 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_NOSCROLL,
19 * TVS_RTLREADING, TVS_TRACKSELECT
21 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
23 * Make the insertion mark look right.
24 * Scroll (instead of repaint) as much as possible.
32 #include "wine/winestring.h"
35 #include "debugtools.h"
37 /* internal structures */
39 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
50 int iIntegral; /* item height multiplier (1 is normal) */
51 int iLevel; /* indentation level:0=root level */
52 HTREEITEM parent; /* handle to parent or 0 if at root*/
53 HTREEITEM firstChild; /* handle to first child or 0 if no child*/
55 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
56 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
62 LONG textWidth; /* horizontal text extent for pszText */
63 LONG visibleOrder; /* visible ordering, 0 is first visible item */
67 typedef struct tagTREEVIEW_INFO
74 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
75 INT cdmode; /* last custom draw setting */
76 UINT uScrollTime; /* max. time for scrolling in milliseconds*/
77 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
79 UINT uItemHeight; /* item height */
82 LONG clientWidth; /* width of control window */
83 LONG clientHeight; /* height of control window */
85 LONG treeWidth; /* width of visible tree items */
86 LONG treeHeight; /* height of visible tree items */
88 UINT uIndent; /* indentation in pixels */
89 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
90 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
91 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
93 HTREEITEM firstVisible; /* handle to first visible item */
95 HTREEITEM dropItem; /* handle to item selected by drag cursor */
96 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
97 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
98 HIMAGELIST dragList; /* Bitmap of dragged item */
103 COLORREF clrInsertMark;
109 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
110 BOOL bIgnoreEditKillFocus;
113 HIMAGELIST himlNormal;
114 int normalImageHeight;
115 int normalImageWidth;
116 HIMAGELIST himlState;
117 int stateImageHeight;
124 /* bitflags for infoPtr->uInternalStatus */
126 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
127 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
128 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
129 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
130 #define TV_RDRAG 0x10 /* dito Rbutton */
131 #define TV_RDRAGGING 0x20
133 /* bitflags for infoPtr->timer */
135 #define TV_EDIT_TIMER 2
136 #define TV_EDIT_TIMER_SET 2
139 VOID TREEVIEW_Register (VOID);
140 VOID TREEVIEW_Unregister (VOID);
143 DEFAULT_DEBUG_CHANNEL(treeview);
146 #define TEXT_CALLBACK_SIZE 260
148 #define TREEVIEW_LEFT_MARGIN 8
150 #define MINIMUM_INDENT 19
152 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
154 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
155 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
156 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
159 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
162 static VOID TREEVIEW_QueueRefresh(TREEVIEW_INFO *);
163 static VOID TREEVIEW_QueueItemRefresh(TREEVIEW_INFO *, TREEVIEW_ITEM *);
165 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
166 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
167 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
168 static LRESULT TREEVIEW_RButtonUp(TREEVIEW_INFO *, LPPOINT);
169 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
170 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
171 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
174 /* Random Utilities *****************************************************/
178 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
183 /* The definition is at the end of the file. */
184 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
187 /* Returns the treeview private data if hwnd is a treeview.
188 * Otherwise returns an undefined value. */
189 static TREEVIEW_INFO *
190 TREEVIEW_GetInfoPtr(HWND hwnd)
192 return (TREEVIEW_INFO *)GetWindowLongA(hwnd, 0);
195 /* Don't call this. Nothing wants an item index. */
197 TREEVIEW_GetItemIndex(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
199 assert(infoPtr != NULL);
201 return DPA_GetPtrIndex(infoPtr->items, handle);
204 /***************************************************************************
205 * This method checks that handle is an item for this tree.
208 TREEVIEW_ValidItem(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
210 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
212 TRACE("invalid item %p\n", handle);
220 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
224 GetObjectA(hOrigFont, sizeof(font), &font);
225 font.lfWeight = FW_BOLD;
226 return CreateFontIndirectA(&font);
230 TREEVIEW_FontForItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
232 return (item->state & TVIS_BOLD) ? infoPtr->hBoldFont : infoPtr->hFont;
235 /* for trace/debugging purposes only */
237 TREEVIEW_ItemName(TREEVIEW_ITEM *item)
239 if (item == NULL) return "<null item>";
240 if (item->pszText == LPSTR_TEXTCALLBACKA) return "<callback>";
241 if (item->pszText == NULL) return "<null>";
242 return item->pszText;
245 /* An item is not a child of itself. */
247 TREEVIEW_IsChildOf(TREEVIEW_ITEM *parent, TREEVIEW_ITEM *child)
251 child = child->parent;
252 if (child == parent) return TRUE;
253 } while (child != NULL);
259 /* Tree Traversal *******************************************************/
261 /***************************************************************************
262 * This method returns the last expanded sibling or child child item
265 static TREEVIEW_ITEM *
266 TREEVIEW_GetLastListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
271 while (wineItem->lastChild)
273 if (wineItem->state & TVIS_EXPANDED)
274 wineItem = wineItem->lastChild;
279 if (wineItem == infoPtr->root)
285 /***************************************************************************
286 * This method returns the previous non-hidden item in the list not
287 * considering the tree hierarchy.
289 static TREEVIEW_ITEM *
290 TREEVIEW_GetPrevListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
292 if (tvItem->prevSibling)
294 /* This item has a prevSibling, get the last item in the sibling's tree. */
295 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
297 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
298 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
304 /* this item does not have a prevSibling, get the parent */
305 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
310 /***************************************************************************
311 * This method returns the next physical item in the treeview not
312 * considering the tree hierarchy.
314 static TREEVIEW_ITEM *
315 TREEVIEW_GetNextListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
317 assert(tvItem != NULL);
320 * If this item has children and is expanded, return the first child
322 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
324 return tvItem->firstChild;
329 * try to get the sibling
331 if (tvItem->nextSibling)
332 return tvItem->nextSibling;
335 * Otherwise, get the parent's sibling.
337 while (tvItem->parent)
339 tvItem = tvItem->parent;
341 if (tvItem->nextSibling)
342 return tvItem->nextSibling;
348 /***************************************************************************
349 * This method returns the nth item starting at the given item. It returns
350 * the last item (or first) we we run out of items.
352 * Will scroll backward if count is <0.
353 * forward if count is >0.
355 static TREEVIEW_ITEM *
356 TREEVIEW_GetListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
359 TREEVIEW_ITEM *(*next_item)(TREEVIEW_INFO *, TREEVIEW_ITEM *);
360 TREEVIEW_ITEM *previousItem;
362 assert(wineItem != NULL);
366 next_item = TREEVIEW_GetNextListItem;
371 next_item = TREEVIEW_GetPrevListItem;
378 previousItem = wineItem;
379 wineItem = next_item(infoPtr, wineItem);
381 } while (--count && wineItem != NULL);
384 return wineItem ? wineItem : previousItem;
387 /* Notifications ************************************************************/
390 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO *infoPtr, UINT code)
393 HWND hwnd = infoPtr->hwnd;
396 nmhdr.hwndFrom = hwnd;
397 nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
400 return (BOOL)SendMessageA(GetParent(hwnd), WM_NOTIFY,
401 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
405 TREEVIEW_TVItemFromItem(UINT mask, TVITEMA *tvItem, TREEVIEW_ITEM *item)
408 tvItem->hItem = item;
409 tvItem->state = item->state;
410 tvItem->stateMask = 0;
411 tvItem->iImage = item->iImage;
412 tvItem->pszText = item->pszText;
413 tvItem->cchTextMax = item->cchTextMax;
414 tvItem->iImage = item->iImage;
415 tvItem->iSelectedImage = item->iSelectedImage;
416 tvItem->cChildren = item->cChildren;
417 tvItem->lParam = item->lParam;
421 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO *infoPtr, UINT code, UINT action,
422 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
424 HWND hwnd = infoPtr->hwnd;
427 TRACE("code:%x action:%x olditem:%p newitem:%p\n",
428 code, action, oldItem, newItem);
430 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWA));
432 nmhdr.hdr.hwndFrom = hwnd;
433 nmhdr.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
434 nmhdr.hdr.code = code;
435 nmhdr.action = action;
438 TREEVIEW_TVItemFromItem(mask, &nmhdr.itemOld, oldItem);
441 TREEVIEW_TVItemFromItem(mask, &nmhdr.itemNew, newItem);
446 return (BOOL)SendMessageA(GetParent(hwnd), WM_NOTIFY,
447 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
452 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO *infoPtr, UINT code,
453 HTREEITEM dragItem, POINT pt)
455 HWND hwnd = infoPtr->hwnd;
458 TRACE("code:%x dragitem:%p\n", code, dragItem);
460 nmhdr.hdr.hwndFrom = hwnd;
461 nmhdr.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
462 nmhdr.hdr.code = code;
464 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
465 nmhdr.itemNew.hItem = dragItem;
466 nmhdr.itemNew.state = dragItem->state;
467 nmhdr.itemNew.lParam = dragItem->lParam;
469 nmhdr.ptDrag.x = pt.x;
470 nmhdr.ptDrag.y = pt.y;
472 return (BOOL)SendMessageA(GetParent(hwnd), WM_NOTIFY,
473 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
479 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
482 HWND hwnd = infoPtr->hwnd;
483 NMTVCUSTOMDRAW nmcdhdr;
486 TRACE("drawstage:%lx hdc:%x\n", dwDrawStage, hdc);
488 nmcd = &nmcdhdr.nmcd;
489 nmcd->hdr.hwndFrom = hwnd;
490 nmcd->hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
491 nmcd->hdr.code = NM_CUSTOMDRAW;
492 nmcd->dwDrawStage = dwDrawStage;
495 nmcd->dwItemSpec = 0;
496 nmcd->uItemState = 0;
497 nmcd->lItemlParam = 0;
498 nmcdhdr.clrText = infoPtr->clrText;
499 nmcdhdr.clrTextBk = infoPtr->clrBk;
502 return (BOOL)SendMessageA(GetParent(hwnd), WM_NOTIFY,
503 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
509 /* FIXME: need to find out when the flags in uItemState need to be set */
512 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO *infoPtr, HDC hdc,
513 TREEVIEW_ITEM *wineItem, UINT uItemDrawState)
515 HWND hwnd = infoPtr->hwnd;
516 NMTVCUSTOMDRAW nmcdhdr;
518 DWORD dwDrawStage, dwItemSpec;
522 dwDrawStage = CDDS_ITEM | uItemDrawState;
523 dwItemSpec = (DWORD)wineItem;
525 if (wineItem->state & TVIS_SELECTED)
526 uItemState |= CDIS_SELECTED;
527 if (wineItem == infoPtr->selectedItem)
528 uItemState |= CDIS_FOCUS;
529 if (wineItem == infoPtr->hotItem)
530 uItemState |= CDIS_HOT;
532 nmcd = &nmcdhdr.nmcd;
533 nmcd->hdr.hwndFrom = hwnd;
534 nmcd->hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
535 nmcd->hdr.code = NM_CUSTOMDRAW;
536 nmcd->dwDrawStage = dwDrawStage;
538 nmcd->rc = wineItem->rect;
539 nmcd->dwItemSpec = dwItemSpec;
540 nmcd->uItemState = uItemState;
541 nmcd->lItemlParam = wineItem->lParam;
542 nmcdhdr.clrText = infoPtr->clrText;
543 nmcdhdr.clrTextBk = infoPtr->clrBk;
544 nmcdhdr.iLevel = wineItem->iLevel;
546 TRACE("drawstage:%lx hdc:%x item:%lx, itemstate:%x, lItemlParam:%lx\n",
547 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
548 nmcd->uItemState, nmcd->lItemlParam);
550 retval = SendMessageA(GetParent(hwnd), WM_NOTIFY,
551 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
554 infoPtr->clrText = nmcdhdr.clrText;
555 infoPtr->clrBk = nmcdhdr.clrTextBk;
560 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
562 HWND hwnd = infoPtr->hwnd;
565 tvdi.hdr.hwndFrom = hwnd;
566 tvdi.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
567 tvdi.hdr.code = TVN_BEGINLABELEDITA;
569 tvdi.item.mask = TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT;
570 tvdi.item.hItem = editItem;
571 tvdi.item.state = editItem->state;
572 tvdi.item.lParam = editItem->lParam;
573 tvdi.item.pszText = editItem->pszText;
574 tvdi.item.cchTextMax = editItem->cchTextMax;
576 return SendMessageA(GetParent(hwnd), WM_NOTIFY, tvdi.hdr.idFrom,
581 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
584 NMTVDISPINFOA callback;
585 HWND hwnd = infoPtr->hwnd;
587 mask &= wineItem->callbackMask;
589 if (mask == 0) return;
591 callback.hdr.hwndFrom = hwnd;
592 callback.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
593 callback.hdr.code = TVN_GETDISPINFOA;
595 /* 'state' always contains valid value, as well as 'lParam'.
596 * All other parameters are uninitialized.
598 callback.item.pszText = wineItem->pszText;
599 callback.item.cchTextMax = wineItem->cchTextMax;
600 callback.item.mask = mask;
601 callback.item.hItem = wineItem;
602 callback.item.state = wineItem->state;
603 callback.item.lParam = wineItem->lParam;
605 /* If text is changed we need to recalculate textWidth */
606 if (mask & TVIF_TEXT)
607 wineItem->textWidth = 0;
609 SendMessageA(GetParent(hwnd), WM_NOTIFY, callback.hdr.idFrom, (LPARAM)&callback);
611 /* It may have changed due to a call to SetItem. */
612 mask &= wineItem->callbackMask;
614 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
616 /* Instead of copying text into our buffer user specified its own */
617 int len = max(lstrlenA(callback.item.pszText) + 1, TEXT_CALLBACK_SIZE);
618 LPSTR newText = COMCTL32_ReAlloc(wineItem->pszText, len);
622 wineItem->pszText = newText;
623 strcpy(wineItem->pszText, callback.item.pszText);
624 wineItem->cchTextMax = len;
626 /* If ReAlloc fails we have nothing to do, but keep original text */
629 if (mask & TVIF_IMAGE)
630 wineItem->iImage = callback.item.iImage;
632 if (mask & TVIF_SELECTEDIMAGE)
633 wineItem->iSelectedImage = callback.item.iSelectedImage;
635 if (mask & TVIF_CHILDREN)
636 wineItem->cChildren = callback.item.cChildren;
638 /* These members are now permanently set. */
639 if (callback.item.mask & TVIF_DI_SETITEM)
640 wineItem->callbackMask &= ~callback.item.mask;
643 /***************************************************************************
644 * This function uses cChildren field to decide whether the item has
646 * Note: if this returns TRUE, the child items may not actually exist,
647 * they could be virtual.
649 * Just use wineItem->firstChild to check for physical children.
652 TREEVIEW_HasChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
654 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
656 return wineItem->cChildren > 0;
660 /* Item Position ********************************************************/
662 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
664 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO *infoPtr,
667 /* Same effect, different optimisation. */
669 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
670 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
672 BOOL lar = ((infoPtr->dwStyle
673 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
677 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
679 item->stateOffset = item->linesOffset + infoPtr->uIndent;
680 item->imageOffset = item->stateOffset
681 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
682 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
686 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
692 /* DRAW's OM docker creates items like this */
693 if (item->pszText == NULL)
699 if (item->textWidth != 0 && !(item->callbackMask & TVIF_TEXT))
708 hdc = GetDC(infoPtr->hwnd);
709 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
712 GetTextExtentPoint32A(hdc, item->pszText, strlen(item->pszText), &sz);
713 item->textWidth = sz.cx;
717 SelectObject(hdc, hOldFont);
723 TREEVIEW_ComputeItemRect(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
725 item->rect.top = infoPtr->uItemHeight *
726 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
728 item->rect.bottom = item->rect.top
729 + infoPtr->uItemHeight * item->iIntegral;
732 item->rect.right = infoPtr->clientWidth;
735 /* We know that only items after start need their order updated. */
737 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
744 start = infoPtr->root->firstChild;
748 order = start->visibleOrder;
750 for (item = start; item != NULL;
751 item = TREEVIEW_GetNextListItem(infoPtr, item))
753 item->visibleOrder = order;
754 order += item->iIntegral;
757 infoPtr->maxVisibleOrder = order;
759 for (item = start; item != NULL;
760 item = TREEVIEW_GetNextListItem(infoPtr, item))
762 TREEVIEW_ComputeItemRect(infoPtr, item);
767 /* Update metrics of all items in selected subtree.
768 * root must be expanded
771 TREEVIEW_UpdateSubTree(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
773 TREEVIEW_ITEM *sibling;
777 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
780 root->state &= ~TVIS_EXPANDED;
781 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
782 root->state |= TVIS_EXPANDED;
784 hdc = GetDC(infoPtr->hwnd);
785 hOldFont = SelectObject(hdc, infoPtr->hFont);
787 for (; root != sibling;
788 root = TREEVIEW_GetNextListItem(infoPtr, root))
790 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
792 if (root->callbackMask & TVIF_TEXT)
793 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
795 if (root->textWidth == 0)
797 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
798 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
802 SelectObject(hdc, hOldFont);
803 ReleaseDC(infoPtr->hwnd, hdc);
806 /* Item Allocation **********************************************************/
808 static TREEVIEW_ITEM *
809 TREEVIEW_AllocateItem(TREEVIEW_INFO *infoPtr)
811 TREEVIEW_ITEM *newItem = COMCTL32_Alloc(sizeof(TREEVIEW_ITEM));
816 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
818 COMCTL32_Free(newItem);
825 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
826 * free item->pszText. */
828 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
830 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
835 /* Item Insertion *******************************************************/
837 /***************************************************************************
838 * This method inserts newItem before sibling as a child of parent.
839 * sibling can be NULL, but only if parent has no children.
842 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
843 TREEVIEW_ITEM *parent)
845 assert(newItem != NULL);
846 assert(parent != NULL);
850 assert(sibling->parent == parent);
852 if (sibling->prevSibling != NULL)
853 sibling->prevSibling->nextSibling = newItem;
855 newItem->prevSibling = sibling->prevSibling;
856 sibling->prevSibling = newItem;
859 newItem->prevSibling = NULL;
861 newItem->nextSibling = sibling;
863 if (parent->firstChild == sibling)
864 parent->firstChild = newItem;
866 if (parent->lastChild == NULL)
867 parent->lastChild = newItem;
870 /***************************************************************************
871 * This method inserts newItem after sibling as a child of parent.
872 * sibling can be NULL, but only if parent has no children.
875 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
876 TREEVIEW_ITEM *parent)
878 assert(newItem != NULL);
879 assert(parent != NULL);
883 assert(sibling->parent == parent);
885 if (sibling->nextSibling != NULL)
886 sibling->nextSibling->prevSibling = newItem;
888 newItem->nextSibling = sibling->nextSibling;
889 sibling->nextSibling = newItem;
892 newItem->nextSibling = NULL;
894 newItem->prevSibling = sibling;
896 if (parent->lastChild == sibling)
897 parent->lastChild = newItem;
899 if (parent->firstChild == NULL)
900 parent->firstChild = newItem;
904 TREEVIEW_DoSetItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
905 const TVITEMEXA *tvItem)
907 UINT callbackClear = 0;
908 UINT callbackSet = 0;
910 /* Do this first in case it fails. */
911 if (tvItem->mask & TVIF_TEXT)
913 if (tvItem->pszText != LPSTR_TEXTCALLBACKA)
915 int len = lstrlenA(tvItem->pszText) + 1;
916 LPSTR newText = COMCTL32_ReAlloc(wineItem->pszText, len);
918 if (newText == NULL) return FALSE;
920 callbackClear |= TVIF_TEXT;
922 wineItem->pszText = newText;
923 wineItem->cchTextMax = len;
924 lstrcpynA(wineItem->pszText, tvItem->pszText, len);
928 callbackSet |= TVIF_TEXT;
930 wineItem->pszText = COMCTL32_ReAlloc(wineItem->pszText,
932 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
936 if (tvItem->mask & TVIF_CHILDREN)
938 wineItem->cChildren = tvItem->cChildren;
940 if (wineItem->cChildren == I_CHILDRENCALLBACK)
941 callbackSet |= TVIF_CHILDREN;
943 callbackClear |= TVIF_CHILDREN;
946 if (tvItem->mask & TVIF_IMAGE)
948 wineItem->iImage = tvItem->iImage;
950 if (wineItem->iImage == I_IMAGECALLBACK)
951 callbackSet |= TVIF_IMAGE;
953 callbackClear |= TVIF_IMAGE;
956 if (tvItem->mask & TVIF_SELECTEDIMAGE)
958 wineItem->iSelectedImage = tvItem->iSelectedImage;
960 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
961 callbackSet |= TVIF_SELECTEDIMAGE;
963 callbackClear |= TVIF_SELECTEDIMAGE;
966 if (tvItem->mask & TVIF_PARAM)
967 wineItem->lParam = tvItem->lParam;
969 /* If the application sets TVIF_INTEGRAL without
970 * supplying a TVITEMEX structure, it's toast. */
971 if (tvItem->mask & TVIF_INTEGRAL)
972 wineItem->iIntegral = tvItem->iIntegral;
974 if (tvItem->mask & TVIF_STATE)
976 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
978 wineItem->state &= ~tvItem->stateMask;
979 wineItem->state |= (tvItem->state & tvItem->stateMask);
982 wineItem->callbackMask |= callbackSet;
983 wineItem->callbackMask &= ~callbackClear;
988 /* Note that the new item is pre-zeroed. */
990 TREEVIEW_InsertItemA(TREEVIEW_INFO *infoPtr, LPARAM lParam)
992 const TVINSERTSTRUCTA *ptdi = (LPTVINSERTSTRUCTA) lParam;
993 const TVITEMEXA *tvItem = &ptdi->DUMMYUNIONNAME.itemex;
994 HTREEITEM insertAfter;
995 TREEVIEW_ITEM *newItem, *parentItem;
996 BOOL bTextUpdated = FALSE;
998 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1000 parentItem = infoPtr->root;
1004 parentItem = ptdi->hParent;
1006 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1008 WARN("invalid parent %p\n", parentItem);
1009 return (LRESULT)(HTREEITEM)NULL;
1013 insertAfter = ptdi->hInsertAfter;
1015 /* Validate this now for convenience. */
1016 switch ((DWORD)insertAfter)
1018 case (DWORD)TVI_FIRST:
1019 case (DWORD)TVI_LAST:
1020 case (DWORD)TVI_SORT:
1024 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1025 insertAfter->parent != parentItem)
1027 WARN("invalid insert after %p\n", insertAfter);
1028 insertAfter = TVI_LAST;
1032 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1033 (tvItem->mask & TVIF_TEXT)
1034 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKA) ? "<callback>"
1038 newItem = TREEVIEW_AllocateItem(infoPtr);
1039 if (newItem == NULL)
1040 return (LRESULT)(HTREEITEM)NULL;
1042 newItem->parent = parentItem;
1043 newItem->iIntegral = 1;
1045 if (!TREEVIEW_DoSetItem(infoPtr, newItem, tvItem))
1046 return (LRESULT)(HTREEITEM)NULL;
1048 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1050 infoPtr->uNumItems++;
1052 switch ((DWORD)insertAfter)
1054 case (DWORD)TVI_FIRST:
1055 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1056 if (infoPtr->firstVisible == parentItem->firstChild)
1057 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1060 case (DWORD)TVI_LAST:
1061 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1064 /* hInsertAfter names a specific item we want to insert after */
1066 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1069 case (DWORD)TVI_SORT:
1071 TREEVIEW_ITEM *aChild;
1072 TREEVIEW_ITEM *previousChild = NULL;
1073 BOOL bItemInserted = FALSE;
1075 aChild = parentItem->firstChild;
1077 bTextUpdated = TRUE;
1078 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1080 /* Iterate the parent children to see where we fit in */
1081 while (aChild != NULL)
1085 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1086 comp = lstrcmpA(newItem->pszText, aChild->pszText);
1088 if (comp < 0) /* we are smaller than the current one */
1090 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1091 bItemInserted = TRUE;
1094 else if (comp > 0) /* we are bigger than the current one */
1096 previousChild = aChild;
1098 /* This will help us to exit if there is no more sibling */
1099 aChild = (aChild->nextSibling == 0)
1101 : aChild->nextSibling;
1103 /* Look at the next item */
1109 * An item with this name is already existing, therefore,
1110 * we add after the one we found
1112 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1113 bItemInserted = TRUE;
1119 * we reach the end of the child list and the item as not
1120 * yet been inserted, therefore, insert it after the last child.
1122 if ((!bItemInserted) && (aChild == NULL))
1123 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1130 TRACE("new item %p; parent %p, mask %x\n", newItem,
1131 newItem->parent, tvItem->mask);
1133 newItem->iLevel = newItem->parent->iLevel + 1;
1135 if (newItem->parent->cChildren == 0)
1136 newItem->parent->cChildren = 1;
1138 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1140 if (STATEIMAGEINDEX(newItem->state) == 0)
1141 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1144 if (infoPtr->firstVisible == NULL)
1145 infoPtr->firstVisible = newItem;
1147 TREEVIEW_VerifyTree(infoPtr);
1149 if (parentItem == infoPtr->root ||
1150 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1152 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1154 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1155 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1158 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1160 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1161 TREEVIEW_UpdateScrollBars(infoPtr);
1162 TREEVIEW_QueueRefresh(infoPtr);
1166 newItem->visibleOrder = -1;
1168 /* refresh treeview if newItem is the first item inserted under parentItem */
1169 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1171 /* parent got '+' - update it */
1172 TREEVIEW_QueueItemRefresh(infoPtr, parentItem);
1176 return (LRESULT)newItem;
1181 TREEVIEW_InsertItemW(TREEVIEW_INFO *infoPtr, LPARAM lParam)
1183 TVINSERTSTRUCTW *tvisW;
1184 TVINSERTSTRUCTA tvisA;
1187 tvisW = (LPTVINSERTSTRUCTW) lParam;
1189 tvisA.hParent = tvisW->hParent;
1190 tvisA.hInsertAfter = tvisW->hInsertAfter;
1192 tvisA.DUMMYUNIONNAME.item.mask = tvisW->DUMMYUNIONNAME.item.mask;
1193 tvisA.DUMMYUNIONNAME.item.hItem = tvisW->DUMMYUNIONNAME.item.hItem;
1194 tvisA.DUMMYUNIONNAME.item.state = tvisW->DUMMYUNIONNAME.item.state;
1195 tvisA.DUMMYUNIONNAME.item.stateMask = tvisW->DUMMYUNIONNAME.item.stateMask;
1196 tvisA.DUMMYUNIONNAME.item.cchTextMax =
1197 tvisW->DUMMYUNIONNAME.item.cchTextMax;
1199 if (tvisW->DUMMYUNIONNAME.item.pszText)
1201 if (tvisW->DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKW)
1203 int len = lstrlenW(tvisW->DUMMYUNIONNAME.item.pszText) + 1;
1205 tvisA.DUMMYUNIONNAME.item.pszText = COMCTL32_Alloc(len);
1206 lstrcpyWtoA(tvisA.DUMMYUNIONNAME.item.pszText,
1207 tvisW->DUMMYUNIONNAME.item.pszText);
1211 tvisA.DUMMYUNIONNAME.item.pszText = LPSTR_TEXTCALLBACKA;
1212 tvisA.DUMMYUNIONNAME.item.cchTextMax = 0;
1216 tvisA.DUMMYUNIONNAME.item.iImage = tvisW->DUMMYUNIONNAME.item.iImage;
1217 tvisA.DUMMYUNIONNAME.item.iSelectedImage =
1218 tvisW->DUMMYUNIONNAME.item.iSelectedImage;
1219 tvisA.DUMMYUNIONNAME.item.cChildren = tvisW->DUMMYUNIONNAME.item.cChildren;
1220 tvisA.DUMMYUNIONNAME.item.lParam = tvisW->DUMMYUNIONNAME.item.lParam;
1222 lRes = TREEVIEW_InsertItemA(infoPtr, (LPARAM)&tvisA);
1224 if (tvisA.DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKA)
1226 COMCTL32_Free(tvisA.DUMMYUNIONNAME.item.pszText);
1234 /* Item Deletion ************************************************************/
1236 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1239 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1241 TREEVIEW_ITEM *kill = parentItem->firstChild;
1243 while (kill != NULL)
1245 TREEVIEW_ITEM *next = kill->nextSibling;
1247 TREEVIEW_RemoveItem(infoPtr, kill);
1252 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1253 assert(parentItem->firstChild == NULL);
1254 assert(parentItem->lastChild == NULL);
1258 TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1260 TREEVIEW_ITEM *parentItem = item->parent;
1262 assert(item != NULL);
1263 assert(item->parent != NULL); /* i.e. it must not be the root */
1265 if (parentItem->firstChild == item)
1266 parentItem->firstChild = item->nextSibling;
1268 if (parentItem->lastChild == item)
1269 parentItem->lastChild = item->prevSibling;
1271 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1272 && parentItem->cChildren > 0)
1273 parentItem->cChildren = 0;
1275 if (item->prevSibling)
1276 item->prevSibling->nextSibling = item->nextSibling;
1278 if (item->nextSibling)
1279 item->nextSibling->prevSibling = item->prevSibling;
1283 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1285 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1287 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMA,
1288 TVIF_HANDLE | TVIF_PARAM, 0, wineItem, 0);
1290 if (wineItem->firstChild)
1291 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1293 TREEVIEW_UnlinkItem(wineItem);
1295 infoPtr->uNumItems--;
1297 if (wineItem->pszText != LPSTR_TEXTCALLBACKA)
1298 COMCTL32_Free(wineItem->pszText);
1300 TREEVIEW_FreeItem(infoPtr, wineItem);
1304 /* Empty out the tree. */
1306 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1308 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1310 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1314 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1316 TREEVIEW_ITEM *oldSelection = infoPtr->selectedItem;
1317 TREEVIEW_ITEM *newSelection = oldSelection;
1318 TREEVIEW_ITEM *newFirstVisible = NULL;
1319 TREEVIEW_ITEM *parent, *prev = NULL;
1320 BOOL visible = FALSE;
1322 if (wineItem == TVI_ROOT)
1324 TRACE("TVI_ROOT\n");
1325 parent = infoPtr->root;
1326 newSelection = NULL;
1328 TREEVIEW_RemoveTree(infoPtr);
1332 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1335 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1336 parent = wineItem->parent;
1338 if (ISVISIBLE(wineItem))
1340 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1344 if (infoPtr->selectedItem != NULL
1345 && (wineItem == infoPtr->selectedItem
1346 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1348 if (wineItem->nextSibling)
1349 newSelection = wineItem->nextSibling;
1350 else if (wineItem->parent != infoPtr->root)
1351 newSelection = wineItem->parent;
1354 if (infoPtr->firstVisible == wineItem)
1356 if (wineItem->nextSibling)
1357 newFirstVisible = wineItem->nextSibling;
1358 else if (wineItem->prevSibling)
1359 newFirstVisible = wineItem->prevSibling;
1360 else if (wineItem->parent != infoPtr->root)
1361 newFirstVisible = wineItem->parent;
1364 newFirstVisible = infoPtr->firstVisible;
1366 TREEVIEW_RemoveItem(infoPtr, wineItem);
1369 /* Don't change if somebody else already has. */
1370 if (oldSelection == infoPtr->selectedItem)
1372 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1373 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1375 infoPtr->selectedItem = 0;
1378 /* Validate insertMark dropItem.
1379 * hotItem ??? - used for comparision only.
1381 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1382 infoPtr->insertMarkItem = 0;
1384 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1385 infoPtr->dropItem = 0;
1387 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1388 newFirstVisible = infoPtr->root->firstChild;
1390 TREEVIEW_VerifyTree(infoPtr);
1395 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1396 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1397 TREEVIEW_UpdateScrollBars(infoPtr);
1398 TREEVIEW_QueueRefresh(infoPtr);
1400 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1402 /* parent lost '+/-' - update it */
1403 TREEVIEW_QueueItemRefresh(infoPtr, parent);
1410 /* Get/Set Messages *********************************************************/
1412 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1415 infoPtr->bRedraw = TRUE;
1417 infoPtr->bRedraw = FALSE;
1423 TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1426 return infoPtr->uIndent;
1430 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1434 if (newIndent < MINIMUM_INDENT)
1435 newIndent = MINIMUM_INDENT;
1437 if (infoPtr->uIndent != newIndent)
1439 infoPtr->uIndent = newIndent;
1440 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1441 TREEVIEW_UpdateScrollBars(infoPtr);
1442 TREEVIEW_QueueRefresh(infoPtr);
1450 TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1453 return infoPtr->hwndToolTip;
1457 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1462 prevToolTip = infoPtr->hwndToolTip;
1463 infoPtr->hwndToolTip = hwndTT;
1470 TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1472 return infoPtr->uScrollTime;
1476 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1478 UINT uOldScrollTime = infoPtr->uScrollTime;
1480 infoPtr->uScrollTime = min(uScrollTime, 100);
1482 return uOldScrollTime;
1487 TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1493 case (WPARAM)TVSIL_NORMAL:
1494 return (LRESULT)infoPtr->himlNormal;
1496 case (WPARAM)TVSIL_STATE:
1497 return (LRESULT)infoPtr->himlState;
1505 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1507 HIMAGELIST himlOld = 0;
1508 int oldWidth = infoPtr->normalImageWidth;
1509 int oldHeight = infoPtr->normalImageHeight;
1512 TRACE("%x,%p\n", wParam, himlNew);
1516 case (WPARAM)TVSIL_NORMAL:
1517 himlOld = infoPtr->himlNormal;
1518 infoPtr->himlNormal = himlNew;
1520 if (himlNew != NULL)
1521 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1522 &infoPtr->normalImageHeight);
1525 infoPtr->normalImageWidth = 0;
1526 infoPtr->normalImageHeight = 0;
1531 case (WPARAM)TVSIL_STATE:
1532 himlOld = infoPtr->himlState;
1533 infoPtr->himlState = himlNew;
1535 if (himlNew != NULL)
1536 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1537 &infoPtr->stateImageHeight);
1540 infoPtr->stateImageWidth = 0;
1541 infoPtr->stateImageHeight = 0;
1547 if (oldWidth != infoPtr->normalImageWidth ||
1548 oldHeight != infoPtr->normalImageHeight)
1550 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1551 TREEVIEW_UpdateScrollBars(infoPtr);
1554 TREEVIEW_QueueRefresh(infoPtr);
1556 return (LRESULT)himlOld;
1559 /* Compute the natural height (based on the font size) for items. */
1561 TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1565 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1567 GetTextMetricsA(hdc, &tm);
1569 SelectObject(hdc, hOldFont);
1572 /* The 16 is a hack because our fonts are tiny. */
1573 return max(16, tm.tmHeight + tm.tmExternalLeading);
1577 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1579 INT prevHeight = infoPtr->uItemHeight;
1581 TRACE("%d \n", newHeight);
1582 if (newHeight == -1)
1584 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1585 infoPtr->bHeightSet = FALSE;
1589 infoPtr->uItemHeight = newHeight;
1590 infoPtr->bHeightSet = TRUE;
1593 /* Round down, unless we support odd ("non even") heights. */
1594 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1595 infoPtr->uItemHeight &= ~1;
1597 if (infoPtr->uItemHeight != prevHeight)
1599 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1600 TREEVIEW_UpdateScrollBars(infoPtr);
1601 TREEVIEW_QueueRefresh(infoPtr);
1608 TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1611 return infoPtr->uItemHeight;
1616 TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1618 TRACE("%x\n", infoPtr->hFont);
1619 return infoPtr->hFont;
1624 TREEVIEW_ResetTextWidth(LPVOID pItem, DWORD unused)
1628 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1634 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1636 UINT uHeight = infoPtr->uItemHeight;
1638 TRACE("%x %i\n", hFont, bRedraw);
1640 infoPtr->hFont = hFont ? hFont : GetStockObject(SYSTEM_FONT);
1642 DeleteObject(infoPtr->hBoldFont);
1643 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1645 if (!infoPtr->bHeightSet)
1646 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1648 if (uHeight != infoPtr->uItemHeight)
1649 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1651 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1653 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1654 TREEVIEW_UpdateScrollBars(infoPtr);
1657 TREEVIEW_QueueRefresh(infoPtr);
1664 TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1667 return (LRESULT)infoPtr->clrLine;
1671 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1673 COLORREF prevColor = infoPtr->clrLine;
1676 infoPtr->clrLine = color;
1677 return (LRESULT)prevColor;
1682 TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1685 return (LRESULT)infoPtr->clrText;
1689 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1691 COLORREF prevColor = infoPtr->clrText;
1694 infoPtr->clrText = color;
1696 if (infoPtr->clrText != prevColor)
1697 TREEVIEW_QueueRefresh(infoPtr);
1699 return (LRESULT)prevColor;
1704 TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1707 return (LRESULT)infoPtr->clrBk;
1711 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1713 COLORREF prevColor = infoPtr->clrBk;
1716 infoPtr->clrBk = newColor;
1718 if (newColor != prevColor)
1719 TREEVIEW_QueueRefresh(infoPtr);
1721 return (LRESULT)prevColor;
1726 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1729 return (LRESULT)infoPtr->clrInsertMark;
1733 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1735 COLORREF prevColor = infoPtr->clrInsertMark;
1737 TRACE("%lx\n", color);
1738 infoPtr->clrInsertMark = color;
1740 return (LRESULT)prevColor;
1745 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1747 TRACE("%d %p\n", wParam, item);
1749 if (!TREEVIEW_ValidItem(infoPtr, item))
1752 infoPtr->insertBeforeorAfter = wParam;
1753 infoPtr->insertMarkItem = item;
1755 TREEVIEW_QueueRefresh(infoPtr);
1761 /************************************************************************
1762 * Some serious braindamage here. lParam is a pointer to both the
1763 * input HTREEITEM and the output RECT.
1766 TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1768 TREEVIEW_ITEM *wineItem;
1769 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1773 * validate parameters
1779 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
1783 * If wParam is TRUE return the text size otherwise return
1784 * the whole item size
1788 /* Windows does not send TVN_GETDISPINFO here. */
1790 lpRect->top = wineItem->rect.top;
1791 lpRect->bottom = wineItem->rect.bottom;
1793 lpRect->left = wineItem->textOffset;
1794 lpRect->right = wineItem->textOffset + wineItem->textWidth;
1798 *lpRect = wineItem->rect;
1801 TRACE("%s [L:%d R:%d T:%d B:%d]\n", fTextRect ? "text" : "item",
1802 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
1807 static inline LRESULT
1808 TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
1810 /* Suprise! This does not take integral height into account. */
1811 return infoPtr->clientHeight / infoPtr->uItemHeight;
1816 TREEVIEW_GetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1818 TREEVIEW_ITEM *wineItem;
1820 wineItem = tvItem->hItem;
1821 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1824 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
1826 if (tvItem->mask & TVIF_CHILDREN)
1827 tvItem->cChildren = wineItem->cChildren;
1829 if (tvItem->mask & TVIF_HANDLE)
1830 tvItem->hItem = wineItem;
1832 if (tvItem->mask & TVIF_IMAGE)
1833 tvItem->iImage = wineItem->iImage;
1835 if (tvItem->mask & TVIF_INTEGRAL)
1836 tvItem->iIntegral = wineItem->iIntegral;
1838 /* undocumented: windows ignores TVIF_PARAM and
1839 * * always sets lParam
1841 tvItem->lParam = wineItem->lParam;
1843 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1844 tvItem->iSelectedImage = wineItem->iSelectedImage;
1846 if (tvItem->mask & TVIF_STATE)
1847 tvItem->state = wineItem->state & tvItem->stateMask;
1849 if (tvItem->mask & TVIF_TEXT)
1850 lstrcpynA(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
1852 TRACE("item <%p>, txt %p, img %p, mask %x\n",
1853 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
1858 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
1859 * which is wrong. */
1861 TREEVIEW_SetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1863 TREEVIEW_ITEM *wineItem;
1864 TREEVIEW_ITEM originalItem;
1866 wineItem = tvItem->hItem;
1868 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
1871 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1874 if (!TREEVIEW_DoSetItem(infoPtr, wineItem, tvItem))
1877 /* store the orignal item values */
1878 originalItem = *wineItem;
1880 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
1881 if ((tvItem->mask & TVIF_TEXT
1882 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
1883 && ISVISIBLE(wineItem))
1885 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
1886 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
1889 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
1891 /* The refresh updates everything, but we can't wait until then. */
1892 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
1894 /* if any of the items values changed, redraw the item */
1895 if(memcmp(&originalItem, wineItem, sizeof(TREEVIEW_ITEM)))
1897 if (tvItem->mask & TVIF_INTEGRAL)
1899 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
1900 TREEVIEW_UpdateScrollBars(infoPtr);
1902 TREEVIEW_QueueRefresh(infoPtr);
1906 TREEVIEW_UpdateScrollBars(infoPtr);
1907 TREEVIEW_QueueItemRefresh(infoPtr, wineItem);
1916 TREEVIEW_GetItemW(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1918 TREEVIEW_ITEM *wineItem;
1920 iItem = (INT)tvItem->hItem;
1922 wineItem = tvItem->hItem;
1923 if(!TREEVIEW_ValidItem (infoPtr, wineItem))
1926 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
1928 if (tvItem->mask & TVIF_CHILDREN) {
1929 if (TVIF_CHILDREN==I_CHILDRENCALLBACK)
1930 FIXME("I_CHILDRENCALLBACK not supported\n");
1931 tvItem->cChildren = wineItem->cChildren;
1934 if (tvItem->mask & TVIF_HANDLE) {
1935 tvItem->hItem = wineItem;
1937 if (tvItem->mask & TVIF_IMAGE) {
1938 tvItem->iImage = wineItem->iImage;
1940 if (tvItem->mask & TVIF_INTEGRAL) {
1941 tvItem->iIntegral = wineItem->iIntegral;
1943 /* undocumented: windows ignores TVIF_PARAM and
1944 * always sets lParam */
1945 tvItem->lParam = wineItem->lParam;
1946 if (tvItem->mask & TVIF_SELECTEDIMAGE) {
1947 tvItem->iSelectedImage = wineItem->iSelectedImage;
1949 if (tvItem->mask & TVIF_STATE) {
1950 tvItem->state = wineItem->state & tvItem->stateMask;
1953 if (tvItem->mask & TVIF_TEXT) {
1954 if (wineItem->pszText == LPSTR_TEXTCALLBACKW) {
1955 tvItem->pszText = LPSTR_TEXTCALLBACKW; /* FIXME:send notification? */
1956 ERR(" GetItem called with LPSTR_TEXTCALLBACK\n");
1958 else if (wineItem->pszText) {
1959 lstrcpynAtoW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
1963 wineItem->pszText = NULL;
1964 TRACE("item %d<%p>, txt %p, img %p, action %x\n",
1965 iItem, tvItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
1970 TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
1974 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
1977 return (wineItem->state & mask);
1981 TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
1983 TREEVIEW_ITEM *retval;
1987 /* handle all the global data here */
1990 case TVGN_CHILD: /* Special case: child of 0 is root */
1995 retval = infoPtr->root->firstChild;
1999 retval = infoPtr->selectedItem;
2002 case TVGN_FIRSTVISIBLE:
2003 retval = infoPtr->firstVisible;
2006 case TVGN_DROPHILITE:
2007 retval = infoPtr->dropItem;
2010 case TVGN_LASTVISIBLE:
2011 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2017 TRACE("flags:%x, returns %p\n", which, retval);
2018 return (LRESULT)retval;
2021 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2027 retval = wineItem->nextSibling;
2030 retval = wineItem->prevSibling;
2033 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2036 retval = wineItem->firstChild;
2038 case TVGN_NEXTVISIBLE:
2039 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2041 case TVGN_PREVIOUSVISIBLE:
2042 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2045 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2049 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2050 return (LRESULT)retval;
2055 TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2057 TRACE(" %d\n", infoPtr->uNumItems);
2058 return (LRESULT)infoPtr->uNumItems;
2062 TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2064 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2066 static const unsigned int state_table[] = { 0, 2, 1 };
2070 state = STATEIMAGEINDEX(item->state);
2071 TRACE("state:%x\n", state);
2072 item->state &= ~TVIS_STATEIMAGEMASK;
2075 state = state_table[state];
2077 item->state |= INDEXTOSTATEIMAGEMASK(state);
2079 TRACE("state:%x\n", state);
2080 TREEVIEW_QueueItemRefresh(infoPtr, item);
2085 /* Painting *************************************************************/
2087 /* Draw the lines and expand button for an item. Also draws one section
2088 * of the line from item's parent to item's parent's next sibling. */
2090 TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2092 LONG centerx, centery;
2093 BOOL lar = ((infoPtr->dwStyle
2094 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2097 if (!lar && item->iLevel == 0)
2100 centerx = (item->linesOffset + item->stateOffset) / 2;
2101 centery = (item->rect.top + item->rect.bottom) / 2;
2103 if (infoPtr->dwStyle & TVS_HASLINES)
2105 HPEN hOldPen, hNewPen;
2109 * Get a dotted grey pen
2111 hNewPen = CreatePen(PS_DOT, 0, infoPtr->clrLine);
2112 hOldPen = SelectObject(hdc, hNewPen);
2114 MoveToEx(hdc, item->stateOffset, centery, NULL);
2115 LineTo(hdc, centerx - 1, centery);
2117 if (item->prevSibling || item->parent != infoPtr->root)
2119 MoveToEx(hdc, centerx, item->rect.top, NULL);
2120 LineTo(hdc, centerx, centery);
2123 if (item->nextSibling)
2125 MoveToEx(hdc, centerx, centery, NULL);
2126 LineTo(hdc, centerx, item->rect.bottom + 1);
2129 /* Draw the line from our parent to its next sibling. */
2130 parent = item->parent;
2131 while (parent != infoPtr->root)
2133 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2135 if (parent->nextSibling
2136 /* skip top-levels unless TVS_LINESATROOT */
2137 && parent->stateOffset > parent->linesOffset)
2139 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2140 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2143 parent = parent->parent;
2146 SelectObject(hdc, hOldPen);
2147 DeleteObject(hNewPen);
2151 * Display the (+/-) signs
2154 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2156 if (item->cChildren)
2158 LONG height = item->rect.bottom - item->rect.top;
2159 LONG width = item->stateOffset - item->linesOffset;
2160 LONG rectsize = min(height, width) / 4;
2161 /* plussize = ceil(rectsize * 3/4) */
2162 LONG plussize = (rectsize + 1) * 3 / 4;
2164 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2165 HPEN hOldPen = SelectObject(hdc, hNewPen);
2166 HBRUSH hbr = CreateSolidBrush(infoPtr->clrBk);
2167 HBRUSH hbrOld = SelectObject(hdc, hbr);
2169 Rectangle(hdc, centerx - rectsize, centery - rectsize,
2170 centerx + rectsize + 1, centery + rectsize + 1);
2172 SelectObject(hdc, hbrOld);
2175 SelectObject(hdc, hOldPen);
2176 DeleteObject(hNewPen);
2178 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2179 LineTo(hdc, centerx + plussize, centery);
2181 if (!(item->state & TVIS_EXPANDED))
2183 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2184 LineTo(hdc, centerx, centery + plussize);
2191 TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2197 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2199 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2201 /* The custom draw handler can query the text rectangle,
2203 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2207 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2209 cditem = TREEVIEW_SendCustomDrawItemNotify
2210 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT);
2211 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2213 if (cditem & CDRF_SKIPDEFAULT)
2215 SelectObject(hdc, hOldFont);
2220 if (cditem & CDRF_NEWFONT)
2221 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2223 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2225 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2228 * Display the images associated with this item
2233 /* State images are displayed to the left of the Normal image
2234 * image number is in state; zero should be `display no image'.
2236 imageIndex = STATEIMAGEINDEX(wineItem->state);
2238 if (infoPtr->himlState && imageIndex)
2240 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2241 wineItem->stateOffset,
2242 centery - infoPtr->stateImageHeight / 2,
2246 /* Now, draw the normal image; can be either selected or
2247 * non-selected image.
2250 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage))
2252 /* The item is curently selected */
2253 imageIndex = wineItem->iSelectedImage;
2257 /* The item is not selected */
2258 imageIndex = wineItem->iImage;
2261 if (infoPtr->himlNormal)
2263 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2265 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2266 wineItem->imageOffset,
2267 centery - infoPtr->normalImageHeight / 2,
2268 ILD_NORMAL | ovlIdx);
2274 * Display the text associated with this item
2277 /* Don't paint item's text if it's being edited */
2278 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2280 if (wineItem->pszText)
2282 COLORREF oldTextColor = 0;
2285 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2288 oldBkMode = SetBkMode(hdc, TRANSPARENT);
2290 /* - If item is drop target or it is selected and window is in focus -
2291 * use blue background (COLOR_HIGHLIGHT).
2292 * - If item is selected, window is not in focus, but it has style
2293 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2294 * - Otherwise - don't fill background
2296 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2297 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2298 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2300 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2302 hbrBk = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
2304 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2308 hbrBk = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
2310 if (infoPtr->clrText == -1)
2312 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2314 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2319 if (infoPtr->clrText == -1)
2321 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2323 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2326 rcText.top = wineItem->rect.top;
2327 rcText.bottom = wineItem->rect.bottom;
2328 rcText.left = wineItem->textOffset;
2329 rcText.right = rcText.left + wineItem->textWidth + 4;
2333 FillRect(hdc, &rcText, hbrBk);
2334 DeleteObject(hbrBk);
2337 /* Draw the box arround the selected item */
2338 if ((wineItem == infoPtr->selectedItem) && inFocus)
2340 HPEN hNewPen = CreatePen(PS_DOT, 0,
2341 GetSysColor(COLOR_WINDOWTEXT));
2342 HPEN hOldPen = SelectObject(hdc, hNewPen);
2343 INT rop = SetROP2(hdc, R2_XORPEN);
2346 points[4].x = points[0].x = rcText.left;
2347 points[4].y = points[0].y = rcText.top;
2348 points[1].x = rcText.right - 1;
2349 points[1].y = rcText.top;
2350 points[2].x = rcText.right - 1;
2351 points[2].y = rcText.bottom - 1;
2352 points[3].x = rcText.left;
2353 points[3].y = rcText.bottom - 1;
2355 Polyline(hdc, points, 5);
2358 SelectObject(hdc, hOldPen);
2359 DeleteObject(hNewPen);
2368 lstrlenA(wineItem->pszText),
2370 DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
2372 /* Restore the hdc state */
2373 SetTextColor(hdc, oldTextColor);
2375 if (oldBkMode != TRANSPARENT)
2376 SetBkMode(hdc, oldBkMode);
2380 /* Draw insertion mark if necessary */
2382 if (infoPtr->insertMarkItem)
2383 TRACE("item:%d,mark:%d\n",
2384 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2385 (int)infoPtr->insertMarkItem);
2387 if (wineItem == infoPtr->insertMarkItem)
2389 HPEN hNewPen, hOldPen;
2393 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2394 hOldPen = SelectObject(hdc, hNewPen);
2396 if (infoPtr->insertBeforeorAfter)
2397 offset = wineItem->rect.bottom - 1;
2399 offset = wineItem->rect.top + 1;
2401 left = wineItem->textOffset - 2;
2402 right = wineItem->textOffset + wineItem->textWidth + 2;
2404 MoveToEx(hdc, left, offset - 3, NULL);
2405 LineTo(hdc, left, offset + 4);
2407 MoveToEx(hdc, left, offset, NULL);
2408 LineTo(hdc, right + 1, offset);
2410 MoveToEx(hdc, right, offset + 3, NULL);
2411 LineTo(hdc, right, offset - 4);
2413 SelectObject(hdc, hOldPen);
2414 DeleteObject(hNewPen);
2417 if (cditem & CDRF_NOTIFYPOSTPAINT)
2419 cditem = TREEVIEW_SendCustomDrawItemNotify
2420 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT);
2421 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2424 SelectObject(hdc, hOldFont);
2427 /* Computes treeHeight and treeWidth and updates the scroll bars.
2430 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2432 TREEVIEW_ITEM *wineItem;
2433 HWND hwnd = infoPtr->hwnd;
2437 LONG scrollX = infoPtr->scrollX;
2439 infoPtr->treeWidth = 0;
2440 infoPtr->treeHeight = 0;
2442 /* We iterate through all visible items in order to get the tree height
2444 wineItem = infoPtr->root->firstChild;
2446 while (wineItem != NULL)
2448 if (ISVISIBLE(wineItem))
2450 /* actually we draw text at textOffset + 2 */
2451 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2452 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2454 /* This is scroll-adjusted, but we fix this below. */
2455 infoPtr->treeHeight = wineItem->rect.bottom;
2458 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2461 /* Fix the scroll adjusted treeHeight and treeWidth. */
2462 if (infoPtr->root->firstChild)
2463 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2465 infoPtr->treeWidth += infoPtr->scrollX;
2467 /* Adding one scroll bar may take up enough space that it forces us
2468 * to add the other as well. */
2469 if (infoPtr->treeHeight > infoPtr->clientHeight)
2473 if (infoPtr->treeWidth
2474 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2477 else if (infoPtr->treeWidth > infoPtr->clientWidth)
2480 if (!vert && horz && infoPtr->treeHeight
2481 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2484 si.cbSize = sizeof(SCROLLINFO);
2485 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2490 infoPtr->uInternalStatus |= TV_VSCROLL;
2492 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2493 si.nPos = infoPtr->firstVisible->visibleOrder;
2494 si.nMax = infoPtr->maxVisibleOrder - 1;
2496 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2500 if (infoPtr->uInternalStatus & TV_VSCROLL)
2501 ShowScrollBar(hwnd, SB_VERT, FALSE);
2503 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2508 infoPtr->uInternalStatus |= TV_HSCROLL;
2510 si.nPage = infoPtr->clientWidth;
2511 si.nPos = infoPtr->scrollX;
2512 si.nMax = infoPtr->treeWidth - 1;
2514 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2516 si.nPos = si.nMax - max( si.nPage-1, 0 );
2520 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2524 if (infoPtr->uInternalStatus & TV_HSCROLL)
2525 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2530 if (infoPtr->scrollX != scrollX)
2532 TREEVIEW_HScroll(infoPtr,
2533 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2537 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2540 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2542 TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2544 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2547 GetClientRect(infoPtr->hwnd, &rect);
2548 FillRect(hDC, &rect, hBrush);
2549 DeleteObject(hBrush);
2555 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2557 HWND hwnd = infoPtr->hwnd;
2559 TREEVIEW_ITEM *wineItem;
2561 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2563 TRACE("empty window\n");
2567 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2570 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2572 ReleaseDC(hwnd, hdc);
2576 for (wineItem = infoPtr->root->firstChild;
2578 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2580 if (ISVISIBLE(wineItem))
2582 /* Avoid unneeded calculations */
2583 if (wineItem->rect.top > rect.bottom)
2585 if (wineItem->rect.bottom < rect.top)
2588 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2592 TREEVIEW_UpdateScrollBars(infoPtr);
2594 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2596 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2600 TREEVIEW_QueueRefresh(TREEVIEW_INFO *infoPtr)
2602 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2605 /* It be that item->rect is out of date. If so, we invalidate the wrong area,
2606 * but then whoever updates item->rect knows that they must invalidate after
2609 TREEVIEW_QueueItemRefresh(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2612 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2616 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2627 GetUpdateRect(infoPtr->hwnd, &rc, TRUE);
2631 hdc = BeginPaint(infoPtr->hwnd, &ps);
2635 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2636 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2639 EndPaint(infoPtr->hwnd, &ps);
2645 /* Sorting **************************************************************/
2647 /***************************************************************************
2648 * Forward the DPA local callback to the treeview owner callback
2651 TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2653 /* Forward the call to the client-defined callback */
2654 return pCallBackSort->lpfnCompare(first->lParam,
2656 pCallBackSort->lParam);
2659 /***************************************************************************
2660 * Treeview native sort routine: sort on item text.
2663 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2664 TREEVIEW_INFO *infoPtr)
2666 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2667 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2669 return strcasecmp(first->pszText, second->pszText);
2672 /* Returns the number of physical children belonging to item. */
2674 TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2679 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2685 /* Returns a DPA containing a pointer to each physical child of item in
2686 * sibling order. If item has no children, an empty DPA is returned. */
2688 TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2690 HTREEITEM child = item->firstChild;
2692 HDPA list = DPA_Create(8);
2693 if (list == 0) return NULL;
2695 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2697 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2707 /***************************************************************************
2708 * Setup the treeview structure with regards of the sort method
2709 * and sort the children of the TV item specified in lParam
2710 * fRecurse: currently unused. Should be zero.
2711 * parent: if pSort!=NULL, should equal pSort->hParent.
2712 * otherwise, item which child items are to be sorted.
2713 * pSort: sort method info. if NULL, sort on item text.
2714 * if non-NULL, sort on item's lParam content, and let the
2715 * application decide what that means. See also TVM_SORTCHILDRENCB.
2719 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2723 PFNDPACOMPARE pfnCompare;
2726 /* undocumented feature: TVI_ROOT means `sort the whole tree' */
2727 if (parent == TVI_ROOT)
2728 parent = infoPtr->root;
2730 /* Check for a valid handle to the parent item */
2731 if (!TREEVIEW_ValidItem(infoPtr, parent))
2733 ERR("invalid item hParent=%x\n", (INT)parent);
2739 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2740 lpCompare = (LPARAM)pSort;
2744 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2745 lpCompare = (LPARAM)infoPtr;
2748 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2750 /* Make sure there is something to sort */
2753 /* TREEVIEW_ITEM rechaining */
2756 HTREEITEM nextItem = 0;
2757 HTREEITEM prevItem = 0;
2759 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
2761 if (sortList == NULL)
2764 /* let DPA sort the list */
2765 DPA_Sort(sortList, pfnCompare, lpCompare);
2767 /* The order of DPA entries has been changed, so fixup the
2768 * nextSibling and prevSibling pointers. */
2770 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
2771 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
2773 /* link the two current item toghether */
2774 item->nextSibling = nextItem;
2775 nextItem->prevSibling = item;
2777 if (prevItem == NULL)
2779 /* this is the first item, update the parent */
2780 parent->firstChild = item;
2781 item->prevSibling = NULL;
2785 /* fix the back chaining */
2786 item->prevSibling = prevItem;
2789 /* get ready for the next one */
2794 /* the last item is pointed to by item and never has a sibling */
2795 item->nextSibling = NULL;
2796 parent->lastChild = item;
2798 DPA_Destroy(sortList);
2800 TREEVIEW_VerifyTree(infoPtr);
2802 if (parent->state & TVIS_EXPANDED)
2804 int visOrder = infoPtr->firstVisible->visibleOrder;
2806 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
2808 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
2810 TREEVIEW_ITEM *item;
2812 for (item = infoPtr->root->firstChild; item != NULL;
2813 item = TREEVIEW_GetNextListItem(infoPtr, item))
2815 if (item->visibleOrder == visOrder)
2819 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
2822 TREEVIEW_QueueRefresh(infoPtr);
2831 /***************************************************************************
2832 * Setup the treeview structure with regards of the sort method
2833 * and sort the children of the TV item specified in lParam
2836 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
2838 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
2842 /***************************************************************************
2843 * Sort the children of the TV item specified in lParam.
2846 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2848 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
2852 /* Expansion/Collapse ***************************************************/
2855 TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2858 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGA, action,
2859 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
2860 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
2865 TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2868 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDA, action,
2869 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
2870 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
2875 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
2876 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
2878 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2879 BOOL bRemoveChildren, BOOL bUser)
2881 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
2882 BOOL bSetSelection, bSetFirstVisible;
2884 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
2886 if (!(wineItem->state & TVIS_EXPANDED) || wineItem->firstChild == NULL)
2890 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
2892 wineItem->state &= ~TVIS_EXPANDED;
2895 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
2897 bSetSelection = (infoPtr->selectedItem != NULL
2898 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
2900 bSetFirstVisible = (infoPtr->firstVisible != NULL
2901 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
2903 if (bRemoveChildren)
2905 TRACE("TVE_COLLAPSERESET\n");
2906 wineItem->state &= ~TVIS_EXPANDEDONCE;
2907 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
2910 if (wineItem->firstChild)
2912 TREEVIEW_ITEM *item, *sibling;
2914 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2916 for (item = wineItem->firstChild; item != sibling;
2917 item = TREEVIEW_GetNextListItem(infoPtr, item))
2919 item->visibleOrder = -1;
2923 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2925 TREEVIEW_SetFirstVisible(infoPtr, bSetFirstVisible ? wineItem
2926 : infoPtr->firstVisible, TRUE);
2930 /* Don't call DoSelectItem, it sends notifications. */
2931 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
2932 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
2933 wineItem->state |= TVIS_SELECTED;
2934 infoPtr->selectedItem = wineItem;
2936 TREEVIEW_EnsureVisible(infoPtr, wineItem, FALSE);
2939 TREEVIEW_UpdateScrollBars(infoPtr);
2940 TREEVIEW_QueueRefresh(infoPtr);
2946 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2947 BOOL bExpandPartial, BOOL bUser)
2951 if (!TREEVIEW_HasChildren(infoPtr, wineItem)
2952 || wineItem->state & TVIS_EXPANDED)
2955 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
2957 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
2959 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
2961 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
2965 wineItem->state |= TVIS_EXPANDED;
2966 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
2967 wineItem->state |= TVIS_EXPANDEDONCE;
2971 /* this item has already been expanded */
2972 wineItem->state |= TVIS_EXPANDED;
2976 FIXME("TVE_EXPANDPARTIAL not implemented\n");
2978 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2979 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
2980 TREEVIEW_UpdateScrollBars(infoPtr);
2982 /* Scroll up so that as many children as possible are visible.
2983 * This looses when expanding causes an HScroll bar to appear, but we
2984 * don't know that yet, so the last item is obscured. */
2985 if (wineItem->firstChild != NULL)
2987 int nChildren = wineItem->lastChild->visibleOrder
2988 - wineItem->firstChild->visibleOrder + 1;
2990 int visible_pos = wineItem->visibleOrder
2991 - infoPtr->firstVisible->visibleOrder;
2993 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
2995 if (visible_pos > 0 && nChildren > rows_below)
2997 int scroll = nChildren - rows_below;
2999 if (scroll > visible_pos)
3000 scroll = visible_pos;
3004 TREEVIEW_ITEM *newFirstVisible
3005 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3009 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3014 TREEVIEW_QueueRefresh(infoPtr);
3020 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3024 if (wineItem->state & TVIS_EXPANDED)
3025 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3027 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3031 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3033 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3035 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3037 if (TREEVIEW_HasChildren(infoPtr, item))
3038 TREEVIEW_ExpandAll(infoPtr, item);
3042 /* Note:If the specified item is the child of a collapsed parent item,
3043 the parent's list of child items is (recursively) expanded to reveal the
3044 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3045 know if it also applies here.
3049 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3051 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3054 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3055 TREEVIEW_ItemName(wineItem), flag,
3056 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3058 switch (flag & TVE_TOGGLE)
3061 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3065 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3069 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3076 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3080 /* Hit-Testing **********************************************************/
3082 static TREEVIEW_ITEM *
3083 TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3085 TREEVIEW_ITEM *wineItem;
3088 if (!infoPtr->firstVisible)
3091 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3093 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3094 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3096 if (row >= wineItem->visibleOrder
3097 && row < wineItem->visibleOrder + wineItem->iIntegral)
3105 TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3107 TREEVIEW_ITEM *wineItem;
3113 GetClientRect(infoPtr->hwnd, &rect);
3120 status |= TVHT_TOLEFT;
3122 else if (x > rect.right)
3124 status |= TVHT_TORIGHT;
3129 status |= TVHT_ABOVE;
3131 else if (y > rect.bottom)
3133 status |= TVHT_BELOW;
3138 lpht->flags = status;
3139 return (LRESULT)(HTREEITEM)NULL;
3142 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3145 lpht->flags = TVHT_NOWHERE;
3146 return (LRESULT)(HTREEITEM)NULL;
3149 if (x >= wineItem->textOffset + wineItem->textWidth)
3151 lpht->flags = TVHT_ONITEMRIGHT;
3153 else if (x >= wineItem->textOffset)
3155 lpht->flags = TVHT_ONITEMLABEL;
3157 else if (x >= wineItem->imageOffset)
3159 lpht->flags = TVHT_ONITEMICON;
3161 else if (x >= wineItem->stateOffset)
3163 lpht->flags = TVHT_ONITEMSTATEICON;
3165 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3167 lpht->flags = TVHT_ONITEMBUTTON;
3171 lpht->flags = TVHT_ONITEMINDENT;
3174 lpht->hItem = wineItem;
3175 TRACE("(%ld,%ld):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3177 return (LRESULT)wineItem;
3180 /* Item Label Editing ***************************************************/
3183 TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3185 return infoPtr->hwndEdit;
3188 static LRESULT CALLBACK
3189 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3191 TREEVIEW_INFO *infoPtr;
3192 BOOL bCancel = FALSE;
3199 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3201 TRACE("WM_PAINT start\n");
3202 rc = CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3204 TRACE("WM_PAINT done\n");
3210 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3211 if (infoPtr->bIgnoreEditKillFocus)
3218 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3221 if (wParam == (WPARAM)VK_ESCAPE)
3226 else if (wParam == (WPARAM)VK_RETURN)
3234 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3236 return CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3241 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3242 /* eg. Using a messagebox */
3244 infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3245 infoPtr->bIgnoreEditKillFocus = TRUE;
3246 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3247 infoPtr->bIgnoreEditKillFocus = FALSE;
3253 /* should handle edit control messages here */
3256 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3258 TRACE("%x %ld\n", wParam, lParam);
3260 switch (HIWORD(wParam))
3265 * Adjust the edit window size
3268 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3269 HDC hdc = GetDC(infoPtr->hwndEdit);
3272 HFONT hFont, hOldFont = 0;
3274 infoPtr->bLabelChanged = TRUE;
3276 len = GetWindowTextA(infoPtr->hwndEdit, buffer, sizeof(buffer));
3278 /* Select font to get the right dimension of the string */
3279 hFont = SendMessageA(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3282 hOldFont = SelectObject(hdc, hFont);
3285 if (GetTextExtentPoint32A(hdc, buffer, strlen(buffer), &sz))
3287 TEXTMETRICA textMetric;
3289 /* Add Extra spacing for the next character */
3290 GetTextMetricsA(hdc, &textMetric);
3291 sz.cx += (textMetric.tmMaxCharWidth * 2);
3293 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3295 infoPtr->clientWidth - editItem->textOffset + 2);
3297 SetWindowPos(infoPtr->hwndEdit,
3302 editItem->rect.bottom - editItem->rect.top + 3,
3303 SWP_NOMOVE | SWP_DRAWFRAME);
3308 SelectObject(hdc, hOldFont);
3311 ReleaseDC(infoPtr->hwnd, hdc);
3316 return SendMessageA(GetParent(infoPtr->hwnd), WM_COMMAND, wParam, lParam);
3323 TREEVIEW_EditLabelA(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3325 HWND hwnd = infoPtr->hwnd;
3328 TREEVIEW_ITEM *editItem = hItem;
3329 HINSTANCE hinst = GetWindowLongA(hwnd, GWL_HINSTANCE);
3332 TEXTMETRICA textMetric;
3334 TRACE("%x %p\n", (unsigned)hwnd, hItem);
3335 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3338 if (infoPtr->hwndEdit)
3339 return infoPtr->hwndEdit;
3341 infoPtr->bLabelChanged = FALSE;
3343 /* Make sure that edit item is selected */
3344 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3345 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3347 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3350 /* Select the font to get appropriate metric dimensions */
3351 if (infoPtr->hFont != 0)
3353 hOldFont = SelectObject(hdc, infoPtr->hFont);
3356 /*Get String Lenght in pixels */
3357 GetTextExtentPoint32A(hdc, editItem->pszText, strlen(editItem->pszText),
3360 /*Add Extra spacing for the next character */
3361 GetTextMetricsA(hdc, &textMetric);
3362 sz.cx += (textMetric.tmMaxCharWidth * 2);
3364 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3365 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3367 if (infoPtr->hFont != 0)
3369 SelectObject(hdc, hOldFont);
3372 ReleaseDC(hwnd, hdc);
3373 hwndEdit = CreateWindowExA(WS_EX_LEFT,
3376 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3377 WS_CLIPSIBLINGS | ES_WANTRETURN |
3378 ES_LEFT, editItem->textOffset - 2,
3379 editItem->rect.top - 1, sz.cx + 3,
3380 editItem->rect.bottom -
3381 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3382 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3384 infoPtr->hwndEdit = hwndEdit;
3386 /* Get a 2D border. */
3387 SetWindowLongA(hwndEdit, GWL_EXSTYLE,
3388 GetWindowLongA(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3389 SetWindowLongA(hwndEdit, GWL_STYLE,
3390 GetWindowLongA(hwndEdit, GWL_STYLE) | WS_BORDER);
3392 SendMessageA(hwndEdit, WM_SETFONT, TREEVIEW_FontForItem(infoPtr, editItem),
3395 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongA(hwndEdit, GWL_WNDPROC,
3397 TREEVIEW_Edit_SubclassProc);
3399 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3401 DestroyWindow(hwndEdit);
3402 infoPtr->hwndEdit = 0;
3406 infoPtr->selectedItem = hItem;
3407 SetWindowTextA(hwndEdit, editItem->pszText);
3409 SendMessageA(hwndEdit, EM_SETSEL, 0, -1);
3410 ShowWindow(hwndEdit, SW_SHOW);
3417 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3419 HWND hwnd = infoPtr->hwnd;
3420 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3423 char tmpText[1024] = { '\0' };
3426 if (!infoPtr->hwndEdit)
3429 tvdi.hdr.hwndFrom = hwnd;
3430 tvdi.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
3431 tvdi.hdr.code = TVN_ENDLABELEDITA;
3433 tvdi.item.hItem = editedItem;
3434 tvdi.item.state = editedItem->state;
3435 tvdi.item.lParam = editedItem->lParam;
3439 iLength = GetWindowTextA(infoPtr->hwndEdit, tmpText, 1023);
3441 if (iLength >= 1023)
3443 ERR("Insuficient space to retrieve new item label.");
3446 tvdi.item.pszText = tmpText;
3447 tvdi.item.cchTextMax = iLength + 1;
3451 tvdi.item.pszText = NULL;
3452 tvdi.item.cchTextMax = 0;
3455 bCommit = (BOOL)SendMessageA(GetParent(hwnd),
3457 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3459 if (!bCancel && bCommit) /* Apply the changes */
3461 if (strcmp(tmpText, editedItem->pszText) != 0)
3463 if (NULL == COMCTL32_ReAlloc(editedItem->pszText, iLength + 1))
3465 ERR("OutOfMemory, cannot allocate space for label");
3466 DestroyWindow(infoPtr->hwndEdit);
3467 infoPtr->hwndEdit = 0;
3472 editedItem->cchTextMax = iLength + 1;
3473 lstrcpyA(editedItem->pszText, tmpText);
3478 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3479 DestroyWindow(infoPtr->hwndEdit);
3480 infoPtr->hwndEdit = 0;
3485 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3487 if (wParam != TV_EDIT_TIMER)
3489 ERR("got unknown timer\n");
3493 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3494 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3496 TREEVIEW_EditLabelA(infoPtr, infoPtr->selectedItem);
3502 /* Mouse Tracking/Drag **************************************************/
3504 /***************************************************************************
3505 * This is quite unusual piece of code, but that's how it's implemented in
3509 TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3511 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3512 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3516 r.top = pt.y - cyDrag;
3517 r.left = pt.x - cxDrag;
3518 r.bottom = pt.y + cyDrag;
3519 r.right = pt.x + cxDrag;
3521 SetCapture(infoPtr->hwnd);
3525 if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3527 if (msg.message == WM_MOUSEMOVE)
3529 pt.x = (LONG)(INT16)LOWORD(msg.lParam);
3530 pt.y = (LONG)(INT16)HIWORD(msg.lParam);
3531 if (PtInRect(&r, pt))
3539 else if (msg.message >= WM_LBUTTONDOWN &&
3540 msg.message <= WM_RBUTTONDBLCLK)
3542 if (msg.message == WM_RBUTTONUP)
3543 TREEVIEW_RButtonUp(infoPtr, &pt);
3547 DispatchMessageA(&msg);
3550 if (GetCapture() != infoPtr->hwnd)
3560 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3562 TREEVIEW_ITEM *wineItem;
3566 SetFocus(infoPtr->hwnd);
3568 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3570 /* If there is pending 'edit label' event - kill it now */
3571 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3574 hit.pt.x = (LONG)(INT16)LOWORD(lParam);
3575 hit.pt.y = (LONG)(INT16)HIWORD(lParam);
3577 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3580 TRACE("item %d \n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3582 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3586 case TVHT_ONITEMRIGHT:
3587 /* FIXME: we should not have send NM_DBLCLK in this case. */
3590 case TVHT_ONITEMINDENT:
3591 if (!(infoPtr->dwStyle & TVS_HASLINES))
3597 int level = hit.pt.x / infoPtr->uIndent;
3598 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3600 while (wineItem->iLevel > level)
3602 wineItem = wineItem->parent;
3608 case TVHT_ONITEMLABEL:
3609 case TVHT_ONITEMICON:
3610 case TVHT_ONITEMBUTTON:
3611 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3614 case TVHT_ONITEMSTATEICON:
3615 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3616 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3618 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3627 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3629 HWND hwnd = infoPtr->hwnd;
3634 /* If Edit control is active - kill it and return.
3635 * The best way to do it is to set focus to itself.
3636 * Edit control subclassed procedure will automatically call
3639 if (infoPtr->hwndEdit)
3645 ht.pt.x = (LONG)(INT16)LOWORD(lParam);
3646 ht.pt.y = (LONG)(INT16)HIWORD(lParam);
3648 TREEVIEW_HitTest(infoPtr, &ht);
3649 TRACE("item %d \n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3651 /* update focusedItem and redraw both items */
3652 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3654 infoPtr->focusedItem = ht.hItem;
3655 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3657 if(infoPtr->selectedItem)
3658 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3661 bTrack = (ht.flags & TVHT_ONITEM)
3662 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3664 /* Send NM_CLICK right away */
3666 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3669 if (ht.flags & TVHT_ONITEMBUTTON)
3671 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
3675 { /* if TREEVIEW_TrackMouse == 1 dragging occured and the cursor left the dragged item's rectangle */
3676 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3678 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGA, ht.hItem,
3680 infoPtr->dropItem = ht.hItem;
3682 /* clean up focuedItem as we dragged and won't select this item */
3683 if(infoPtr->focusedItem)
3685 /* refresh the item that was focused */
3686 tempItem = infoPtr->focusedItem;
3687 infoPtr->focusedItem = 0;
3688 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
3690 /* refresh the selected item to return the filled background */
3691 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
3698 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3702 * If the style allow editing and the node is already selected
3703 * and the click occured on the item label...
3705 if ((infoPtr->dwStyle & TVS_EDITLABELS) &&
3706 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem))
3708 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3709 KillTimer(hwnd, TV_EDIT_TIMER);
3711 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
3712 infoPtr->Timer |= TV_EDIT_TIMER_SET;
3714 else if (ht.flags & TVHT_ONITEM) /* select the item if the hit was inside of the icon or text */
3717 * if we are TVS_SINGLEEXPAND then we want this single click to
3718 * do a bunch of things.
3720 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
3721 (infoPtr->hwndEdit == 0))
3723 TREEVIEW_ITEM *SelItem;
3726 * Send the notification
3728 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVIF_HANDLE | TVIF_PARAM,
3732 * Close the previous selection all the way to the root
3733 * as long as the new selection is not a child
3735 if((infoPtr->selectedItem)
3736 && (infoPtr->selectedItem != ht.hItem))
3738 BOOL closeit = TRUE;
3741 /* determine of the hitItem is a child of the currently selected item */
3742 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3744 closeit = (SelItem != infoPtr->selectedItem);
3745 SelItem = SelItem->parent;
3750 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3751 SelItem = infoPtr->selectedItem;
3753 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3755 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3756 SelItem = SelItem->parent;
3762 * Expand the current item
3764 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
3767 /* Select the current item */
3768 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
3770 else if (ht.flags & TVHT_ONITEMSTATEICON)
3772 /* TVS_CHECKBOXES requires us to toggle the current state */
3773 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3774 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
3784 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3788 if (infoPtr->hwndEdit)
3790 SetFocus(infoPtr->hwnd);
3794 ht.pt.x = (LONG)(INT16)LOWORD(lParam);
3795 ht.pt.y = (LONG)(INT16)HIWORD(lParam);
3797 TREEVIEW_HitTest(infoPtr, &ht);
3799 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3803 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGA, ht.hItem,
3805 infoPtr->dropItem = ht.hItem;
3810 SetFocus(infoPtr->hwnd);
3811 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
3818 TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
3820 HWND hwnd = infoPtr->hwnd;
3823 /* Change to screen coordinate for WM_CONTEXTMENU */
3824 ClientToScreen(hwnd, &pt);
3826 SendMessageA(hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, MAKELPARAM(pt.x, pt.y));
3832 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3834 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
3838 HBITMAP hbmp, hOldbmp;
3845 if (!(infoPtr->himlNormal))
3848 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
3851 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
3853 hwtop = GetDesktopWindow();
3854 htopdc = GetDC(hwtop);
3855 hdc = CreateCompatibleDC(htopdc);
3857 hOldFont = SelectObject(hdc, infoPtr->hFont);
3858 GetTextExtentPoint32A(hdc, dragItem->pszText, lstrlenA(dragItem->pszText),
3860 TRACE("%ld %ld %s %d\n", size.cx, size.cy, dragItem->pszText,
3861 lstrlenA(dragItem->pszText));
3862 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
3863 hOldbmp = SelectObject(hdc, hbmp);
3865 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
3870 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
3871 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
3875 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
3876 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
3879 /* draw item text */
3881 SetRect(&rc, cx, 0, size.cx, size.cy);
3882 DrawTextA(hdc, dragItem->pszText, lstrlenA(dragItem->pszText), &rc,
3884 SelectObject(hdc, hOldFont);
3885 SelectObject(hdc, hOldbmp);
3887 ImageList_Add(infoPtr->dragList, hbmp, 0);
3891 ReleaseDC(hwtop, htopdc);
3893 return (LRESULT)infoPtr->dragList;
3896 /* Selection ************************************************************/
3899 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
3902 TREEVIEW_ITEM *prevSelect;
3905 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
3907 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
3908 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
3909 newSelect ? newSelect->state : 0);
3911 /* reset and redraw focusedItem if focusedItem was set so we don't */
3912 /* have to worry about the previously focused item when we set a new one */
3913 if(infoPtr->focusedItem)
3915 rcFocused = (infoPtr->focusedItem)->rect;
3916 infoPtr->focusedItem = 0;
3917 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
3923 prevSelect = infoPtr->selectedItem;
3925 if (prevSelect == newSelect)
3928 if (TREEVIEW_SendTreeviewNotify(infoPtr,
3931 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
3937 prevSelect->state &= ~TVIS_SELECTED;
3939 newSelect->state |= TVIS_SELECTED;
3941 infoPtr->selectedItem = newSelect;
3943 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
3945 TREEVIEW_SendTreeviewNotify(infoPtr,
3948 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
3951 TREEVIEW_QueueItemRefresh(infoPtr, prevSelect);
3952 TREEVIEW_QueueItemRefresh(infoPtr, newSelect);
3955 case TVGN_DROPHILITE:
3956 prevSelect = infoPtr->dropItem;
3959 prevSelect->state &= ~TVIS_DROPHILITED;
3961 infoPtr->dropItem = newSelect;
3964 newSelect->state |= TVIS_DROPHILITED;
3966 TREEVIEW_QueueItemRefresh(infoPtr, prevSelect);
3967 TREEVIEW_QueueItemRefresh(infoPtr, newSelect);
3970 case TVGN_FIRSTVISIBLE:
3971 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
3972 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
3973 TREEVIEW_QueueRefresh(infoPtr);
3977 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
3981 /* FIXME: handle NM_KILLFOCUS etc */
3983 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
3985 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
3988 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
3990 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
3996 /* Scrolling ************************************************************/
3999 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4001 HTREEITEM newFirstVisible = NULL;
4004 if (!TREEVIEW_ValidItem(infoPtr, item))
4007 if (!ISVISIBLE(item))
4009 /* Expand parents as necessary. */
4012 /* see if we are trying to ensure that root is vislble */
4013 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4014 parent = item->parent;
4016 parent = item; /* this item is the topmost item */
4018 while (parent != infoPtr->root)
4020 if (!(parent->state & TVIS_EXPANDED))
4021 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4023 parent = parent->parent;
4027 TRACE("%p (%s) %ld - %ld\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4028 infoPtr->firstVisible->visibleOrder);
4030 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4032 if (visible_pos < 0)
4034 /* item is before the start of the list: put it at the top. */
4035 newFirstVisible = item;
4037 else if (visible_pos >= TREEVIEW_GetVisibleCount(infoPtr)
4038 /* Sometimes, before we are displayed, GVC is 0, causing us to
4039 * spuriously scroll up. */
4042 /* item is past the end of the list. */
4043 int scroll = visible_pos - TREEVIEW_GetVisibleCount(infoPtr);
4045 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4051 /* Scroll window so item's text is visible as much as possible */
4052 /* Calculation of amount of extra space is taken from EditLabel code */
4054 TEXTMETRICA textMetric;
4055 HDC hdc = GetWindowDC(infoPtr->hwnd);
4057 x = item->textWidth;
4059 GetTextMetricsA(hdc, &textMetric);
4060 ReleaseDC(infoPtr->hwnd, hdc);
4062 x += (textMetric.tmMaxCharWidth * 2);
4063 x = max(x, textMetric.tmMaxCharWidth * 3);
4065 if (item->textOffset < 0)
4066 pos = item->textOffset;
4067 else if (item->textOffset + x > infoPtr->clientWidth)
4069 if (x > infoPtr->clientWidth)
4070 pos = item->textOffset;
4072 pos = item->textOffset + x - infoPtr->clientWidth;
4077 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4080 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4082 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4091 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4092 TREEVIEW_ITEM *newFirstVisible,
4093 BOOL bUpdateScrollPos)
4097 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4099 if (newFirstVisible != NULL)
4101 /* Prevent an empty gap from appearing at the bottom... */
4102 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4103 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4107 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4110 /* ... unless we just don't have enough items. */
4111 if (newFirstVisible == NULL)
4112 newFirstVisible = infoPtr->root->firstChild;
4116 if (infoPtr->firstVisible != newFirstVisible)
4118 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4120 infoPtr->firstVisible = newFirstVisible;
4121 TREEVIEW_QueueRefresh(infoPtr);
4125 TREEVIEW_ITEM *item;
4126 int scroll = infoPtr->uItemHeight *
4127 (infoPtr->firstVisible->visibleOrder
4128 - newFirstVisible->visibleOrder);
4130 infoPtr->firstVisible = newFirstVisible;
4132 for (item = infoPtr->root->firstChild; item != NULL;
4133 item = TREEVIEW_GetNextListItem(infoPtr, item))
4135 item->rect.top += scroll;
4136 item->rect.bottom += scroll;
4139 if (bUpdateScrollPos)
4140 SetScrollPos(infoPtr->hwnd, SB_VERT,
4141 newFirstVisible->visibleOrder, TRUE);
4143 ScrollWindow(infoPtr->hwnd, 0, scroll, NULL, NULL);
4144 UpdateWindow(infoPtr->hwnd);
4149 /************************************************************************
4150 * VScroll is always in units of visible items. i.e. we always have a
4151 * visible item aligned to the top of the control. (Unless we have no
4155 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4157 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4158 TREEVIEW_ITEM *newFirstVisible = NULL;
4160 int nScrollCode = LOWORD(wParam);
4162 TRACE("wp %x\n", wParam);
4164 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4167 if (infoPtr->hwndEdit)
4168 SetFocus(infoPtr->hwnd);
4170 if (!oldFirstVisible)
4172 assert(infoPtr->root->firstChild == NULL);
4176 switch (nScrollCode)
4179 newFirstVisible = infoPtr->root->firstChild;
4183 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4187 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4191 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4195 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4196 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4200 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4201 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4205 case SB_THUMBPOSITION:
4206 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4207 infoPtr->root->firstChild,
4208 (LONG)(INT16)HIWORD(wParam));
4215 if (newFirstVisible != NULL)
4217 if (newFirstVisible != oldFirstVisible)
4218 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4219 nScrollCode != SB_THUMBTRACK);
4220 else if (nScrollCode == SB_THUMBPOSITION)
4221 SetScrollPos(infoPtr->hwnd, SB_VERT,
4222 newFirstVisible->visibleOrder, TRUE);
4229 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4232 int scrollX = infoPtr->scrollX;
4233 int nScrollCode = LOWORD(wParam);
4235 TRACE("wp %x\n", wParam);
4237 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4240 if (infoPtr->hwndEdit)
4241 SetFocus(infoPtr->hwnd);
4243 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4244 /* shall never occure */
4251 switch (nScrollCode)
4254 scrollX -= infoPtr->uItemHeight;
4257 scrollX += infoPtr->uItemHeight;
4260 scrollX -= infoPtr->clientWidth;
4263 scrollX += infoPtr->clientWidth;
4267 case SB_THUMBPOSITION:
4268 scrollX = (int)(INT16)HIWORD(wParam);
4275 if (scrollX > maxWidth)
4277 else if (scrollX < 0)
4281 if (scrollX != infoPtr->scrollX)
4283 TREEVIEW_ITEM *item;
4284 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4286 for (item = infoPtr->root->firstChild; item != NULL;
4287 item = TREEVIEW_GetNextListItem(infoPtr, item))
4289 item->linesOffset += scroll_pixels;
4290 item->stateOffset += scroll_pixels;
4291 item->imageOffset += scroll_pixels;
4292 item->textOffset += scroll_pixels;
4295 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4296 infoPtr->scrollX = scrollX;
4297 UpdateWindow(infoPtr->hwnd);
4300 if (nScrollCode != SB_THUMBTRACK)
4301 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4307 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4310 UINT pulScrollLines = 3;
4312 if (infoPtr->firstVisible == NULL)
4315 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4317 gcWheelDelta = -(short)HIWORD(wParam);
4318 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4320 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4322 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4323 int maxDy = infoPtr->maxVisibleOrder;
4331 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4336 /* Create/Destroy *******************************************************/
4339 TREEVIEW_Create(HWND hwnd)
4342 TREEVIEW_INFO *infoPtr;
4344 TRACE("wnd %x, style %lx\n", hwnd, GetWindowLongA(hwnd, GWL_STYLE));
4346 infoPtr = (TREEVIEW_INFO *)COMCTL32_Alloc(sizeof(TREEVIEW_INFO));
4348 if (infoPtr == NULL)
4350 ERR("could not allocate info memory!\n");
4354 SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
4356 infoPtr->hwnd = hwnd;
4357 infoPtr->dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
4358 infoPtr->uInternalStatus = 0;
4360 infoPtr->uNumItems = 0;
4361 infoPtr->cdmode = 0;
4362 infoPtr->uScrollTime = 300; /* milliseconds */
4363 infoPtr->bRedraw = TRUE;
4365 GetClientRect(hwnd, &rcClient);
4367 /* No scroll bars yet. */
4368 infoPtr->clientWidth = rcClient.right;
4369 infoPtr->clientHeight = rcClient.bottom;
4371 infoPtr->treeWidth = 0;
4372 infoPtr->treeHeight = 0;
4374 infoPtr->uIndent = 19;
4375 infoPtr->selectedItem = 0;
4376 infoPtr->focusedItem = 0;
4378 infoPtr->firstVisible = 0;
4379 infoPtr->maxVisibleOrder = 0;
4380 infoPtr->dropItem = 0;
4381 infoPtr->insertMarkItem = 0;
4382 infoPtr->insertBeforeorAfter = 0;
4385 infoPtr->scrollX = 0;
4387 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4388 infoPtr->clrText = -1; /* use system color */
4389 infoPtr->clrLine = RGB(128, 128, 128);
4390 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4394 infoPtr->hwndEdit = 0;
4395 infoPtr->wpEditOrig = NULL;
4396 infoPtr->bIgnoreEditKillFocus = FALSE;
4397 infoPtr->bLabelChanged = FALSE;
4399 infoPtr->himlNormal = NULL;
4400 infoPtr->himlState = NULL;
4401 infoPtr->normalImageWidth = 0;
4402 infoPtr->normalImageHeight = 0;
4403 infoPtr->stateImageWidth = 0;
4404 infoPtr->stateImageHeight = 0;
4406 infoPtr->items = DPA_Create(16);
4408 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
4409 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4411 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4413 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4414 infoPtr->root->state = TVIS_EXPANDED;
4415 infoPtr->root->iLevel = -1;
4416 infoPtr->root->visibleOrder = -1;
4419 infoPtr->hwndNotify = GetParent32 (hwnd);
4420 infoPtr->bTransparent = ( GetWindowLongA( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4423 infoPtr->hwndToolTip = 0;
4424 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4425 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4427 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4430 HBITMAP hbm, hbmOld;
4434 infoPtr->himlState =
4435 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4437 hdc = CreateCompatibleDC(0);
4438 hbm = CreateCompatibleBitmap(hdc, 48, 16);
4439 hbmOld = SelectObject(hdc, hbm);
4441 rc.left = 0; rc.top = 0;
4442 rc.right = 48; rc.bottom = 16;
4443 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4445 rc.left = 18; rc.top = 2;
4446 rc.right = 30; rc.bottom = 14;
4447 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4448 DFCS_BUTTONCHECK|DFCS_FLAT);
4450 rc.left = 34; rc.right = 46;
4451 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4452 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4454 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4455 GetSysColor(COLOR_WINDOW));
4456 TRACE("chckbox index %d\n", nIndex);
4457 SelectObject(hdc, hbmOld);
4461 infoPtr->stateImageWidth = 16;
4462 infoPtr->stateImageHeight = 16;
4469 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4473 TREEVIEW_RemoveTree(infoPtr);
4475 /* tool tip is automatically destroyed: we are its owner */
4477 /* Restore original windproc. */
4478 if (infoPtr->hwndEdit)
4479 SetWindowLongA(infoPtr->hwndEdit, GWL_WNDPROC,
4480 (LONG)infoPtr->wpEditOrig);
4482 /* Deassociate treeview from the window before doing anything drastic. */
4483 SetWindowLongA(infoPtr->hwnd, 0, (LONG)NULL);
4485 DeleteObject(infoPtr->hBoldFont);
4486 COMCTL32_Free(infoPtr);
4491 /* Miscellaneous Messages ***********************************************/
4494 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
4502 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4503 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
4504 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
4505 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
4506 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
4507 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
4508 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
4509 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
4510 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
4514 if (key >= VK_PRIOR && key <= VK_DOWN)
4516 unsigned char code = scroll[key - VK_PRIOR].code;
4518 (((code & (1 << 7)) == (SB_HORZ << 7))
4520 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
4526 /************************************************************************
4529 * VK_UP Move selection to the previous non-hidden item.
4530 * VK_DOWN Move selection to the next non-hidden item.
4531 * VK_HOME Move selection to the first item.
4532 * VK_END Move selection to the last item.
4533 * VK_LEFT If expanded then collapse, otherwise move to parent.
4534 * VK_RIGHT If collapsed then expand, otherwise move to first child.
4536 * VK_SUBTRACT Collapse.
4537 * VK_MULTIPLY Expand all.
4538 * VK_PRIOR Move up GetVisibleCount items.
4539 * VK_NEXT Move down GetVisibleCount items.
4540 * VK_BACK Move to parent.
4541 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
4544 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4546 /* If it is non-NULL and different, it will be selected and visible. */
4547 TREEVIEW_ITEM *newSelection = NULL;
4549 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
4551 TRACE("%x\n", wParam);
4553 if (prevItem == NULL)
4556 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
4557 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
4562 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
4564 newSelection = infoPtr->root->firstChild;
4568 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
4572 newSelection = infoPtr->root->firstChild;
4576 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4580 if (prevItem->state & TVIS_EXPANDED)
4582 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4584 else if (prevItem->parent != infoPtr->root)
4586 newSelection = prevItem->parent;
4591 if (TREEVIEW_HasChildren(infoPtr, prevItem))
4593 if (!(prevItem->state & TVIS_EXPANDED))
4594 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4597 newSelection = prevItem->firstChild;
4604 TREEVIEW_ExpandAll(infoPtr, prevItem);
4608 if (!(prevItem->state & TVIS_EXPANDED))
4609 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4613 if (prevItem->state & TVIS_EXPANDED)
4614 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4619 = TREEVIEW_GetListItem(infoPtr, prevItem,
4620 -TREEVIEW_GetVisibleCount(infoPtr));
4625 = TREEVIEW_GetListItem(infoPtr, prevItem,
4626 TREEVIEW_GetVisibleCount(infoPtr));
4630 newSelection = prevItem->parent;
4631 if (newSelection == infoPtr->root)
4632 newSelection = NULL;
4636 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4637 TREEVIEW_ToggleItemState(infoPtr, prevItem);
4641 if (newSelection && newSelection != prevItem)
4643 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
4646 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
4654 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4656 if (wParam == SIZE_RESTORED)
4658 infoPtr->clientWidth = (LONG)(INT16)LOWORD(lParam);
4659 infoPtr->clientHeight = (LONG)(INT16)HIWORD(lParam);
4661 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
4662 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
4663 TREEVIEW_UpdateScrollBars(infoPtr);
4667 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
4670 TREEVIEW_QueueRefresh(infoPtr);
4675 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4677 TRACE("(%x %lx)\n", wParam, lParam);
4679 if (wParam == GWL_STYLE)
4681 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
4683 /* we have to take special care about tooltips */
4684 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
4686 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
4688 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
4693 DestroyWindow(infoPtr->hwndToolTip);
4694 infoPtr->hwndToolTip = 0;
4698 infoPtr->dwStyle = dwNewStyle;
4701 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
4702 TREEVIEW_UpdateScrollBars(infoPtr);
4703 TREEVIEW_QueueRefresh(infoPtr);
4709 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
4713 if (!infoPtr->selectedItem)
4715 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
4719 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
4720 TREEVIEW_QueueItemRefresh(infoPtr, infoPtr->selectedItem);
4725 TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
4729 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
4730 TREEVIEW_QueueItemRefresh(infoPtr, infoPtr->selectedItem);
4735 static LRESULT WINAPI
4736 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
4738 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
4739 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
4742 if (uMsg == WM_CREATE)
4743 TREEVIEW_Create(hwnd);
4750 case TVM_CREATEDRAGIMAGE:
4751 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
4753 case TVM_DELETEITEM:
4754 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
4756 case TVM_EDITLABELA:
4757 return TREEVIEW_EditLabelA(infoPtr, (HTREEITEM)lParam);
4759 case TVM_EDITLABELW:
4760 FIXME("Unimplemented msg TVM_EDITLABELW\n");
4763 case TVM_ENDEDITLABELNOW:
4764 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
4766 case TVM_ENSUREVISIBLE:
4767 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
4770 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
4772 case TVM_GETBKCOLOR:
4773 return TREEVIEW_GetBkColor(infoPtr);
4776 return TREEVIEW_GetCount(infoPtr);
4778 case TVM_GETEDITCONTROL:
4779 return TREEVIEW_GetEditControl(infoPtr);
4781 case TVM_GETIMAGELIST:
4782 return TREEVIEW_GetImageList(infoPtr, wParam);
4785 return TREEVIEW_GetIndent(infoPtr);
4787 case TVM_GETINSERTMARKCOLOR:
4788 return TREEVIEW_GetInsertMarkColor(infoPtr);
4790 case TVM_GETISEARCHSTRINGA:
4791 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
4794 case TVM_GETISEARCHSTRINGW:
4795 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
4799 return TREEVIEW_GetItemA(infoPtr, (LPTVITEMEXA)lParam);
4802 return TREEVIEW_GetItemW(infoPtr, (LPTVITEMEXA)lParam);
4804 case TVM_GETITEMHEIGHT:
4805 return TREEVIEW_GetItemHeight(infoPtr);
4807 case TVM_GETITEMRECT:
4808 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
4810 case TVM_GETITEMSTATE:
4811 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
4813 case TVM_GETLINECOLOR:
4814 return TREEVIEW_GetLineColor(infoPtr);
4816 case TVM_GETNEXTITEM:
4817 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
4819 case TVM_GETSCROLLTIME:
4820 return TREEVIEW_GetScrollTime(infoPtr);
4822 case TVM_GETTEXTCOLOR:
4823 return TREEVIEW_GetTextColor(infoPtr);
4825 case TVM_GETTOOLTIPS:
4826 return TREEVIEW_GetToolTips(infoPtr);
4828 case TVM_GETUNICODEFORMAT:
4829 FIXME("Unimplemented msg TVM_GETUNICODEFORMAT\n");
4832 case TVM_GETVISIBLECOUNT:
4833 return TREEVIEW_GetVisibleCount(infoPtr);
4836 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
4838 case TVM_INSERTITEMA:
4839 return TREEVIEW_InsertItemA(infoPtr, lParam);
4841 case TVM_INSERTITEMW:
4842 return TREEVIEW_InsertItemW(infoPtr, lParam);
4844 case TVM_SELECTITEM:
4845 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
4847 case TVM_SETBKCOLOR:
4848 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
4850 case TVM_SETIMAGELIST:
4851 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
4854 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
4856 case TVM_SETINSERTMARK:
4857 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
4859 case TVM_SETINSERTMARKCOLOR:
4860 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
4863 return TREEVIEW_SetItemA(infoPtr, (LPTVITEMEXA)lParam);
4866 FIXME("Unimplemented msg TVM_SETITEMW\n");
4869 case TVM_SETLINECOLOR:
4870 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
4872 case TVM_SETITEMHEIGHT:
4873 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
4875 case TVM_SETSCROLLTIME:
4876 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
4878 case TVM_SETTEXTCOLOR:
4879 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
4881 case TVM_SETTOOLTIPS:
4882 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
4884 case TVM_SETUNICODEFORMAT:
4885 FIXME("Unimplemented msg TVM_SETUNICODEFORMAT\n");
4888 case TVM_SORTCHILDREN:
4889 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
4891 case TVM_SORTCHILDRENCB:
4892 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
4897 return TREEVIEW_Command(infoPtr, wParam, lParam);
4900 return TREEVIEW_Destroy(infoPtr);
4905 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
4908 return DLGC_WANTARROWS | DLGC_WANTCHARS;
4911 return TREEVIEW_GetFont(infoPtr);
4914 return TREEVIEW_HScroll(infoPtr, wParam);
4917 return TREEVIEW_KeyDown(infoPtr, wParam);
4920 return TREEVIEW_KillFocus(infoPtr);
4922 case WM_LBUTTONDBLCLK:
4923 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
4925 case WM_LBUTTONDOWN:
4926 return TREEVIEW_LButtonDown(infoPtr, lParam);
4928 /* WM_MBUTTONDOWN */
4934 /* WM_NOTIFYFORMAT */
4937 return TREEVIEW_Paint(infoPtr, wParam);
4939 /* WM_PRINTCLIENT */
4941 case WM_RBUTTONDOWN:
4942 return TREEVIEW_RButtonDown(infoPtr, lParam);
4945 return TREEVIEW_SetFocus(infoPtr);
4948 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
4951 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
4954 return TREEVIEW_Size(infoPtr, wParam, lParam);
4956 case WM_STYLECHANGED:
4957 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
4959 /* WM_SYSCOLORCHANGE */
4964 return TREEVIEW_HandleTimer(infoPtr, wParam);
4967 return TREEVIEW_VScroll(infoPtr, wParam);
4969 /* WM_WININICHANGE */
4972 if (wParam & (MK_SHIFT | MK_CONTROL))
4974 return TREEVIEW_MouseWheel(infoPtr, wParam);
4977 TRACE("drawItem\n");
4981 /* This mostly catches MFC and Delphi messages. :( */
4982 if (uMsg >= WM_USER)
4983 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
4985 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
4991 /* Class Registration ***************************************************/
4994 TREEVIEW_Register(void)
5000 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
5001 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5002 wndClass.lpfnWndProc = (WNDPROC)TREEVIEW_WindowProc;
5003 wndClass.cbClsExtra = 0;
5004 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5006 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
5007 wndClass.hbrBackground = 0;
5008 wndClass.lpszClassName = WC_TREEVIEWA;
5010 RegisterClassA(&wndClass);
5015 TREEVIEW_Unregister(void)
5017 UnregisterClassA(WC_TREEVIEWA, (HINSTANCE) NULL);
5021 /* Tree Verification ****************************************************/
5025 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5027 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5028 TREEVIEW_ITEM *item)
5030 assert(infoPtr != NULL);
5031 assert(item != NULL);
5033 /* both NULL, or both non-null */
5034 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5036 assert(item->firstChild != item);
5037 assert(item->lastChild != item);
5039 if (item->firstChild)
5041 assert(item->firstChild->parent == item);
5042 assert(item->firstChild->prevSibling == NULL);
5045 if (item->lastChild)
5047 assert(item->lastChild->parent == item);
5048 assert(item->lastChild->nextSibling == NULL);
5051 assert(item->nextSibling != item);
5052 if (item->nextSibling)
5054 assert(item->nextSibling->parent == item->parent);
5055 assert(item->nextSibling->prevSibling == item);
5058 assert(item->prevSibling != item);
5059 if (item->prevSibling)
5061 assert(item->prevSibling->parent == item->parent);
5062 assert(item->prevSibling->nextSibling == item);
5067 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5069 assert(item != NULL);
5071 assert(item->parent != NULL);
5072 assert(item->parent != item);
5073 assert(item->iLevel == item->parent->iLevel + 1);
5075 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5077 TREEVIEW_VerifyItemCommon(infoPtr, item);
5079 TREEVIEW_VerifyChildren(infoPtr, item);
5083 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5085 TREEVIEW_ITEM *child;
5086 assert(item != NULL);
5088 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5089 TREEVIEW_VerifyItem(infoPtr, child);
5093 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5095 TREEVIEW_ITEM *root = infoPtr->root;
5097 assert(root != NULL);
5098 assert(root->iLevel == -1);
5099 assert(root->parent == NULL);
5100 assert(root->prevSibling == NULL);
5102 TREEVIEW_VerifyItemCommon(infoPtr, root);
5104 TREEVIEW_VerifyChildren(infoPtr, root);
5108 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5110 assert(infoPtr != NULL);
5112 TREEVIEW_VerifyRoot(infoPtr);