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
70 HWND hwndNotify; /* Owner window to send notifications to */
75 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
76 INT cdmode; /* last custom draw setting */
77 UINT uScrollTime; /* max. time for scrolling in milliseconds */
78 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
80 UINT uItemHeight; /* item height */
83 LONG clientWidth; /* width of control window */
84 LONG clientHeight; /* height of control window */
86 LONG treeWidth; /* width of visible tree items */
87 LONG treeHeight; /* height of visible tree items */
89 UINT uIndent; /* indentation in pixels */
90 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
91 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
92 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
94 HTREEITEM firstVisible; /* handle to first visible item */
96 HTREEITEM dropItem; /* handle to item selected by drag cursor */
97 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
98 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
99 HIMAGELIST dragList; /* Bitmap of dragged item */
104 COLORREF clrInsertMark;
110 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
111 BOOL bIgnoreEditKillFocus;
114 HIMAGELIST himlNormal;
115 int normalImageHeight;
116 int normalImageWidth;
117 HIMAGELIST himlState;
118 int stateImageHeight;
125 /* bitflags for infoPtr->uInternalStatus */
127 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
128 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
129 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
130 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
131 #define TV_RDRAG 0x10 /* dito Rbutton */
132 #define TV_RDRAGGING 0x20
134 /* bitflags for infoPtr->timer */
136 #define TV_EDIT_TIMER 2
137 #define TV_EDIT_TIMER_SET 2
140 VOID TREEVIEW_Register (VOID);
141 VOID TREEVIEW_Unregister (VOID);
144 DEFAULT_DEBUG_CHANNEL(treeview);
147 #define TEXT_CALLBACK_SIZE 260
149 #define TREEVIEW_LEFT_MARGIN 8
151 #define MINIMUM_INDENT 19
153 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
155 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
156 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
157 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
160 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
163 static VOID TREEVIEW_Invalidate(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(infoPtr->hwndNotify, 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(infoPtr->hwndNotify, 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(infoPtr->hwndNotify, 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(infoPtr->hwndNotify, 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(infoPtr->hwndNotify, 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(infoPtr->hwndNotify, 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(infoPtr->hwndNotify, 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 - 1;
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 if (infoPtr->firstVisible == parentItem->firstChild)
1056 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1057 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
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 has 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 *item;
1153 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1155 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1156 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1159 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1161 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1162 TREEVIEW_UpdateScrollBars(infoPtr);
1164 * if the item was inserted in a visible part of the tree,
1165 * invalidate it, as well as those after it
1167 for (item = newItem;
1169 item = TREEVIEW_GetNextListItem(infoPtr, item))
1170 TREEVIEW_Invalidate(infoPtr, item);
1174 newItem->visibleOrder = -1;
1176 /* refresh treeview if newItem is the first item inserted under parentItem */
1177 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1179 /* parent got '+' - update it */
1180 TREEVIEW_Invalidate(infoPtr, parentItem);
1184 return (LRESULT)newItem;
1189 TREEVIEW_InsertItemW(TREEVIEW_INFO *infoPtr, LPARAM lParam)
1191 TVINSERTSTRUCTW *tvisW;
1192 TVINSERTSTRUCTA tvisA;
1195 tvisW = (LPTVINSERTSTRUCTW) lParam;
1197 tvisA.hParent = tvisW->hParent;
1198 tvisA.hInsertAfter = tvisW->hInsertAfter;
1200 tvisA.DUMMYUNIONNAME.item.mask = tvisW->DUMMYUNIONNAME.item.mask;
1201 tvisA.DUMMYUNIONNAME.item.hItem = tvisW->DUMMYUNIONNAME.item.hItem;
1202 tvisA.DUMMYUNIONNAME.item.state = tvisW->DUMMYUNIONNAME.item.state;
1203 tvisA.DUMMYUNIONNAME.item.stateMask = tvisW->DUMMYUNIONNAME.item.stateMask;
1204 tvisA.DUMMYUNIONNAME.item.cchTextMax =
1205 tvisW->DUMMYUNIONNAME.item.cchTextMax;
1207 if (tvisW->DUMMYUNIONNAME.item.pszText)
1209 if (tvisW->DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKW)
1211 int len = lstrlenW(tvisW->DUMMYUNIONNAME.item.pszText) + 1;
1213 tvisA.DUMMYUNIONNAME.item.pszText = COMCTL32_Alloc(len);
1214 lstrcpyWtoA(tvisA.DUMMYUNIONNAME.item.pszText,
1215 tvisW->DUMMYUNIONNAME.item.pszText);
1219 tvisA.DUMMYUNIONNAME.item.pszText = LPSTR_TEXTCALLBACKA;
1220 tvisA.DUMMYUNIONNAME.item.cchTextMax = 0;
1224 tvisA.DUMMYUNIONNAME.item.iImage = tvisW->DUMMYUNIONNAME.item.iImage;
1225 tvisA.DUMMYUNIONNAME.item.iSelectedImage =
1226 tvisW->DUMMYUNIONNAME.item.iSelectedImage;
1227 tvisA.DUMMYUNIONNAME.item.cChildren = tvisW->DUMMYUNIONNAME.item.cChildren;
1228 tvisA.DUMMYUNIONNAME.item.lParam = tvisW->DUMMYUNIONNAME.item.lParam;
1230 lRes = TREEVIEW_InsertItemA(infoPtr, (LPARAM)&tvisA);
1232 if (tvisA.DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKA)
1234 COMCTL32_Free(tvisA.DUMMYUNIONNAME.item.pszText);
1242 /* Item Deletion ************************************************************/
1244 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1247 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1249 TREEVIEW_ITEM *kill = parentItem->firstChild;
1251 while (kill != NULL)
1253 TREEVIEW_ITEM *next = kill->nextSibling;
1255 TREEVIEW_RemoveItem(infoPtr, kill);
1260 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1261 assert(parentItem->firstChild == NULL);
1262 assert(parentItem->lastChild == NULL);
1266 TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1268 TREEVIEW_ITEM *parentItem = item->parent;
1270 assert(item != NULL);
1271 assert(item->parent != NULL); /* i.e. it must not be the root */
1273 if (parentItem->firstChild == item)
1274 parentItem->firstChild = item->nextSibling;
1276 if (parentItem->lastChild == item)
1277 parentItem->lastChild = item->prevSibling;
1279 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1280 && parentItem->cChildren > 0)
1281 parentItem->cChildren = 0;
1283 if (item->prevSibling)
1284 item->prevSibling->nextSibling = item->nextSibling;
1286 if (item->nextSibling)
1287 item->nextSibling->prevSibling = item->prevSibling;
1291 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1293 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1295 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMA,
1296 TVIF_HANDLE | TVIF_PARAM, 0, wineItem, 0);
1298 if (wineItem->firstChild)
1299 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1301 TREEVIEW_UnlinkItem(wineItem);
1303 infoPtr->uNumItems--;
1305 if (wineItem->pszText != LPSTR_TEXTCALLBACKA)
1306 COMCTL32_Free(wineItem->pszText);
1308 TREEVIEW_FreeItem(infoPtr, wineItem);
1312 /* Empty out the tree. */
1314 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1316 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1318 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1322 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1324 TREEVIEW_ITEM *oldSelection = infoPtr->selectedItem;
1325 TREEVIEW_ITEM *newSelection = oldSelection;
1326 TREEVIEW_ITEM *newFirstVisible = NULL;
1327 TREEVIEW_ITEM *parent, *prev = NULL;
1328 BOOL visible = FALSE;
1330 if (wineItem == TVI_ROOT)
1332 TRACE("TVI_ROOT\n");
1333 parent = infoPtr->root;
1334 newSelection = NULL;
1336 TREEVIEW_RemoveTree(infoPtr);
1340 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1343 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1344 parent = wineItem->parent;
1346 if (ISVISIBLE(wineItem))
1348 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1352 if (infoPtr->selectedItem != NULL
1353 && (wineItem == infoPtr->selectedItem
1354 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1356 if (wineItem->nextSibling)
1357 newSelection = wineItem->nextSibling;
1358 else if (wineItem->parent != infoPtr->root)
1359 newSelection = wineItem->parent;
1362 if (infoPtr->firstVisible == wineItem)
1364 if (wineItem->nextSibling)
1365 newFirstVisible = wineItem->nextSibling;
1366 else if (wineItem->prevSibling)
1367 newFirstVisible = wineItem->prevSibling;
1368 else if (wineItem->parent != infoPtr->root)
1369 newFirstVisible = wineItem->parent;
1372 newFirstVisible = infoPtr->firstVisible;
1374 TREEVIEW_RemoveItem(infoPtr, wineItem);
1377 /* Don't change if somebody else already has. */
1378 if (oldSelection == infoPtr->selectedItem)
1380 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1381 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1383 infoPtr->selectedItem = 0;
1386 /* Validate insertMark dropItem.
1387 * hotItem ??? - used for comparison only.
1389 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1390 infoPtr->insertMarkItem = 0;
1392 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1393 infoPtr->dropItem = 0;
1395 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1396 newFirstVisible = infoPtr->root->firstChild;
1398 TREEVIEW_VerifyTree(infoPtr);
1403 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1404 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1405 TREEVIEW_UpdateScrollBars(infoPtr);
1406 TREEVIEW_Invalidate(infoPtr, NULL);
1408 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1410 /* parent lost '+/-' - update it */
1411 TREEVIEW_Invalidate(infoPtr, parent);
1418 /* Get/Set Messages *********************************************************/
1420 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1423 infoPtr->bRedraw = TRUE;
1425 infoPtr->bRedraw = FALSE;
1431 TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1434 return infoPtr->uIndent;
1438 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1442 if (newIndent < MINIMUM_INDENT)
1443 newIndent = MINIMUM_INDENT;
1445 if (infoPtr->uIndent != newIndent)
1447 infoPtr->uIndent = newIndent;
1448 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1449 TREEVIEW_UpdateScrollBars(infoPtr);
1450 TREEVIEW_Invalidate(infoPtr, NULL);
1458 TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1461 return infoPtr->hwndToolTip;
1465 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1470 prevToolTip = infoPtr->hwndToolTip;
1471 infoPtr->hwndToolTip = hwndTT;
1478 TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1480 return infoPtr->uScrollTime;
1484 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1486 UINT uOldScrollTime = infoPtr->uScrollTime;
1488 infoPtr->uScrollTime = min(uScrollTime, 100);
1490 return uOldScrollTime;
1495 TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1501 case (WPARAM)TVSIL_NORMAL:
1502 return (LRESULT)infoPtr->himlNormal;
1504 case (WPARAM)TVSIL_STATE:
1505 return (LRESULT)infoPtr->himlState;
1513 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1515 HIMAGELIST himlOld = 0;
1516 int oldWidth = infoPtr->normalImageWidth;
1517 int oldHeight = infoPtr->normalImageHeight;
1520 TRACE("%x,%p\n", wParam, himlNew);
1524 case (WPARAM)TVSIL_NORMAL:
1525 himlOld = infoPtr->himlNormal;
1526 infoPtr->himlNormal = himlNew;
1528 if (himlNew != NULL)
1529 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1530 &infoPtr->normalImageHeight);
1533 infoPtr->normalImageWidth = 0;
1534 infoPtr->normalImageHeight = 0;
1539 case (WPARAM)TVSIL_STATE:
1540 himlOld = infoPtr->himlState;
1541 infoPtr->himlState = himlNew;
1543 if (himlNew != NULL)
1544 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1545 &infoPtr->stateImageHeight);
1548 infoPtr->stateImageWidth = 0;
1549 infoPtr->stateImageHeight = 0;
1555 if (oldWidth != infoPtr->normalImageWidth ||
1556 oldHeight != infoPtr->normalImageHeight)
1558 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1559 TREEVIEW_UpdateScrollBars(infoPtr);
1562 TREEVIEW_Invalidate(infoPtr, NULL);
1564 return (LRESULT)himlOld;
1567 /* Compute the natural height (based on the font size) for items. */
1569 TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1573 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1575 GetTextMetricsA(hdc, &tm);
1577 SelectObject(hdc, hOldFont);
1580 /* The 16 is a hack because our fonts are tiny. */
1581 /* add 2 for the focus border and 1 more for margin some apps assume */
1582 return max(16, tm.tmHeight + tm.tmExternalLeading + 3);
1586 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1588 INT prevHeight = infoPtr->uItemHeight;
1590 TRACE("%d \n", newHeight);
1591 if (newHeight == -1)
1593 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1594 infoPtr->bHeightSet = FALSE;
1598 infoPtr->uItemHeight = newHeight;
1599 infoPtr->bHeightSet = TRUE;
1602 /* Round down, unless we support odd ("non even") heights. */
1603 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1604 infoPtr->uItemHeight &= ~1;
1606 if (infoPtr->uItemHeight != prevHeight)
1608 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1609 TREEVIEW_UpdateScrollBars(infoPtr);
1610 TREEVIEW_Invalidate(infoPtr, NULL);
1617 TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1620 return infoPtr->uItemHeight;
1625 TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1627 TRACE("%x\n", infoPtr->hFont);
1628 return infoPtr->hFont;
1633 TREEVIEW_ResetTextWidth(LPVOID pItem, DWORD unused)
1637 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1643 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1645 UINT uHeight = infoPtr->uItemHeight;
1647 TRACE("%x %i\n", hFont, bRedraw);
1649 infoPtr->hFont = hFont ? hFont : GetStockObject(SYSTEM_FONT);
1651 DeleteObject(infoPtr->hBoldFont);
1652 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1654 if (!infoPtr->bHeightSet)
1655 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1657 if (uHeight != infoPtr->uItemHeight)
1658 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1660 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1662 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1663 TREEVIEW_UpdateScrollBars(infoPtr);
1666 TREEVIEW_Invalidate(infoPtr, NULL);
1673 TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1676 return (LRESULT)infoPtr->clrLine;
1680 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1682 COLORREF prevColor = infoPtr->clrLine;
1685 infoPtr->clrLine = color;
1686 return (LRESULT)prevColor;
1691 TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1694 return (LRESULT)infoPtr->clrText;
1698 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1700 COLORREF prevColor = infoPtr->clrText;
1703 infoPtr->clrText = color;
1705 if (infoPtr->clrText != prevColor)
1706 TREEVIEW_Invalidate(infoPtr, NULL);
1708 return (LRESULT)prevColor;
1713 TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1716 return (LRESULT)infoPtr->clrBk;
1720 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1722 COLORREF prevColor = infoPtr->clrBk;
1725 infoPtr->clrBk = newColor;
1727 if (newColor != prevColor)
1728 TREEVIEW_Invalidate(infoPtr, NULL);
1730 return (LRESULT)prevColor;
1735 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1738 return (LRESULT)infoPtr->clrInsertMark;
1742 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1744 COLORREF prevColor = infoPtr->clrInsertMark;
1746 TRACE("%lx\n", color);
1747 infoPtr->clrInsertMark = color;
1749 return (LRESULT)prevColor;
1754 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1756 TRACE("%d %p\n", wParam, item);
1758 if (!TREEVIEW_ValidItem(infoPtr, item))
1761 infoPtr->insertBeforeorAfter = wParam;
1762 infoPtr->insertMarkItem = item;
1764 TREEVIEW_Invalidate(infoPtr, NULL);
1770 /************************************************************************
1771 * Some serious braindamage here. lParam is a pointer to both the
1772 * input HTREEITEM and the output RECT.
1775 TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1777 TREEVIEW_ITEM *wineItem;
1778 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1782 * validate parameters
1788 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
1792 * If wParam is TRUE return the text size otherwise return
1793 * the whole item size
1797 /* Windows does not send TVN_GETDISPINFO here. */
1799 lpRect->top = wineItem->rect.top;
1800 lpRect->bottom = wineItem->rect.bottom;
1802 lpRect->left = wineItem->textOffset;
1803 lpRect->right = wineItem->textOffset + wineItem->textWidth;
1807 *lpRect = wineItem->rect;
1810 TRACE("%s [L:%d R:%d T:%d B:%d]\n", fTextRect ? "text" : "item",
1811 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
1816 static inline LRESULT
1817 TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
1819 /* Suprise! This does not take integral height into account. */
1820 return infoPtr->clientHeight / infoPtr->uItemHeight;
1825 TREEVIEW_GetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1827 TREEVIEW_ITEM *wineItem;
1829 wineItem = tvItem->hItem;
1830 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1833 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
1835 if (tvItem->mask & TVIF_CHILDREN)
1836 tvItem->cChildren = wineItem->cChildren;
1838 if (tvItem->mask & TVIF_HANDLE)
1839 tvItem->hItem = wineItem;
1841 if (tvItem->mask & TVIF_IMAGE)
1842 tvItem->iImage = wineItem->iImage;
1844 if (tvItem->mask & TVIF_INTEGRAL)
1845 tvItem->iIntegral = wineItem->iIntegral;
1847 /* undocumented: windows ignores TVIF_PARAM and
1848 * * always sets lParam
1850 tvItem->lParam = wineItem->lParam;
1852 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1853 tvItem->iSelectedImage = wineItem->iSelectedImage;
1855 if (tvItem->mask & TVIF_STATE)
1856 tvItem->state = wineItem->state & tvItem->stateMask;
1858 if (tvItem->mask & TVIF_TEXT)
1859 lstrcpynA(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
1861 TRACE("item <%p>, txt %p, img %p, mask %x\n",
1862 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
1867 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
1868 * which is wrong. */
1870 TREEVIEW_SetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1872 TREEVIEW_ITEM *wineItem;
1873 TREEVIEW_ITEM originalItem;
1875 wineItem = tvItem->hItem;
1877 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
1880 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1883 if (!TREEVIEW_DoSetItem(infoPtr, wineItem, tvItem))
1886 /* store the orignal item values */
1887 originalItem = *wineItem;
1889 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
1890 if ((tvItem->mask & TVIF_TEXT
1891 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
1892 && ISVISIBLE(wineItem))
1894 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
1895 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
1898 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
1900 /* The refresh updates everything, but we can't wait until then. */
1901 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
1903 /* if any of the items values changed, redraw the item */
1904 if(memcmp(&originalItem, wineItem, sizeof(TREEVIEW_ITEM)))
1906 if (tvItem->mask & TVIF_INTEGRAL)
1908 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
1909 TREEVIEW_UpdateScrollBars(infoPtr);
1911 TREEVIEW_Invalidate(infoPtr, NULL);
1915 TREEVIEW_UpdateScrollBars(infoPtr);
1916 TREEVIEW_Invalidate(infoPtr, wineItem);
1925 TREEVIEW_GetItemW(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1927 TREEVIEW_ITEM *wineItem;
1929 iItem = (INT)tvItem->hItem;
1931 wineItem = tvItem->hItem;
1932 if(!TREEVIEW_ValidItem (infoPtr, wineItem))
1935 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
1937 if (tvItem->mask & TVIF_CHILDREN) {
1938 if (TVIF_CHILDREN==I_CHILDRENCALLBACK)
1939 FIXME("I_CHILDRENCALLBACK not supported\n");
1940 tvItem->cChildren = wineItem->cChildren;
1943 if (tvItem->mask & TVIF_HANDLE) {
1944 tvItem->hItem = wineItem;
1946 if (tvItem->mask & TVIF_IMAGE) {
1947 tvItem->iImage = wineItem->iImage;
1949 if (tvItem->mask & TVIF_INTEGRAL) {
1950 tvItem->iIntegral = wineItem->iIntegral;
1952 /* undocumented: windows ignores TVIF_PARAM and
1953 * always sets lParam */
1954 tvItem->lParam = wineItem->lParam;
1955 if (tvItem->mask & TVIF_SELECTEDIMAGE) {
1956 tvItem->iSelectedImage = wineItem->iSelectedImage;
1958 if (tvItem->mask & TVIF_STATE) {
1959 tvItem->state = wineItem->state & tvItem->stateMask;
1962 if (tvItem->mask & TVIF_TEXT) {
1963 if (wineItem->pszText == LPSTR_TEXTCALLBACKW) {
1964 tvItem->pszText = LPSTR_TEXTCALLBACKW; /* FIXME:send notification? */
1965 ERR(" GetItem called with LPSTR_TEXTCALLBACK\n");
1967 else if (wineItem->pszText) {
1968 lstrcpynAtoW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
1972 wineItem->pszText = NULL;
1973 TRACE("item %d<%p>, txt %p, img %p, action %x\n",
1974 iItem, tvItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
1979 TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
1983 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
1986 return (wineItem->state & mask);
1990 TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
1992 TREEVIEW_ITEM *retval;
1996 /* handle all the global data here */
1999 case TVGN_CHILD: /* Special case: child of 0 is root */
2004 retval = infoPtr->root->firstChild;
2008 retval = infoPtr->selectedItem;
2011 case TVGN_FIRSTVISIBLE:
2012 retval = infoPtr->firstVisible;
2015 case TVGN_DROPHILITE:
2016 retval = infoPtr->dropItem;
2019 case TVGN_LASTVISIBLE:
2020 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2026 TRACE("flags:%x, returns %p\n", which, retval);
2027 return (LRESULT)retval;
2030 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2036 retval = wineItem->nextSibling;
2039 retval = wineItem->prevSibling;
2042 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2045 retval = wineItem->firstChild;
2047 case TVGN_NEXTVISIBLE:
2048 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2050 case TVGN_PREVIOUSVISIBLE:
2051 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2054 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2058 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2059 return (LRESULT)retval;
2064 TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2066 TRACE(" %d\n", infoPtr->uNumItems);
2067 return (LRESULT)infoPtr->uNumItems;
2071 TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2073 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2075 static const unsigned int state_table[] = { 0, 2, 1 };
2079 state = STATEIMAGEINDEX(item->state);
2080 TRACE("state:%x\n", state);
2081 item->state &= ~TVIS_STATEIMAGEMASK;
2084 state = state_table[state];
2086 item->state |= INDEXTOSTATEIMAGEMASK(state);
2088 TRACE("state:%x\n", state);
2089 TREEVIEW_Invalidate(infoPtr, item);
2094 /* Painting *************************************************************/
2096 /* Draw the lines and expand button for an item. Also draws one section
2097 * of the line from item's parent to item's parent's next sibling. */
2099 TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2101 LONG centerx, centery;
2102 BOOL lar = ((infoPtr->dwStyle
2103 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2106 if (!lar && item->iLevel == 0)
2109 centerx = (item->linesOffset + item->stateOffset) / 2;
2110 centery = (item->rect.top + item->rect.bottom) / 2;
2112 if (infoPtr->dwStyle & TVS_HASLINES)
2114 HPEN hOldPen, hNewPen;
2118 * Get a dotted grey pen
2120 hNewPen = CreatePen(PS_DOT, 0, infoPtr->clrLine);
2121 hOldPen = SelectObject(hdc, hNewPen);
2123 MoveToEx(hdc, item->stateOffset, centery, NULL);
2124 LineTo(hdc, centerx - 1, centery);
2126 if (item->prevSibling || item->parent != infoPtr->root)
2128 MoveToEx(hdc, centerx, item->rect.top, NULL);
2129 LineTo(hdc, centerx, centery);
2132 if (item->nextSibling)
2134 MoveToEx(hdc, centerx, centery, NULL);
2135 LineTo(hdc, centerx, item->rect.bottom + 1);
2138 /* Draw the line from our parent to its next sibling. */
2139 parent = item->parent;
2140 while (parent != infoPtr->root)
2142 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2144 if (parent->nextSibling
2145 /* skip top-levels unless TVS_LINESATROOT */
2146 && parent->stateOffset > parent->linesOffset)
2148 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2149 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2152 parent = parent->parent;
2155 SelectObject(hdc, hOldPen);
2156 DeleteObject(hNewPen);
2160 * Display the (+/-) signs
2163 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2165 if (item->cChildren)
2167 LONG height = item->rect.bottom - item->rect.top;
2168 LONG width = item->stateOffset - item->linesOffset;
2169 LONG rectsize = min(height, width) / 4;
2170 /* plussize = ceil(rectsize * 3/4) */
2171 LONG plussize = (rectsize + 1) * 3 / 4;
2173 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2174 HPEN hOldPen = SelectObject(hdc, hNewPen);
2175 HBRUSH hbr = CreateSolidBrush(infoPtr->clrBk);
2176 HBRUSH hbrOld = SelectObject(hdc, hbr);
2178 Rectangle(hdc, centerx - rectsize, centery - rectsize,
2179 centerx + rectsize + 1, centery + rectsize + 1);
2181 SelectObject(hdc, hbrOld);
2184 SelectObject(hdc, hOldPen);
2185 DeleteObject(hNewPen);
2187 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2188 LineTo(hdc, centerx + plussize, centery);
2190 if (!(item->state & TVIS_EXPANDED))
2192 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2193 LineTo(hdc, centerx, centery + plussize);
2200 TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2206 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2208 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2210 /* The custom draw handler can query the text rectangle,
2212 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2216 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2218 cditem = TREEVIEW_SendCustomDrawItemNotify
2219 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT);
2220 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2222 if (cditem & CDRF_SKIPDEFAULT)
2224 SelectObject(hdc, hOldFont);
2229 if (cditem & CDRF_NEWFONT)
2230 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2232 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2234 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2237 * Display the images associated with this item
2242 /* State images are displayed to the left of the Normal image
2243 * image number is in state; zero should be `display no image'.
2245 imageIndex = STATEIMAGEINDEX(wineItem->state);
2247 if (infoPtr->himlState && imageIndex)
2249 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2250 wineItem->stateOffset,
2251 centery - infoPtr->stateImageHeight / 2,
2255 /* Now, draw the normal image; can be either selected or
2256 * non-selected image.
2259 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage))
2261 /* The item is curently selected */
2262 imageIndex = wineItem->iSelectedImage;
2266 /* The item is not selected */
2267 imageIndex = wineItem->iImage;
2270 if (infoPtr->himlNormal)
2272 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2274 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2275 wineItem->imageOffset,
2276 centery - infoPtr->normalImageHeight / 2,
2277 ILD_NORMAL | ovlIdx);
2283 * Display the text associated with this item
2286 /* Don't paint item's text if it's being edited */
2287 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2289 if (wineItem->pszText)
2291 COLORREF oldTextColor = 0;
2294 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2297 oldBkMode = SetBkMode(hdc, TRANSPARENT);
2299 /* - If item is drop target or it is selected and window is in focus -
2300 * use blue background (COLOR_HIGHLIGHT).
2301 * - If item is selected, window is not in focus, but it has style
2302 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2303 * - Otherwise - don't fill background
2305 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2306 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2307 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2309 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2311 hbrBk = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
2313 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2317 hbrBk = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
2319 if (infoPtr->clrText == -1)
2321 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2323 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2328 if (infoPtr->clrText == -1)
2330 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2332 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2335 rcText.top = wineItem->rect.top;
2336 rcText.bottom = wineItem->rect.bottom;
2337 rcText.left = wineItem->textOffset;
2338 rcText.right = rcText.left + wineItem->textWidth + 4;
2342 FillRect(hdc, &rcText, hbrBk);
2343 DeleteObject(hbrBk);
2346 /* Draw the box arround the selected item */
2347 if ((wineItem == infoPtr->selectedItem) && inFocus)
2349 HPEN hNewPen = CreatePen(PS_DOT, 0,
2350 GetSysColor(COLOR_WINDOWTEXT));
2351 HPEN hOldPen = SelectObject(hdc, hNewPen);
2352 INT rop = SetROP2(hdc, R2_XORPEN);
2355 points[4].x = points[0].x = rcText.left;
2356 points[4].y = points[0].y = rcText.top;
2357 points[1].x = rcText.right - 1;
2358 points[1].y = rcText.top;
2359 points[2].x = rcText.right - 1;
2360 points[2].y = rcText.bottom - 1;
2361 points[3].x = rcText.left;
2362 points[3].y = rcText.bottom - 1;
2364 Polyline(hdc, points, 5);
2367 SelectObject(hdc, hOldPen);
2368 DeleteObject(hNewPen);
2371 InflateRect(&rcText, -2, -1); /* allow for the focus rect */
2376 lstrlenA(wineItem->pszText),
2378 DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
2380 /* Restore the hdc state */
2381 SetTextColor(hdc, oldTextColor);
2383 if (oldBkMode != TRANSPARENT)
2384 SetBkMode(hdc, oldBkMode);
2388 /* Draw insertion mark if necessary */
2390 if (infoPtr->insertMarkItem)
2391 TRACE("item:%d,mark:%d\n",
2392 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2393 (int)infoPtr->insertMarkItem);
2395 if (wineItem == infoPtr->insertMarkItem)
2397 HPEN hNewPen, hOldPen;
2401 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2402 hOldPen = SelectObject(hdc, hNewPen);
2404 if (infoPtr->insertBeforeorAfter)
2405 offset = wineItem->rect.bottom - 1;
2407 offset = wineItem->rect.top + 1;
2409 left = wineItem->textOffset - 2;
2410 right = wineItem->textOffset + wineItem->textWidth + 2;
2412 MoveToEx(hdc, left, offset - 3, NULL);
2413 LineTo(hdc, left, offset + 4);
2415 MoveToEx(hdc, left, offset, NULL);
2416 LineTo(hdc, right + 1, offset);
2418 MoveToEx(hdc, right, offset + 3, NULL);
2419 LineTo(hdc, right, offset - 4);
2421 SelectObject(hdc, hOldPen);
2422 DeleteObject(hNewPen);
2425 if (cditem & CDRF_NOTIFYPOSTPAINT)
2427 cditem = TREEVIEW_SendCustomDrawItemNotify
2428 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT);
2429 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2432 SelectObject(hdc, hOldFont);
2435 /* Computes treeHeight and treeWidth and updates the scroll bars.
2438 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2440 TREEVIEW_ITEM *wineItem;
2441 HWND hwnd = infoPtr->hwnd;
2445 LONG scrollX = infoPtr->scrollX;
2447 infoPtr->treeWidth = 0;
2448 infoPtr->treeHeight = 0;
2450 /* We iterate through all visible items in order to get the tree height
2452 wineItem = infoPtr->root->firstChild;
2454 while (wineItem != NULL)
2456 if (ISVISIBLE(wineItem))
2458 /* actually we draw text at textOffset + 2 */
2459 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2460 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2462 /* This is scroll-adjusted, but we fix this below. */
2463 infoPtr->treeHeight = wineItem->rect.bottom;
2466 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2469 /* Fix the scroll adjusted treeHeight and treeWidth. */
2470 if (infoPtr->root->firstChild)
2471 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2473 infoPtr->treeWidth += infoPtr->scrollX;
2475 /* Adding one scroll bar may take up enough space that it forces us
2476 * to add the other as well. */
2477 if (infoPtr->treeHeight > infoPtr->clientHeight)
2481 if (infoPtr->treeWidth
2482 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2485 else if (infoPtr->treeWidth > infoPtr->clientWidth)
2488 if (!vert && horz && infoPtr->treeHeight
2489 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2492 si.cbSize = sizeof(SCROLLINFO);
2493 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2498 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2499 si.nPos = infoPtr->firstVisible->visibleOrder;
2500 si.nMax = infoPtr->maxVisibleOrder - 1;
2502 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2504 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2505 ShowScrollBar(hwnd, SB_VERT, TRUE);
2506 infoPtr->uInternalStatus |= TV_VSCROLL;
2510 if (infoPtr->uInternalStatus & TV_VSCROLL)
2511 ShowScrollBar(hwnd, SB_VERT, FALSE);
2512 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2517 si.nPage = infoPtr->clientWidth;
2518 si.nPos = infoPtr->scrollX;
2519 si.nMax = infoPtr->treeWidth - 1;
2521 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2523 si.nPos = si.nMax - max( si.nPage-1, 0 );
2527 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2528 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2529 infoPtr->uInternalStatus |= TV_HSCROLL;
2531 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2535 if (infoPtr->uInternalStatus & TV_HSCROLL)
2536 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2537 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2542 if (infoPtr->scrollX != scrollX)
2544 TREEVIEW_HScroll(infoPtr,
2545 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2549 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2552 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2554 TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2556 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2559 GetClientRect(infoPtr->hwnd, &rect);
2560 FillRect(hDC, &rect, hBrush);
2561 DeleteObject(hBrush);
2567 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2569 HWND hwnd = infoPtr->hwnd;
2571 TREEVIEW_ITEM *wineItem;
2573 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2575 TRACE("empty window\n");
2579 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2582 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2584 ReleaseDC(hwnd, hdc);
2588 for (wineItem = infoPtr->root->firstChild;
2590 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2592 if (ISVISIBLE(wineItem))
2594 /* Avoid unneeded calculations */
2595 if (wineItem->rect.top > rect.bottom)
2597 if (wineItem->rect.bottom < rect.top)
2600 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2604 TREEVIEW_UpdateScrollBars(infoPtr);
2606 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2608 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2612 TREEVIEW_Invalidate(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2615 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2617 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2621 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2632 GetUpdateRect(infoPtr->hwnd, &rc, TRUE);
2636 hdc = BeginPaint(infoPtr->hwnd, &ps);
2640 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2641 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2644 EndPaint(infoPtr->hwnd, &ps);
2650 /* Sorting **************************************************************/
2652 /***************************************************************************
2653 * Forward the DPA local callback to the treeview owner callback
2656 TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2658 /* Forward the call to the client-defined callback */
2659 return pCallBackSort->lpfnCompare(first->lParam,
2661 pCallBackSort->lParam);
2664 /***************************************************************************
2665 * Treeview native sort routine: sort on item text.
2668 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2669 TREEVIEW_INFO *infoPtr)
2671 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2672 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2674 return strcasecmp(first->pszText, second->pszText);
2677 /* Returns the number of physical children belonging to item. */
2679 TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2684 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2690 /* Returns a DPA containing a pointer to each physical child of item in
2691 * sibling order. If item has no children, an empty DPA is returned. */
2693 TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2695 HTREEITEM child = item->firstChild;
2697 HDPA list = DPA_Create(8);
2698 if (list == 0) return NULL;
2700 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2702 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2712 /***************************************************************************
2713 * Setup the treeview structure with regards of the sort method
2714 * and sort the children of the TV item specified in lParam
2715 * fRecurse: currently unused. Should be zero.
2716 * parent: if pSort!=NULL, should equal pSort->hParent.
2717 * otherwise, item which child items are to be sorted.
2718 * pSort: sort method info. if NULL, sort on item text.
2719 * if non-NULL, sort on item's lParam content, and let the
2720 * application decide what that means. See also TVM_SORTCHILDRENCB.
2724 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2728 PFNDPACOMPARE pfnCompare;
2731 /* undocumented feature: TVI_ROOT means `sort the whole tree' */
2732 if (parent == TVI_ROOT)
2733 parent = infoPtr->root;
2735 /* Check for a valid handle to the parent item */
2736 if (!TREEVIEW_ValidItem(infoPtr, parent))
2738 ERR("invalid item hParent=%x\n", (INT)parent);
2744 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2745 lpCompare = (LPARAM)pSort;
2749 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2750 lpCompare = (LPARAM)infoPtr;
2753 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2755 /* Make sure there is something to sort */
2758 /* TREEVIEW_ITEM rechaining */
2761 HTREEITEM nextItem = 0;
2762 HTREEITEM prevItem = 0;
2764 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
2766 if (sortList == NULL)
2769 /* let DPA sort the list */
2770 DPA_Sort(sortList, pfnCompare, lpCompare);
2772 /* The order of DPA entries has been changed, so fixup the
2773 * nextSibling and prevSibling pointers. */
2775 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
2776 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
2778 /* link the two current item toghether */
2779 item->nextSibling = nextItem;
2780 nextItem->prevSibling = item;
2782 if (prevItem == NULL)
2784 /* this is the first item, update the parent */
2785 parent->firstChild = item;
2786 item->prevSibling = NULL;
2790 /* fix the back chaining */
2791 item->prevSibling = prevItem;
2794 /* get ready for the next one */
2799 /* the last item is pointed to by item and never has a sibling */
2800 item->nextSibling = NULL;
2801 parent->lastChild = item;
2803 DPA_Destroy(sortList);
2805 TREEVIEW_VerifyTree(infoPtr);
2807 if (parent->state & TVIS_EXPANDED)
2809 int visOrder = infoPtr->firstVisible->visibleOrder;
2811 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
2813 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
2815 TREEVIEW_ITEM *item;
2817 for (item = infoPtr->root->firstChild; item != NULL;
2818 item = TREEVIEW_GetNextListItem(infoPtr, item))
2820 if (item->visibleOrder == visOrder)
2824 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
2827 TREEVIEW_Invalidate(infoPtr, NULL);
2836 /***************************************************************************
2837 * Setup the treeview structure with regards of the sort method
2838 * and sort the children of the TV item specified in lParam
2841 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
2843 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
2847 /***************************************************************************
2848 * Sort the children of the TV item specified in lParam.
2851 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2853 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
2857 /* Expansion/Collapse ***************************************************/
2860 TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2863 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGA, action,
2864 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
2865 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
2870 TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2873 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDA, action,
2874 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
2875 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
2880 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
2881 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
2883 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2884 BOOL bRemoveChildren, BOOL bUser)
2886 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
2887 BOOL bSetSelection, bSetFirstVisible;
2889 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
2891 if (!(wineItem->state & TVIS_EXPANDED) || wineItem->firstChild == NULL)
2895 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
2897 wineItem->state &= ~TVIS_EXPANDED;
2900 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
2902 bSetSelection = (infoPtr->selectedItem != NULL
2903 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
2905 bSetFirstVisible = (infoPtr->firstVisible != NULL
2906 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
2908 if (bRemoveChildren)
2910 TRACE("TVE_COLLAPSERESET\n");
2911 wineItem->state &= ~TVIS_EXPANDEDONCE;
2912 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
2915 if (wineItem->firstChild)
2917 TREEVIEW_ITEM *item, *sibling;
2919 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2921 for (item = wineItem->firstChild; item != sibling;
2922 item = TREEVIEW_GetNextListItem(infoPtr, item))
2924 item->visibleOrder = -1;
2928 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2930 TREEVIEW_SetFirstVisible(infoPtr, bSetFirstVisible ? wineItem
2931 : infoPtr->firstVisible, TRUE);
2935 /* Don't call DoSelectItem, it sends notifications. */
2936 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
2937 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
2938 wineItem->state |= TVIS_SELECTED;
2939 infoPtr->selectedItem = wineItem;
2941 TREEVIEW_EnsureVisible(infoPtr, wineItem, FALSE);
2944 TREEVIEW_UpdateScrollBars(infoPtr);
2945 TREEVIEW_Invalidate(infoPtr, NULL);
2951 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2952 BOOL bExpandPartial, BOOL bUser)
2956 if (!TREEVIEW_HasChildren(infoPtr, wineItem)
2957 || wineItem->state & TVIS_EXPANDED)
2960 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
2962 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
2964 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
2966 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
2970 wineItem->state |= TVIS_EXPANDED;
2971 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
2972 wineItem->state |= TVIS_EXPANDEDONCE;
2976 /* this item has already been expanded */
2977 wineItem->state |= TVIS_EXPANDED;
2981 FIXME("TVE_EXPANDPARTIAL not implemented\n");
2983 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2984 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
2985 TREEVIEW_UpdateScrollBars(infoPtr);
2987 /* Scroll up so that as many children as possible are visible.
2988 * This looses when expanding causes an HScroll bar to appear, but we
2989 * don't know that yet, so the last item is obscured. */
2990 if (wineItem->firstChild != NULL)
2992 int nChildren = wineItem->lastChild->visibleOrder
2993 - wineItem->firstChild->visibleOrder + 1;
2995 int visible_pos = wineItem->visibleOrder
2996 - infoPtr->firstVisible->visibleOrder;
2998 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3000 if (visible_pos > 0 && nChildren > rows_below)
3002 int scroll = nChildren - rows_below;
3004 if (scroll > visible_pos)
3005 scroll = visible_pos;
3009 TREEVIEW_ITEM *newFirstVisible
3010 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3014 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3019 TREEVIEW_Invalidate(infoPtr, NULL);
3025 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3029 if (wineItem->state & TVIS_EXPANDED)
3030 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3032 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3036 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3038 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3040 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3042 if (TREEVIEW_HasChildren(infoPtr, item))
3043 TREEVIEW_ExpandAll(infoPtr, item);
3047 /* Note:If the specified item is the child of a collapsed parent item,
3048 the parent's list of child items is (recursively) expanded to reveal the
3049 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3050 know if it also applies here.
3054 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3056 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3059 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3060 TREEVIEW_ItemName(wineItem), flag,
3061 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3063 switch (flag & TVE_TOGGLE)
3066 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3070 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3074 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3081 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3085 /* Hit-Testing **********************************************************/
3087 static TREEVIEW_ITEM *
3088 TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3090 TREEVIEW_ITEM *wineItem;
3093 if (!infoPtr->firstVisible)
3096 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3098 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3099 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3101 if (row >= wineItem->visibleOrder
3102 && row < wineItem->visibleOrder + wineItem->iIntegral)
3110 TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3112 TREEVIEW_ITEM *wineItem;
3118 GetClientRect(infoPtr->hwnd, &rect);
3125 status |= TVHT_TOLEFT;
3127 else if (x > rect.right)
3129 status |= TVHT_TORIGHT;
3134 status |= TVHT_ABOVE;
3136 else if (y > rect.bottom)
3138 status |= TVHT_BELOW;
3143 lpht->flags = status;
3144 return (LRESULT)(HTREEITEM)NULL;
3147 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3150 lpht->flags = TVHT_NOWHERE;
3151 return (LRESULT)(HTREEITEM)NULL;
3154 if (x >= wineItem->textOffset + wineItem->textWidth)
3156 lpht->flags = TVHT_ONITEMRIGHT;
3158 else if (x >= wineItem->textOffset)
3160 lpht->flags = TVHT_ONITEMLABEL;
3162 else if (x >= wineItem->imageOffset)
3164 lpht->flags = TVHT_ONITEMICON;
3166 else if (x >= wineItem->stateOffset)
3168 lpht->flags = TVHT_ONITEMSTATEICON;
3170 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3172 lpht->flags = TVHT_ONITEMBUTTON;
3176 lpht->flags = TVHT_ONITEMINDENT;
3179 lpht->hItem = wineItem;
3180 TRACE("(%ld,%ld):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3182 return (LRESULT)wineItem;
3185 /* Item Label Editing ***************************************************/
3188 TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3190 return infoPtr->hwndEdit;
3193 static LRESULT CALLBACK
3194 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3196 TREEVIEW_INFO *infoPtr;
3197 BOOL bCancel = FALSE;
3204 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3206 TRACE("WM_PAINT start\n");
3207 rc = CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3209 TRACE("WM_PAINT done\n");
3215 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3216 if (infoPtr->bIgnoreEditKillFocus)
3223 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3226 if (wParam == (WPARAM)VK_ESCAPE)
3231 else if (wParam == (WPARAM)VK_RETURN)
3239 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3241 return CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3246 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3247 /* eg. Using a messagebox */
3249 infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3250 infoPtr->bIgnoreEditKillFocus = TRUE;
3251 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3252 infoPtr->bIgnoreEditKillFocus = FALSE;
3258 /* should handle edit control messages here */
3261 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3263 TRACE("%x %ld\n", wParam, lParam);
3265 switch (HIWORD(wParam))
3270 * Adjust the edit window size
3273 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3274 HDC hdc = GetDC(infoPtr->hwndEdit);
3277 HFONT hFont, hOldFont = 0;
3279 infoPtr->bLabelChanged = TRUE;
3281 len = GetWindowTextA(infoPtr->hwndEdit, buffer, sizeof(buffer));
3283 /* Select font to get the right dimension of the string */
3284 hFont = SendMessageA(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3287 hOldFont = SelectObject(hdc, hFont);
3290 if (GetTextExtentPoint32A(hdc, buffer, strlen(buffer), &sz))
3292 TEXTMETRICA textMetric;
3294 /* Add Extra spacing for the next character */
3295 GetTextMetricsA(hdc, &textMetric);
3296 sz.cx += (textMetric.tmMaxCharWidth * 2);
3298 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3300 infoPtr->clientWidth - editItem->textOffset + 2);
3302 SetWindowPos(infoPtr->hwndEdit,
3307 editItem->rect.bottom - editItem->rect.top + 3,
3308 SWP_NOMOVE | SWP_DRAWFRAME);
3313 SelectObject(hdc, hOldFont);
3316 ReleaseDC(infoPtr->hwnd, hdc);
3321 return SendMessageA(GetParent(infoPtr->hwnd), WM_COMMAND, wParam, lParam);
3328 TREEVIEW_EditLabelA(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3330 HWND hwnd = infoPtr->hwnd;
3333 TREEVIEW_ITEM *editItem = hItem;
3334 HINSTANCE hinst = GetWindowLongA(hwnd, GWL_HINSTANCE);
3337 TEXTMETRICA textMetric;
3339 TRACE("%x %p\n", (unsigned)hwnd, hItem);
3340 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3343 if (infoPtr->hwndEdit)
3344 return infoPtr->hwndEdit;
3346 infoPtr->bLabelChanged = FALSE;
3348 /* Make sure that edit item is selected */
3349 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3350 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3352 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3355 /* Select the font to get appropriate metric dimensions */
3356 if (infoPtr->hFont != 0)
3358 hOldFont = SelectObject(hdc, infoPtr->hFont);
3361 /* Get string length in pixels */
3362 GetTextExtentPoint32A(hdc, editItem->pszText, strlen(editItem->pszText),
3365 /* Add Extra spacing for the next character */
3366 GetTextMetricsA(hdc, &textMetric);
3367 sz.cx += (textMetric.tmMaxCharWidth * 2);
3369 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3370 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3372 if (infoPtr->hFont != 0)
3374 SelectObject(hdc, hOldFont);
3377 ReleaseDC(hwnd, hdc);
3378 hwndEdit = CreateWindowExA(WS_EX_LEFT,
3381 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3382 WS_CLIPSIBLINGS | ES_WANTRETURN |
3383 ES_LEFT, editItem->textOffset - 2,
3384 editItem->rect.top - 1, sz.cx + 3,
3385 editItem->rect.bottom -
3386 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3387 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3389 infoPtr->hwndEdit = hwndEdit;
3391 /* Get a 2D border. */
3392 SetWindowLongA(hwndEdit, GWL_EXSTYLE,
3393 GetWindowLongA(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3394 SetWindowLongA(hwndEdit, GWL_STYLE,
3395 GetWindowLongA(hwndEdit, GWL_STYLE) | WS_BORDER);
3397 SendMessageA(hwndEdit, WM_SETFONT, TREEVIEW_FontForItem(infoPtr, editItem),
3400 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongA(hwndEdit, GWL_WNDPROC,
3402 TREEVIEW_Edit_SubclassProc);
3404 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3406 DestroyWindow(hwndEdit);
3407 infoPtr->hwndEdit = 0;
3411 infoPtr->selectedItem = hItem;
3412 SetWindowTextA(hwndEdit, editItem->pszText);
3414 SendMessageA(hwndEdit, EM_SETSEL, 0, -1);
3415 ShowWindow(hwndEdit, SW_SHOW);
3422 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3424 HWND hwnd = infoPtr->hwnd;
3425 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3428 char tmpText[1024] = { '\0' };
3431 if (!infoPtr->hwndEdit)
3434 tvdi.hdr.hwndFrom = hwnd;
3435 tvdi.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
3436 tvdi.hdr.code = TVN_ENDLABELEDITA;
3438 tvdi.item.hItem = editedItem;
3439 tvdi.item.state = editedItem->state;
3440 tvdi.item.lParam = editedItem->lParam;
3444 iLength = GetWindowTextA(infoPtr->hwndEdit, tmpText, 1023);
3446 if (iLength >= 1023)
3448 ERR("Insuficient space to retrieve new item label.");
3451 tvdi.item.pszText = tmpText;
3452 tvdi.item.cchTextMax = iLength + 1;
3456 tvdi.item.pszText = NULL;
3457 tvdi.item.cchTextMax = 0;
3460 bCommit = (BOOL)SendMessageA(infoPtr->hwndNotify,
3462 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3464 if (!bCancel && bCommit) /* Apply the changes */
3466 if (strcmp(tmpText, editedItem->pszText) != 0)
3468 if (NULL == COMCTL32_ReAlloc(editedItem->pszText, iLength + 1))
3470 ERR("OutOfMemory, cannot allocate space for label");
3471 DestroyWindow(infoPtr->hwndEdit);
3472 infoPtr->hwndEdit = 0;
3477 editedItem->cchTextMax = iLength + 1;
3478 lstrcpyA(editedItem->pszText, tmpText);
3483 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3484 DestroyWindow(infoPtr->hwndEdit);
3485 infoPtr->hwndEdit = 0;
3490 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3492 if (wParam != TV_EDIT_TIMER)
3494 ERR("got unknown timer\n");
3498 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3499 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3501 TREEVIEW_EditLabelA(infoPtr, infoPtr->selectedItem);
3507 /* Mouse Tracking/Drag **************************************************/
3509 /***************************************************************************
3510 * This is quite unusual piece of code, but that's how it's implemented in
3514 TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3516 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3517 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3521 r.top = pt.y - cyDrag;
3522 r.left = pt.x - cxDrag;
3523 r.bottom = pt.y + cyDrag;
3524 r.right = pt.x + cxDrag;
3526 SetCapture(infoPtr->hwnd);
3530 if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3532 if (msg.message == WM_MOUSEMOVE)
3534 pt.x = SLOWORD(msg.lParam);
3535 pt.y = SHIWORD(msg.lParam);
3536 if (PtInRect(&r, pt))
3544 else if (msg.message >= WM_LBUTTONDOWN &&
3545 msg.message <= WM_RBUTTONDBLCLK)
3547 if (msg.message == WM_RBUTTONUP)
3548 TREEVIEW_RButtonUp(infoPtr, &pt);
3552 DispatchMessageA(&msg);
3555 if (GetCapture() != infoPtr->hwnd)
3565 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3567 TREEVIEW_ITEM *wineItem;
3571 SetFocus(infoPtr->hwnd);
3573 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3575 /* If there is pending 'edit label' event - kill it now */
3576 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3579 hit.pt.x = SLOWORD(lParam);
3580 hit.pt.y = SHIWORD(lParam);
3582 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3585 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3587 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3591 case TVHT_ONITEMRIGHT:
3592 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3595 case TVHT_ONITEMINDENT:
3596 if (!(infoPtr->dwStyle & TVS_HASLINES))
3602 int level = hit.pt.x / infoPtr->uIndent;
3603 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3605 while (wineItem->iLevel > level)
3607 wineItem = wineItem->parent;
3613 case TVHT_ONITEMLABEL:
3614 case TVHT_ONITEMICON:
3615 case TVHT_ONITEMBUTTON:
3616 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3619 case TVHT_ONITEMSTATEICON:
3620 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3621 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3623 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3632 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3634 HWND hwnd = infoPtr->hwnd;
3639 /* If Edit control is active - kill it and return.
3640 * The best way to do it is to set focus to itself.
3641 * Edit control subclassed procedure will automatically call
3644 if (infoPtr->hwndEdit)
3650 ht.pt.x = SLOWORD(lParam);
3651 ht.pt.y = SHIWORD(lParam);
3653 TREEVIEW_HitTest(infoPtr, &ht);
3654 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3656 /* update focusedItem and redraw both items */
3657 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3659 infoPtr->focusedItem = ht.hItem;
3660 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3662 if(infoPtr->selectedItem)
3663 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3666 bTrack = (ht.flags & TVHT_ONITEM)
3667 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3669 /* Send NM_CLICK right away */
3671 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3674 if (ht.flags & TVHT_ONITEMBUTTON)
3676 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
3680 { /* if TREEVIEW_TrackMouse == 1 dragging occured and the cursor left the dragged item's rectangle */
3681 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3683 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGA, ht.hItem,
3685 infoPtr->dropItem = ht.hItem;
3687 /* clean up focusedItem as we dragged and won't select this item */
3688 if(infoPtr->focusedItem)
3690 /* refresh the item that was focused */
3691 tempItem = infoPtr->focusedItem;
3692 infoPtr->focusedItem = 0;
3693 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
3695 /* refresh the selected item to return the filled background */
3696 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
3703 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3707 * If the style allows editing and the node is already selected
3708 * and the click occured on the item label...
3710 if ((infoPtr->dwStyle & TVS_EDITLABELS) &&
3711 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem))
3713 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3714 KillTimer(hwnd, TV_EDIT_TIMER);
3716 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
3717 infoPtr->Timer |= TV_EDIT_TIMER_SET;
3719 else if (ht.flags & TVHT_ONITEM) /* select the item if the hit was inside of the icon or text */
3722 * if we are TVS_SINGLEEXPAND then we want this single click to
3723 * do a bunch of things.
3725 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
3726 (infoPtr->hwndEdit == 0))
3728 TREEVIEW_ITEM *SelItem;
3731 * Send the notification
3733 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVIF_HANDLE | TVIF_PARAM,
3737 * Close the previous selection all the way to the root
3738 * as long as the new selection is not a child
3740 if((infoPtr->selectedItem)
3741 && (infoPtr->selectedItem != ht.hItem))
3743 BOOL closeit = TRUE;
3746 /* determine if the hitItem is a child of the currently selected item */
3747 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3749 closeit = (SelItem != infoPtr->selectedItem);
3750 SelItem = SelItem->parent;
3755 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3756 SelItem = infoPtr->selectedItem;
3758 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3760 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3761 SelItem = SelItem->parent;
3767 * Expand the current item
3769 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
3772 /* Select the current item */
3773 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
3775 else if (ht.flags & TVHT_ONITEMSTATEICON)
3777 /* TVS_CHECKBOXES requires us to toggle the current state */
3778 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3779 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
3789 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3793 if (infoPtr->hwndEdit)
3795 SetFocus(infoPtr->hwnd);
3799 ht.pt.x = SLOWORD(lParam);
3800 ht.pt.y = SHIWORD(lParam);
3802 TREEVIEW_HitTest(infoPtr, &ht);
3804 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3808 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGA, ht.hItem,
3810 infoPtr->dropItem = ht.hItem;
3815 SetFocus(infoPtr->hwnd);
3816 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
3823 TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
3830 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3832 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
3836 HBITMAP hbmp, hOldbmp;
3843 if (!(infoPtr->himlNormal))
3846 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
3849 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
3851 hwtop = GetDesktopWindow();
3852 htopdc = GetDC(hwtop);
3853 hdc = CreateCompatibleDC(htopdc);
3855 hOldFont = SelectObject(hdc, infoPtr->hFont);
3856 GetTextExtentPoint32A(hdc, dragItem->pszText, lstrlenA(dragItem->pszText),
3858 TRACE("%ld %ld %s %d\n", size.cx, size.cy, dragItem->pszText,
3859 lstrlenA(dragItem->pszText));
3860 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
3861 hOldbmp = SelectObject(hdc, hbmp);
3863 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
3868 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
3869 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
3873 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
3874 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
3877 /* draw item text */
3879 SetRect(&rc, cx, 0, size.cx, size.cy);
3880 DrawTextA(hdc, dragItem->pszText, lstrlenA(dragItem->pszText), &rc,
3882 SelectObject(hdc, hOldFont);
3883 SelectObject(hdc, hOldbmp);
3885 ImageList_Add(infoPtr->dragList, hbmp, 0);
3889 ReleaseDC(hwtop, htopdc);
3891 return (LRESULT)infoPtr->dragList;
3894 /* Selection ************************************************************/
3897 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
3900 TREEVIEW_ITEM *prevSelect;
3903 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
3905 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
3906 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
3907 newSelect ? newSelect->state : 0);
3909 /* reset and redraw focusedItem if focusedItem was set so we don't */
3910 /* have to worry about the previously focused item when we set a new one */
3911 if(infoPtr->focusedItem)
3913 rcFocused = (infoPtr->focusedItem)->rect;
3914 infoPtr->focusedItem = 0;
3915 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
3921 prevSelect = infoPtr->selectedItem;
3923 if (prevSelect == newSelect)
3926 if (TREEVIEW_SendTreeviewNotify(infoPtr,
3929 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
3935 prevSelect->state &= ~TVIS_SELECTED;
3937 newSelect->state |= TVIS_SELECTED;
3939 infoPtr->selectedItem = newSelect;
3941 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
3943 TREEVIEW_SendTreeviewNotify(infoPtr,
3946 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
3949 TREEVIEW_Invalidate(infoPtr, prevSelect);
3950 TREEVIEW_Invalidate(infoPtr, newSelect);
3953 case TVGN_DROPHILITE:
3954 prevSelect = infoPtr->dropItem;
3957 prevSelect->state &= ~TVIS_DROPHILITED;
3959 infoPtr->dropItem = newSelect;
3962 newSelect->state |= TVIS_DROPHILITED;
3964 TREEVIEW_Invalidate(infoPtr, prevSelect);
3965 TREEVIEW_Invalidate(infoPtr, newSelect);
3968 case TVGN_FIRSTVISIBLE:
3969 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
3970 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
3971 TREEVIEW_Invalidate(infoPtr, NULL);
3975 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
3979 /* FIXME: handle NM_KILLFOCUS etc */
3981 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
3983 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
3986 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
3988 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
3994 /* Scrolling ************************************************************/
3997 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
3999 HTREEITEM newFirstVisible = NULL;
4002 if (!TREEVIEW_ValidItem(infoPtr, item))
4005 if (!ISVISIBLE(item))
4007 /* Expand parents as necessary. */
4010 /* see if we are trying to ensure that root is vislble */
4011 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4012 parent = item->parent;
4014 parent = item; /* this item is the topmost item */
4016 while (parent != infoPtr->root)
4018 if (!(parent->state & TVIS_EXPANDED))
4019 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4021 parent = parent->parent;
4025 TRACE("%p (%s) %ld - %ld\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4026 infoPtr->firstVisible->visibleOrder);
4028 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4030 if (visible_pos < 0)
4032 /* item is before the start of the list: put it at the top. */
4033 newFirstVisible = item;
4035 else if (visible_pos >= TREEVIEW_GetVisibleCount(infoPtr)
4036 /* Sometimes, before we are displayed, GVC is 0, causing us to
4037 * spuriously scroll up. */
4040 /* item is past the end of the list. */
4041 int scroll = visible_pos - TREEVIEW_GetVisibleCount(infoPtr);
4043 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4049 /* Scroll window so item's text is visible as much as possible */
4050 /* Calculation of amount of extra space is taken from EditLabel code */
4052 TEXTMETRICA textMetric;
4053 HDC hdc = GetWindowDC(infoPtr->hwnd);
4055 x = item->textWidth;
4057 GetTextMetricsA(hdc, &textMetric);
4058 ReleaseDC(infoPtr->hwnd, hdc);
4060 x += (textMetric.tmMaxCharWidth * 2);
4061 x = max(x, textMetric.tmMaxCharWidth * 3);
4063 if (item->textOffset < 0)
4064 pos = item->textOffset;
4065 else if (item->textOffset + x > infoPtr->clientWidth)
4067 if (x > infoPtr->clientWidth)
4068 pos = item->textOffset;
4070 pos = item->textOffset + x - infoPtr->clientWidth;
4075 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4078 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4080 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4089 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4090 TREEVIEW_ITEM *newFirstVisible,
4091 BOOL bUpdateScrollPos)
4095 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4097 if (newFirstVisible != NULL)
4099 /* Prevent an empty gap from appearing at the bottom... */
4100 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4101 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4105 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4108 /* ... unless we just don't have enough items. */
4109 if (newFirstVisible == NULL)
4110 newFirstVisible = infoPtr->root->firstChild;
4114 if (infoPtr->firstVisible != newFirstVisible)
4116 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4118 infoPtr->firstVisible = newFirstVisible;
4119 TREEVIEW_Invalidate(infoPtr, NULL);
4123 TREEVIEW_ITEM *item;
4124 int scroll = infoPtr->uItemHeight *
4125 (infoPtr->firstVisible->visibleOrder
4126 - newFirstVisible->visibleOrder);
4128 infoPtr->firstVisible = newFirstVisible;
4130 for (item = infoPtr->root->firstChild; item != NULL;
4131 item = TREEVIEW_GetNextListItem(infoPtr, item))
4133 item->rect.top += scroll;
4134 item->rect.bottom += scroll;
4137 if (bUpdateScrollPos)
4138 SetScrollPos(infoPtr->hwnd, SB_VERT,
4139 newFirstVisible->visibleOrder, TRUE);
4141 ScrollWindow(infoPtr->hwnd, 0, scroll, NULL, NULL);
4142 UpdateWindow(infoPtr->hwnd);
4147 /************************************************************************
4148 * VScroll is always in units of visible items. i.e. we always have a
4149 * visible item aligned to the top of the control. (Unless we have no
4153 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4155 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4156 TREEVIEW_ITEM *newFirstVisible = NULL;
4158 int nScrollCode = LOWORD(wParam);
4160 TRACE("wp %x\n", wParam);
4162 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4165 if (infoPtr->hwndEdit)
4166 SetFocus(infoPtr->hwnd);
4168 if (!oldFirstVisible)
4170 assert(infoPtr->root->firstChild == NULL);
4174 switch (nScrollCode)
4177 newFirstVisible = infoPtr->root->firstChild;
4181 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4185 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4189 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4193 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4194 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4198 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4199 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4203 case SB_THUMBPOSITION:
4204 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4205 infoPtr->root->firstChild,
4206 (LONG)(SHORT)HIWORD(wParam));
4213 if (newFirstVisible != NULL)
4215 if (newFirstVisible != oldFirstVisible)
4216 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4217 nScrollCode != SB_THUMBTRACK);
4218 else if (nScrollCode == SB_THUMBPOSITION)
4219 SetScrollPos(infoPtr->hwnd, SB_VERT,
4220 newFirstVisible->visibleOrder, TRUE);
4227 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4230 int scrollX = infoPtr->scrollX;
4231 int nScrollCode = LOWORD(wParam);
4233 TRACE("wp %x\n", wParam);
4235 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4238 if (infoPtr->hwndEdit)
4239 SetFocus(infoPtr->hwnd);
4241 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4242 /* shall never occur */
4249 switch (nScrollCode)
4252 scrollX -= infoPtr->uItemHeight;
4255 scrollX += infoPtr->uItemHeight;
4258 scrollX -= infoPtr->clientWidth;
4261 scrollX += infoPtr->clientWidth;
4265 case SB_THUMBPOSITION:
4266 scrollX = (int)(SHORT)HIWORD(wParam);
4273 if (scrollX > maxWidth)
4275 else if (scrollX < 0)
4279 if (scrollX != infoPtr->scrollX)
4281 TREEVIEW_ITEM *item;
4282 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4284 for (item = infoPtr->root->firstChild; item != NULL;
4285 item = TREEVIEW_GetNextListItem(infoPtr, item))
4287 item->linesOffset += scroll_pixels;
4288 item->stateOffset += scroll_pixels;
4289 item->imageOffset += scroll_pixels;
4290 item->textOffset += scroll_pixels;
4293 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4294 infoPtr->scrollX = scrollX;
4295 UpdateWindow(infoPtr->hwnd);
4298 if (nScrollCode != SB_THUMBTRACK)
4299 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4305 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4308 UINT pulScrollLines = 3;
4310 if (infoPtr->firstVisible == NULL)
4313 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4315 gcWheelDelta = -(short)HIWORD(wParam);
4316 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4318 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4320 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4321 int maxDy = infoPtr->maxVisibleOrder;
4329 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4334 /* Create/Destroy *******************************************************/
4337 TREEVIEW_Create(HWND hwnd)
4340 TREEVIEW_INFO *infoPtr;
4342 TRACE("wnd %x, style %lx\n", hwnd, GetWindowLongA(hwnd, GWL_STYLE));
4344 infoPtr = (TREEVIEW_INFO *)COMCTL32_Alloc(sizeof(TREEVIEW_INFO));
4346 if (infoPtr == NULL)
4348 ERR("could not allocate info memory!\n");
4352 SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
4354 infoPtr->hwnd = hwnd;
4355 infoPtr->dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
4356 infoPtr->uInternalStatus = 0;
4358 infoPtr->uNumItems = 0;
4359 infoPtr->cdmode = 0;
4360 infoPtr->uScrollTime = 300; /* milliseconds */
4361 infoPtr->bRedraw = TRUE;
4363 GetClientRect(hwnd, &rcClient);
4365 /* No scroll bars yet. */
4366 infoPtr->clientWidth = rcClient.right;
4367 infoPtr->clientHeight = rcClient.bottom;
4369 infoPtr->treeWidth = 0;
4370 infoPtr->treeHeight = 0;
4372 infoPtr->uIndent = 19;
4373 infoPtr->selectedItem = 0;
4374 infoPtr->focusedItem = 0;
4376 infoPtr->firstVisible = 0;
4377 infoPtr->maxVisibleOrder = 0;
4378 infoPtr->dropItem = 0;
4379 infoPtr->insertMarkItem = 0;
4380 infoPtr->insertBeforeorAfter = 0;
4383 infoPtr->scrollX = 0;
4385 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4386 infoPtr->clrText = -1; /* use system color */
4387 infoPtr->clrLine = RGB(128, 128, 128);
4388 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4392 infoPtr->hwndEdit = 0;
4393 infoPtr->wpEditOrig = NULL;
4394 infoPtr->bIgnoreEditKillFocus = FALSE;
4395 infoPtr->bLabelChanged = FALSE;
4397 infoPtr->himlNormal = NULL;
4398 infoPtr->himlState = NULL;
4399 infoPtr->normalImageWidth = 0;
4400 infoPtr->normalImageHeight = 0;
4401 infoPtr->stateImageWidth = 0;
4402 infoPtr->stateImageHeight = 0;
4404 infoPtr->items = DPA_Create(16);
4406 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
4407 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4409 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4411 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4412 infoPtr->root->state = TVIS_EXPANDED;
4413 infoPtr->root->iLevel = -1;
4414 infoPtr->root->visibleOrder = -1;
4416 infoPtr->hwndNotify = GetParent(hwnd);
4418 infoPtr->bTransparent = ( GetWindowLongA( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4421 infoPtr->hwndToolTip = 0;
4422 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4423 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4425 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4428 HBITMAP hbm, hbmOld;
4432 infoPtr->himlState =
4433 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4435 hdc = CreateCompatibleDC(0);
4436 hbm = CreateCompatibleBitmap(hdc, 48, 16);
4437 hbmOld = SelectObject(hdc, hbm);
4439 rc.left = 0; rc.top = 0;
4440 rc.right = 48; rc.bottom = 16;
4441 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4443 rc.left = 18; rc.top = 2;
4444 rc.right = 30; rc.bottom = 14;
4445 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4446 DFCS_BUTTONCHECK|DFCS_FLAT);
4448 rc.left = 34; rc.right = 46;
4449 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4450 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4452 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4453 GetSysColor(COLOR_WINDOW));
4454 TRACE("chckbox index %d\n", nIndex);
4455 SelectObject(hdc, hbmOld);
4459 infoPtr->stateImageWidth = 16;
4460 infoPtr->stateImageHeight = 16;
4467 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4471 TREEVIEW_RemoveTree(infoPtr);
4473 /* tool tip is automatically destroyed: we are its owner */
4475 /* Restore original windproc. */
4476 if (infoPtr->hwndEdit)
4477 SetWindowLongA(infoPtr->hwndEdit, GWL_WNDPROC,
4478 (LONG)infoPtr->wpEditOrig);
4480 /* Deassociate treeview from the window before doing anything drastic. */
4481 SetWindowLongA(infoPtr->hwnd, 0, (LONG)NULL);
4483 DeleteObject(infoPtr->hBoldFont);
4484 COMCTL32_Free(infoPtr);
4489 /* Miscellaneous Messages ***********************************************/
4492 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
4500 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4501 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
4502 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
4503 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
4504 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
4505 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
4506 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
4507 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
4508 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
4512 if (key >= VK_PRIOR && key <= VK_DOWN)
4514 unsigned char code = scroll[key - VK_PRIOR].code;
4516 (((code & (1 << 7)) == (SB_HORZ << 7))
4518 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
4524 /************************************************************************
4527 * VK_UP Move selection to the previous non-hidden item.
4528 * VK_DOWN Move selection to the next non-hidden item.
4529 * VK_HOME Move selection to the first item.
4530 * VK_END Move selection to the last item.
4531 * VK_LEFT If expanded then collapse, otherwise move to parent.
4532 * VK_RIGHT If collapsed then expand, otherwise move to first child.
4534 * VK_SUBTRACT Collapse.
4535 * VK_MULTIPLY Expand all.
4536 * VK_PRIOR Move up GetVisibleCount items.
4537 * VK_NEXT Move down GetVisibleCount items.
4538 * VK_BACK Move to parent.
4539 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
4542 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4544 /* If it is non-NULL and different, it will be selected and visible. */
4545 TREEVIEW_ITEM *newSelection = NULL;
4547 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
4549 TRACE("%x\n", wParam);
4551 if (prevItem == NULL)
4554 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
4555 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
4560 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
4562 newSelection = infoPtr->root->firstChild;
4566 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
4570 newSelection = infoPtr->root->firstChild;
4574 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4578 if (prevItem->state & TVIS_EXPANDED)
4580 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4582 else if (prevItem->parent != infoPtr->root)
4584 newSelection = prevItem->parent;
4589 if (TREEVIEW_HasChildren(infoPtr, prevItem))
4591 if (!(prevItem->state & TVIS_EXPANDED))
4592 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4595 newSelection = prevItem->firstChild;
4602 TREEVIEW_ExpandAll(infoPtr, prevItem);
4606 if (!(prevItem->state & TVIS_EXPANDED))
4607 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4611 if (prevItem->state & TVIS_EXPANDED)
4612 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4617 = TREEVIEW_GetListItem(infoPtr, prevItem,
4618 -TREEVIEW_GetVisibleCount(infoPtr));
4623 = TREEVIEW_GetListItem(infoPtr, prevItem,
4624 TREEVIEW_GetVisibleCount(infoPtr));
4628 newSelection = prevItem->parent;
4629 if (newSelection == infoPtr->root)
4630 newSelection = NULL;
4634 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4635 TREEVIEW_ToggleItemState(infoPtr, prevItem);
4639 if (newSelection && newSelection != prevItem)
4641 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
4644 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
4652 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4654 if (wParam == SIZE_RESTORED)
4656 infoPtr->clientWidth = SLOWORD(lParam);
4657 infoPtr->clientHeight = SHIWORD(lParam);
4659 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
4660 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
4661 TREEVIEW_UpdateScrollBars(infoPtr);
4665 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
4668 TREEVIEW_Invalidate(infoPtr, NULL);
4673 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4675 TRACE("(%x %lx)\n", wParam, lParam);
4677 if (wParam == GWL_STYLE)
4679 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
4681 /* we have to take special care about tooltips */
4682 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
4684 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
4686 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
4691 DestroyWindow(infoPtr->hwndToolTip);
4692 infoPtr->hwndToolTip = 0;
4696 infoPtr->dwStyle = dwNewStyle;
4699 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
4700 TREEVIEW_UpdateScrollBars(infoPtr);
4701 TREEVIEW_Invalidate(infoPtr, NULL);
4707 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
4711 if (!infoPtr->selectedItem)
4713 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
4717 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
4718 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
4723 TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
4727 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
4728 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
4733 static LRESULT WINAPI
4734 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
4736 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
4737 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
4740 if (uMsg == WM_CREATE)
4741 TREEVIEW_Create(hwnd);
4748 case TVM_CREATEDRAGIMAGE:
4749 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
4751 case TVM_DELETEITEM:
4752 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
4754 case TVM_EDITLABELA:
4755 return TREEVIEW_EditLabelA(infoPtr, (HTREEITEM)lParam);
4757 case TVM_EDITLABELW:
4758 FIXME("Unimplemented msg TVM_EDITLABELW\n");
4761 case TVM_ENDEDITLABELNOW:
4762 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
4764 case TVM_ENSUREVISIBLE:
4765 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
4768 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
4770 case TVM_GETBKCOLOR:
4771 return TREEVIEW_GetBkColor(infoPtr);
4774 return TREEVIEW_GetCount(infoPtr);
4776 case TVM_GETEDITCONTROL:
4777 return TREEVIEW_GetEditControl(infoPtr);
4779 case TVM_GETIMAGELIST:
4780 return TREEVIEW_GetImageList(infoPtr, wParam);
4783 return TREEVIEW_GetIndent(infoPtr);
4785 case TVM_GETINSERTMARKCOLOR:
4786 return TREEVIEW_GetInsertMarkColor(infoPtr);
4788 case TVM_GETISEARCHSTRINGA:
4789 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
4792 case TVM_GETISEARCHSTRINGW:
4793 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
4797 return TREEVIEW_GetItemA(infoPtr, (LPTVITEMEXA)lParam);
4800 return TREEVIEW_GetItemW(infoPtr, (LPTVITEMEXA)lParam);
4802 case TVM_GETITEMHEIGHT:
4803 return TREEVIEW_GetItemHeight(infoPtr);
4805 case TVM_GETITEMRECT:
4806 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
4808 case TVM_GETITEMSTATE:
4809 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
4811 case TVM_GETLINECOLOR:
4812 return TREEVIEW_GetLineColor(infoPtr);
4814 case TVM_GETNEXTITEM:
4815 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
4817 case TVM_GETSCROLLTIME:
4818 return TREEVIEW_GetScrollTime(infoPtr);
4820 case TVM_GETTEXTCOLOR:
4821 return TREEVIEW_GetTextColor(infoPtr);
4823 case TVM_GETTOOLTIPS:
4824 return TREEVIEW_GetToolTips(infoPtr);
4826 case TVM_GETUNICODEFORMAT:
4827 FIXME("Unimplemented msg TVM_GETUNICODEFORMAT\n");
4830 case TVM_GETVISIBLECOUNT:
4831 return TREEVIEW_GetVisibleCount(infoPtr);
4834 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
4836 case TVM_INSERTITEMA:
4837 return TREEVIEW_InsertItemA(infoPtr, lParam);
4839 case TVM_INSERTITEMW:
4840 return TREEVIEW_InsertItemW(infoPtr, lParam);
4842 case TVM_SELECTITEM:
4843 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
4845 case TVM_SETBKCOLOR:
4846 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
4848 case TVM_SETIMAGELIST:
4849 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
4852 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
4854 case TVM_SETINSERTMARK:
4855 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
4857 case TVM_SETINSERTMARKCOLOR:
4858 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
4861 return TREEVIEW_SetItemA(infoPtr, (LPTVITEMEXA)lParam);
4864 FIXME("Unimplemented msg TVM_SETITEMW\n");
4867 case TVM_SETLINECOLOR:
4868 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
4870 case TVM_SETITEMHEIGHT:
4871 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
4873 case TVM_SETSCROLLTIME:
4874 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
4876 case TVM_SETTEXTCOLOR:
4877 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
4879 case TVM_SETTOOLTIPS:
4880 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
4882 case TVM_SETUNICODEFORMAT:
4883 FIXME("Unimplemented msg TVM_SETUNICODEFORMAT\n");
4886 case TVM_SORTCHILDREN:
4887 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
4889 case TVM_SORTCHILDRENCB:
4890 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
4895 return TREEVIEW_Command(infoPtr, wParam, lParam);
4898 return TREEVIEW_Destroy(infoPtr);
4903 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
4906 return DLGC_WANTARROWS | DLGC_WANTCHARS;
4909 return TREEVIEW_GetFont(infoPtr);
4912 return TREEVIEW_HScroll(infoPtr, wParam);
4915 return TREEVIEW_KeyDown(infoPtr, wParam);
4918 return TREEVIEW_KillFocus(infoPtr);
4920 case WM_LBUTTONDBLCLK:
4921 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
4923 case WM_LBUTTONDOWN:
4924 return TREEVIEW_LButtonDown(infoPtr, lParam);
4926 /* WM_MBUTTONDOWN */
4932 /* WM_NOTIFYFORMAT */
4935 return TREEVIEW_Paint(infoPtr, wParam);
4937 /* WM_PRINTCLIENT */
4939 case WM_RBUTTONDOWN:
4940 return TREEVIEW_RButtonDown(infoPtr, lParam);
4943 return TREEVIEW_SetFocus(infoPtr);
4946 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
4949 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
4952 return TREEVIEW_Size(infoPtr, wParam, lParam);
4954 case WM_STYLECHANGED:
4955 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
4957 /* WM_SYSCOLORCHANGE */
4962 return TREEVIEW_HandleTimer(infoPtr, wParam);
4965 return TREEVIEW_VScroll(infoPtr, wParam);
4967 /* WM_WININICHANGE */
4970 if (wParam & (MK_SHIFT | MK_CONTROL))
4972 return TREEVIEW_MouseWheel(infoPtr, wParam);
4975 TRACE("drawItem\n");
4979 /* This mostly catches MFC and Delphi messages. :( */
4980 if (uMsg >= WM_USER)
4981 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
4983 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
4989 /* Class Registration ***************************************************/
4992 TREEVIEW_Register(void)
4998 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
4999 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5000 wndClass.lpfnWndProc = (WNDPROC)TREEVIEW_WindowProc;
5001 wndClass.cbClsExtra = 0;
5002 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5004 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
5005 wndClass.hbrBackground = 0;
5006 wndClass.lpszClassName = WC_TREEVIEWA;
5008 RegisterClassA(&wndClass);
5013 TREEVIEW_Unregister(void)
5015 UnregisterClassA(WC_TREEVIEWA, (HINSTANCE) NULL);
5019 /* Tree Verification ****************************************************/
5023 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5025 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5026 TREEVIEW_ITEM *item)
5028 assert(infoPtr != NULL);
5029 assert(item != NULL);
5031 /* both NULL, or both non-null */
5032 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5034 assert(item->firstChild != item);
5035 assert(item->lastChild != item);
5037 if (item->firstChild)
5039 assert(item->firstChild->parent == item);
5040 assert(item->firstChild->prevSibling == NULL);
5043 if (item->lastChild)
5045 assert(item->lastChild->parent == item);
5046 assert(item->lastChild->nextSibling == NULL);
5049 assert(item->nextSibling != item);
5050 if (item->nextSibling)
5052 assert(item->nextSibling->parent == item->parent);
5053 assert(item->nextSibling->prevSibling == item);
5056 assert(item->prevSibling != item);
5057 if (item->prevSibling)
5059 assert(item->prevSibling->parent == item->parent);
5060 assert(item->prevSibling->nextSibling == item);
5065 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5067 assert(item != NULL);
5069 assert(item->parent != NULL);
5070 assert(item->parent != item);
5071 assert(item->iLevel == item->parent->iLevel + 1);
5073 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5075 TREEVIEW_VerifyItemCommon(infoPtr, item);
5077 TREEVIEW_VerifyChildren(infoPtr, item);
5081 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5083 TREEVIEW_ITEM *child;
5084 assert(item != NULL);
5086 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5087 TREEVIEW_VerifyItem(infoPtr, child);
5091 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5093 TREEVIEW_ITEM *root = infoPtr->root;
5095 assert(root != NULL);
5096 assert(root->iLevel == -1);
5097 assert(root->parent == NULL);
5098 assert(root->prevSibling == NULL);
5100 TREEVIEW_VerifyItemCommon(infoPtr, root);
5102 TREEVIEW_VerifyChildren(infoPtr, root);
5106 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5108 assert(infoPtr != NULL);
5110 TREEVIEW_VerifyRoot(infoPtr);