Fix behavior of TVS_SINGLEEXPAND style broken in Corel merge. Optimize
[wine/multimedia.git] / dlls / comctl32 / treeview.c
blob35fde6ff094c5a55dadffa8d45eaee208b926bcd
1 /* Treeview control
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.
14 * TODO:
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.
27 #include <assert.h>
28 #include <string.h>
29 #include <limits.h>
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "commctrl.h"
33 #include "comctl32.h"
34 #include "debugtools.h"
36 /* internal structures */
38 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
40 UINT callbackMask;
41 UINT state;
42 UINT stateMask;
43 LPSTR pszText;
44 int cchTextMax;
45 int iImage;
46 int iSelectedImage;
47 int cChildren;
48 LPARAM lParam;
49 int iIntegral; /* item height multiplier (1 is normal) */
50 int iLevel; /* indentation level:0=root level */
51 HTREEITEM parent; /* handle to parent or 0 if at root*/
52 HTREEITEM firstChild; /* handle to first child or 0 if no child*/
53 HTREEITEM lastChild;
54 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
55 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
56 RECT rect;
57 LONG linesOffset;
58 LONG stateOffset;
59 LONG imageOffset;
60 LONG textOffset;
61 LONG textWidth; /* horizontal text extent for pszText */
62 LONG visibleOrder; /* visible ordering, 0 is first visible item */
63 } TREEVIEW_ITEM;
66 typedef struct tagTREEVIEW_INFO
68 HWND hwnd;
69 DWORD dwStyle;
70 HTREEITEM root;
71 UINT uInternalStatus;
72 INT Timer;
73 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
74 INT cdmode; /* last custom draw setting */
75 UINT uScrollTime; /* max. time for scrolling in milliseconds*/
76 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
78 UINT uItemHeight; /* item height */
79 BOOL bHeightSet;
81 LONG clientWidth; /* width of control window */
82 LONG clientHeight; /* height of control window */
84 LONG treeWidth; /* width of visible tree items */
85 LONG treeHeight; /* height of visible tree items */
87 UINT uIndent; /* indentation in pixels */
88 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
89 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
90 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
92 HTREEITEM firstVisible; /* handle to first visible item */
93 LONG maxVisibleOrder;
94 HTREEITEM dropItem; /* handle to item selected by drag cursor */
95 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
96 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
97 HIMAGELIST dragList; /* Bitmap of dragged item */
98 LONG scrollX;
99 COLORREF clrBk;
100 COLORREF clrText;
101 COLORREF clrLine;
102 COLORREF clrInsertMark;
103 HFONT hFont;
104 HFONT hBoldFont;
105 HWND hwndToolTip;
107 HWND hwndEdit;
108 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
109 BOOL bIgnoreEditKillFocus;
110 BOOL bLabelChanged;
112 HIMAGELIST himlNormal;
113 int normalImageHeight;
114 int normalImageWidth;
115 HIMAGELIST himlState;
116 int stateImageHeight;
117 int stateImageWidth;
118 HDPA items;
119 } TREEVIEW_INFO;
123 /* bitflags for infoPtr->uInternalStatus */
125 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
126 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
127 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
128 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
129 #define TV_RDRAG 0x10 /* dito Rbutton */
130 #define TV_RDRAGGING 0x20
132 /* bitflags for infoPtr->timer */
134 #define TV_EDIT_TIMER 2
135 #define TV_EDIT_TIMER_SET 2
138 VOID TREEVIEW_Register (VOID);
139 VOID TREEVIEW_Unregister (VOID);
142 DEFAULT_DEBUG_CHANNEL(treeview);
145 #define TEXT_CALLBACK_SIZE 260
147 #define TREEVIEW_LEFT_MARGIN 8
149 #define MINIMUM_INDENT 19
151 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
153 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
154 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
155 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
158 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
161 static VOID TREEVIEW_QueueRefresh(TREEVIEW_INFO *);
162 static VOID TREEVIEW_QueueItemRefresh(TREEVIEW_INFO *, TREEVIEW_ITEM *);
164 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
165 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
166 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
167 static LRESULT TREEVIEW_RButtonUp(TREEVIEW_INFO *, LPPOINT);
168 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
169 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
170 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
173 /* Random Utilities *****************************************************/
175 #ifdef NDEBUG
176 static inline void
177 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
179 (void)infoPtr;
181 #else
182 /* The definition is at the end of the file. */
183 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
184 #endif
186 /* Returns the treeview private data if hwnd is a treeview.
187 * Otherwise returns an undefined value. */
188 static TREEVIEW_INFO *
189 TREEVIEW_GetInfoPtr(HWND hwnd)
191 return (TREEVIEW_INFO *)GetWindowLongA(hwnd, 0);
194 /* Don't call this. Nothing wants an item index. */
195 static inline int
196 TREEVIEW_GetItemIndex(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
198 assert(infoPtr != NULL);
200 return DPA_GetPtrIndex(infoPtr->items, handle);
203 /***************************************************************************
204 * This method checks that handle is an item for this tree.
206 static BOOL
207 TREEVIEW_ValidItem(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
209 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
211 TRACE("invalid item %p\n", handle);
212 return FALSE;
214 else
215 return TRUE;
218 static HFONT
219 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
221 LOGFONTA font;
223 GetObjectA(hOrigFont, sizeof(font), &font);
224 font.lfWeight = FW_BOLD;
225 return CreateFontIndirectA(&font);
228 static inline HFONT
229 TREEVIEW_FontForItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
231 return (item->state & TVIS_BOLD) ? infoPtr->hBoldFont : infoPtr->hFont;
234 /* for trace/debugging purposes only */
235 static const char *
236 TREEVIEW_ItemName(TREEVIEW_ITEM *item)
238 if (item == NULL) return "<null item>";
239 if (item->pszText == LPSTR_TEXTCALLBACKA) return "<callback>";
240 if (item->pszText == NULL) return "<null>";
241 return item->pszText;
244 /* An item is not a child of itself. */
245 static BOOL
246 TREEVIEW_IsChildOf(TREEVIEW_ITEM *parent, TREEVIEW_ITEM *child)
250 child = child->parent;
251 if (child == parent) return TRUE;
252 } while (child != NULL);
254 return FALSE;
258 /* Tree Traversal *******************************************************/
260 /***************************************************************************
261 * This method returns the last expanded sibling or child child item
262 * of a tree node
264 static TREEVIEW_ITEM *
265 TREEVIEW_GetLastListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
267 if (!wineItem)
268 return NULL;
270 while (wineItem->lastChild)
272 if (wineItem->state & TVIS_EXPANDED)
273 wineItem = wineItem->lastChild;
274 else
275 break;
278 if (wineItem == infoPtr->root)
279 return NULL;
281 return wineItem;
284 /***************************************************************************
285 * This method returns the previous non-hidden item in the list not
286 * considering the tree hierarchy.
288 static TREEVIEW_ITEM *
289 TREEVIEW_GetPrevListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
291 if (tvItem->prevSibling)
293 /* This item has a prevSibling, get the last item in the sibling's tree. */
294 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
296 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
297 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
298 else
299 return upItem;
301 else
303 /* this item does not have a prevSibling, get the parent */
304 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
309 /***************************************************************************
310 * This method returns the next physical item in the treeview not
311 * considering the tree hierarchy.
313 static TREEVIEW_ITEM *
314 TREEVIEW_GetNextListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
316 assert(tvItem != NULL);
319 * If this item has children and is expanded, return the first child
321 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
323 return tvItem->firstChild;
328 * try to get the sibling
330 if (tvItem->nextSibling)
331 return tvItem->nextSibling;
334 * Otherwise, get the parent's sibling.
336 while (tvItem->parent)
338 tvItem = tvItem->parent;
340 if (tvItem->nextSibling)
341 return tvItem->nextSibling;
344 return NULL;
347 /***************************************************************************
348 * This method returns the nth item starting at the given item. It returns
349 * the last item (or first) we we run out of items.
351 * Will scroll backward if count is <0.
352 * forward if count is >0.
354 static TREEVIEW_ITEM *
355 TREEVIEW_GetListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
356 LONG count)
358 TREEVIEW_ITEM *(*next_item)(TREEVIEW_INFO *, TREEVIEW_ITEM *);
359 TREEVIEW_ITEM *previousItem;
361 assert(wineItem != NULL);
363 if (count > 0)
365 next_item = TREEVIEW_GetNextListItem;
367 else if (count < 0)
369 count = -count;
370 next_item = TREEVIEW_GetPrevListItem;
372 else
373 return wineItem;
377 previousItem = wineItem;
378 wineItem = next_item(infoPtr, wineItem);
380 } while (--count && wineItem != NULL);
383 return wineItem ? wineItem : previousItem;
386 /* Notifications ************************************************************/
388 static BOOL
389 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO *infoPtr, UINT code)
391 NMHDR nmhdr;
392 HWND hwnd = infoPtr->hwnd;
394 TRACE("%x\n", code);
395 nmhdr.hwndFrom = hwnd;
396 nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
397 nmhdr.code = code;
399 return (BOOL)SendMessageA(GetParent(hwnd), WM_NOTIFY,
400 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
403 static VOID
404 TREEVIEW_TVItemFromItem(UINT mask, TVITEMA *tvItem, TREEVIEW_ITEM *item)
406 tvItem->mask = mask;
407 tvItem->hItem = item;
408 tvItem->state = item->state;
409 tvItem->stateMask = 0;
410 tvItem->iImage = item->iImage;
411 tvItem->pszText = item->pszText;
412 tvItem->cchTextMax = item->cchTextMax;
413 tvItem->iImage = item->iImage;
414 tvItem->iSelectedImage = item->iSelectedImage;
415 tvItem->cChildren = item->cChildren;
416 tvItem->lParam = item->lParam;
419 static BOOL
420 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO *infoPtr, UINT code, UINT action,
421 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
423 HWND hwnd = infoPtr->hwnd;
424 NMTREEVIEWA nmhdr;
426 TRACE("code:%x action:%x olditem:%p newitem:%p\n",
427 code, action, oldItem, newItem);
429 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWA));
431 nmhdr.hdr.hwndFrom = hwnd;
432 nmhdr.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
433 nmhdr.hdr.code = code;
434 nmhdr.action = action;
436 if (oldItem)
437 TREEVIEW_TVItemFromItem(mask, &nmhdr.itemOld, oldItem);
439 if (newItem)
440 TREEVIEW_TVItemFromItem(mask, &nmhdr.itemNew, newItem);
442 nmhdr.ptDrag.x = 0;
443 nmhdr.ptDrag.y = 0;
445 return (BOOL)SendMessageA(GetParent(hwnd), WM_NOTIFY,
446 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
447 (LPARAM)&nmhdr);
450 static BOOL
451 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO *infoPtr, UINT code,
452 HTREEITEM dragItem, POINT pt)
454 HWND hwnd = infoPtr->hwnd;
455 NMTREEVIEWA nmhdr;
457 TRACE("code:%x dragitem:%p\n", code, dragItem);
459 nmhdr.hdr.hwndFrom = hwnd;
460 nmhdr.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
461 nmhdr.hdr.code = code;
462 nmhdr.action = 0;
463 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
464 nmhdr.itemNew.hItem = dragItem;
465 nmhdr.itemNew.state = dragItem->state;
466 nmhdr.itemNew.lParam = dragItem->lParam;
468 nmhdr.ptDrag.x = pt.x;
469 nmhdr.ptDrag.y = pt.y;
471 return (BOOL)SendMessageA(GetParent(hwnd), WM_NOTIFY,
472 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
473 (LPARAM)&nmhdr);
477 static BOOL
478 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
479 HDC hdc, RECT rc)
481 HWND hwnd = infoPtr->hwnd;
482 NMTVCUSTOMDRAW nmcdhdr;
483 LPNMCUSTOMDRAW nmcd;
485 TRACE("drawstage:%lx hdc:%x\n", dwDrawStage, hdc);
487 nmcd = &nmcdhdr.nmcd;
488 nmcd->hdr.hwndFrom = hwnd;
489 nmcd->hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
490 nmcd->hdr.code = NM_CUSTOMDRAW;
491 nmcd->dwDrawStage = dwDrawStage;
492 nmcd->hdc = hdc;
493 nmcd->rc = rc;
494 nmcd->dwItemSpec = 0;
495 nmcd->uItemState = 0;
496 nmcd->lItemlParam = 0;
497 nmcdhdr.clrText = infoPtr->clrText;
498 nmcdhdr.clrTextBk = infoPtr->clrBk;
499 nmcdhdr.iLevel = 0;
501 return (BOOL)SendMessageA(GetParent(hwnd), WM_NOTIFY,
502 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
503 (LPARAM)&nmcdhdr);
508 /* FIXME: need to find out when the flags in uItemState need to be set */
510 static BOOL
511 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO *infoPtr, HDC hdc,
512 TREEVIEW_ITEM *wineItem, UINT uItemDrawState)
514 HWND hwnd = infoPtr->hwnd;
515 NMTVCUSTOMDRAW nmcdhdr;
516 LPNMCUSTOMDRAW nmcd;
517 DWORD dwDrawStage, dwItemSpec;
518 UINT uItemState;
519 INT retval;
521 dwDrawStage = CDDS_ITEM | uItemDrawState;
522 dwItemSpec = (DWORD)wineItem;
523 uItemState = 0;
524 if (wineItem->state & TVIS_SELECTED)
525 uItemState |= CDIS_SELECTED;
526 if (wineItem == infoPtr->selectedItem)
527 uItemState |= CDIS_FOCUS;
528 if (wineItem == infoPtr->hotItem)
529 uItemState |= CDIS_HOT;
531 nmcd = &nmcdhdr.nmcd;
532 nmcd->hdr.hwndFrom = hwnd;
533 nmcd->hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
534 nmcd->hdr.code = NM_CUSTOMDRAW;
535 nmcd->dwDrawStage = dwDrawStage;
536 nmcd->hdc = hdc;
537 nmcd->rc = wineItem->rect;
538 nmcd->dwItemSpec = dwItemSpec;
539 nmcd->uItemState = uItemState;
540 nmcd->lItemlParam = wineItem->lParam;
541 nmcdhdr.clrText = infoPtr->clrText;
542 nmcdhdr.clrTextBk = infoPtr->clrBk;
543 nmcdhdr.iLevel = wineItem->iLevel;
545 TRACE("drawstage:%lx hdc:%x item:%lx, itemstate:%x, lItemlParam:%lx\n",
546 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
547 nmcd->uItemState, nmcd->lItemlParam);
549 retval = SendMessageA(GetParent(hwnd), WM_NOTIFY,
550 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
551 (LPARAM)&nmcdhdr);
553 infoPtr->clrText = nmcdhdr.clrText;
554 infoPtr->clrBk = nmcdhdr.clrTextBk;
555 return (BOOL)retval;
558 static BOOL
559 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
561 HWND hwnd = infoPtr->hwnd;
562 NMTVDISPINFOA tvdi;
564 tvdi.hdr.hwndFrom = hwnd;
565 tvdi.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
566 tvdi.hdr.code = TVN_BEGINLABELEDITA;
568 tvdi.item.mask = TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT;
569 tvdi.item.hItem = editItem;
570 tvdi.item.state = editItem->state;
571 tvdi.item.lParam = editItem->lParam;
572 tvdi.item.pszText = editItem->pszText;
573 tvdi.item.cchTextMax = editItem->cchTextMax;
575 return SendMessageA(GetParent(hwnd), WM_NOTIFY, tvdi.hdr.idFrom,
576 (LPARAM)&tvdi);
579 static void
580 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
581 UINT mask)
583 NMTVDISPINFOA callback;
584 HWND hwnd = infoPtr->hwnd;
586 mask &= wineItem->callbackMask;
588 if (mask == 0) return;
590 callback.hdr.hwndFrom = hwnd;
591 callback.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
592 callback.hdr.code = TVN_GETDISPINFOA;
594 /* 'state' always contains valid value, as well as 'lParam'.
595 * All other parameters are uninitialized.
597 callback.item.pszText = wineItem->pszText;
598 callback.item.cchTextMax = wineItem->cchTextMax;
599 callback.item.mask = mask;
600 callback.item.hItem = wineItem;
601 callback.item.state = wineItem->state;
602 callback.item.lParam = wineItem->lParam;
604 /* If text is changed we need to recalculate textWidth */
605 if (mask & TVIF_TEXT)
606 wineItem->textWidth = 0;
608 SendMessageA(GetParent(hwnd), WM_NOTIFY, callback.hdr.idFrom, (LPARAM)&callback);
610 /* It may have changed due to a call to SetItem. */
611 mask &= wineItem->callbackMask;
613 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
615 /* Instead of copying text into our buffer user specified its own */
616 int len = max(lstrlenA(callback.item.pszText) + 1, TEXT_CALLBACK_SIZE);
617 LPSTR newText = COMCTL32_ReAlloc(wineItem->pszText, len);
619 if (newText)
621 wineItem->pszText = newText;
622 strcpy(wineItem->pszText, callback.item.pszText);
623 wineItem->cchTextMax = len;
625 /* If ReAlloc fails we have nothing to do, but keep original text */
628 if (mask & TVIF_IMAGE)
629 wineItem->iImage = callback.item.iImage;
631 if (mask & TVIF_SELECTEDIMAGE)
632 wineItem->iSelectedImage = callback.item.iSelectedImage;
634 if (mask & TVIF_CHILDREN)
635 wineItem->cChildren = callback.item.cChildren;
637 /* These members are now permanently set. */
638 if (callback.item.mask & TVIF_DI_SETITEM)
639 wineItem->callbackMask &= ~callback.item.mask;
642 /***************************************************************************
643 * This function uses cChildren field to decide whether the item has
644 * children or not.
645 * Note: if this returns TRUE, the child items may not actually exist,
646 * they could be virtual.
648 * Just use wineItem->firstChild to check for physical children.
650 static BOOL
651 TREEVIEW_HasChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
653 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
655 return wineItem->cChildren > 0;
659 /* Item Position ********************************************************/
661 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
662 static VOID
663 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO *infoPtr,
664 TREEVIEW_ITEM *item)
666 /* Same effect, different optimisation. */
667 #if 0
668 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
669 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
670 #else
671 BOOL lar = ((infoPtr->dwStyle
672 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
673 > TVS_LINESATROOT);
674 #endif
676 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
677 - infoPtr->scrollX;
678 item->stateOffset = item->linesOffset + infoPtr->uIndent;
679 item->imageOffset = item->stateOffset
680 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
681 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
684 static VOID
685 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
687 HDC hdc;
688 HFONT hOldFont=0;
689 SIZE sz;
691 /* DRAW's OM docker creates items like this */
692 if (item->pszText == NULL)
694 item->textWidth = 0;
695 return;
698 if (item->textWidth != 0 && !(item->callbackMask & TVIF_TEXT))
699 return;
701 if (hDC != 0)
703 hdc = hDC;
705 else
707 hdc = GetDC(infoPtr->hwnd);
708 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
711 GetTextExtentPoint32A(hdc, item->pszText, strlen(item->pszText), &sz);
712 item->textWidth = sz.cx;
714 if (hDC == 0)
716 SelectObject(hdc, hOldFont);
717 ReleaseDC(0, hdc);
721 static VOID
722 TREEVIEW_ComputeItemRect(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
724 item->rect.top = infoPtr->uItemHeight *
725 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
727 item->rect.bottom = item->rect.top
728 + infoPtr->uItemHeight * item->iIntegral;
730 item->rect.left = 0;
731 item->rect.right = infoPtr->clientWidth;
734 /* We know that only items after start need their order updated. */
735 static void
736 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
738 TREEVIEW_ITEM *item;
739 int order;
741 if (!start)
743 start = infoPtr->root->firstChild;
744 order = 0;
746 else
747 order = start->visibleOrder;
749 for (item = start; item != NULL;
750 item = TREEVIEW_GetNextListItem(infoPtr, item))
752 item->visibleOrder = order;
753 order += item->iIntegral;
756 infoPtr->maxVisibleOrder = order;
758 for (item = start; item != NULL;
759 item = TREEVIEW_GetNextListItem(infoPtr, item))
761 TREEVIEW_ComputeItemRect(infoPtr, item);
766 /* Update metrics of all items in selected subtree.
767 * root must be expanded
769 static VOID
770 TREEVIEW_UpdateSubTree(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
772 TREEVIEW_ITEM *sibling;
773 HDC hdc;
774 HFONT hOldFont;
776 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
777 return;
779 root->state &= ~TVIS_EXPANDED;
780 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
781 root->state |= TVIS_EXPANDED;
783 hdc = GetDC(infoPtr->hwnd);
784 hOldFont = SelectObject(hdc, infoPtr->hFont);
786 for (; root != sibling;
787 root = TREEVIEW_GetNextListItem(infoPtr, root))
789 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
791 if (root->callbackMask & TVIF_TEXT)
792 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
794 if (root->textWidth == 0)
796 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
797 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
801 SelectObject(hdc, hOldFont);
802 ReleaseDC(infoPtr->hwnd, hdc);
805 /* Item Allocation **********************************************************/
807 static TREEVIEW_ITEM *
808 TREEVIEW_AllocateItem(TREEVIEW_INFO *infoPtr)
810 TREEVIEW_ITEM *newItem = COMCTL32_Alloc(sizeof(TREEVIEW_ITEM));
812 if (!newItem)
813 return NULL;
815 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
817 COMCTL32_Free(newItem);
818 return NULL;
821 return newItem;
824 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
825 * free item->pszText. */
826 static void
827 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
829 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
830 COMCTL32_Free(item);
834 /* Item Insertion *******************************************************/
836 /***************************************************************************
837 * This method inserts newItem before sibling as a child of parent.
838 * sibling can be NULL, but only if parent has no children.
840 static void
841 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
842 TREEVIEW_ITEM *parent)
844 assert(newItem != NULL);
845 assert(parent != NULL);
847 if (sibling != NULL)
849 assert(sibling->parent == parent);
851 if (sibling->prevSibling != NULL)
852 sibling->prevSibling->nextSibling = newItem;
854 newItem->prevSibling = sibling->prevSibling;
855 sibling->prevSibling = newItem;
857 else
858 newItem->prevSibling = NULL;
860 newItem->nextSibling = sibling;
862 if (parent->firstChild == sibling)
863 parent->firstChild = newItem;
865 if (parent->lastChild == NULL)
866 parent->lastChild = newItem;
869 /***************************************************************************
870 * This method inserts newItem after sibling as a child of parent.
871 * sibling can be NULL, but only if parent has no children.
873 static void
874 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
875 TREEVIEW_ITEM *parent)
877 assert(newItem != NULL);
878 assert(parent != NULL);
880 if (sibling != NULL)
882 assert(sibling->parent == parent);
884 if (sibling->nextSibling != NULL)
885 sibling->nextSibling->prevSibling = newItem;
887 newItem->nextSibling = sibling->nextSibling;
888 sibling->nextSibling = newItem;
890 else
891 newItem->nextSibling = NULL;
893 newItem->prevSibling = sibling;
895 if (parent->lastChild == sibling)
896 parent->lastChild = newItem;
898 if (parent->firstChild == NULL)
899 parent->firstChild = newItem;
902 static BOOL
903 TREEVIEW_DoSetItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
904 const TVITEMEXA *tvItem)
906 UINT callbackClear = 0;
907 UINT callbackSet = 0;
909 /* Do this first in case it fails. */
910 if (tvItem->mask & TVIF_TEXT)
912 if (tvItem->pszText != LPSTR_TEXTCALLBACKA)
914 int len = lstrlenA(tvItem->pszText) + 1;
915 LPSTR newText = COMCTL32_ReAlloc(wineItem->pszText, len);
917 if (newText == NULL) return FALSE;
919 callbackClear |= TVIF_TEXT;
921 wineItem->pszText = newText;
922 wineItem->cchTextMax = len;
923 lstrcpynA(wineItem->pszText, tvItem->pszText, len);
925 else
927 callbackSet |= TVIF_TEXT;
929 wineItem->pszText = COMCTL32_ReAlloc(wineItem->pszText,
930 TEXT_CALLBACK_SIZE);
931 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
935 if (tvItem->mask & TVIF_CHILDREN)
937 wineItem->cChildren = tvItem->cChildren;
939 if (wineItem->cChildren == I_CHILDRENCALLBACK)
940 callbackSet |= TVIF_CHILDREN;
941 else
942 callbackClear |= TVIF_CHILDREN;
945 if (tvItem->mask & TVIF_IMAGE)
947 wineItem->iImage = tvItem->iImage;
949 if (wineItem->iImage == I_IMAGECALLBACK)
950 callbackSet |= TVIF_IMAGE;
951 else
952 callbackClear |= TVIF_IMAGE;
955 if (tvItem->mask & TVIF_SELECTEDIMAGE)
957 wineItem->iSelectedImage = tvItem->iSelectedImage;
959 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
960 callbackSet |= TVIF_SELECTEDIMAGE;
961 else
962 callbackClear |= TVIF_SELECTEDIMAGE;
965 if (tvItem->mask & TVIF_PARAM)
966 wineItem->lParam = tvItem->lParam;
968 /* If the application sets TVIF_INTEGRAL without
969 * supplying a TVITEMEX structure, it's toast. */
970 if (tvItem->mask & TVIF_INTEGRAL)
971 wineItem->iIntegral = tvItem->iIntegral;
973 if (tvItem->mask & TVIF_STATE)
975 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
976 tvItem->stateMask);
977 wineItem->state &= ~tvItem->stateMask;
978 wineItem->state |= (tvItem->state & tvItem->stateMask);
981 wineItem->callbackMask |= callbackSet;
982 wineItem->callbackMask &= ~callbackClear;
984 return TRUE;
987 /* Note that the new item is pre-zeroed. */
988 static LRESULT
989 TREEVIEW_InsertItemA(TREEVIEW_INFO *infoPtr, LPARAM lParam)
991 const TVINSERTSTRUCTA *ptdi = (LPTVINSERTSTRUCTA) lParam;
992 const TVITEMEXA *tvItem = &ptdi->DUMMYUNIONNAME.itemex;
993 HTREEITEM insertAfter;
994 TREEVIEW_ITEM *newItem, *parentItem;
995 BOOL bTextUpdated = FALSE;
997 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
999 parentItem = infoPtr->root;
1001 else
1003 parentItem = ptdi->hParent;
1005 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1007 WARN("invalid parent %p\n", parentItem);
1008 return (LRESULT)(HTREEITEM)NULL;
1012 insertAfter = ptdi->hInsertAfter;
1014 /* Validate this now for convenience. */
1015 switch ((DWORD)insertAfter)
1017 case (DWORD)TVI_FIRST:
1018 case (DWORD)TVI_LAST:
1019 case (DWORD)TVI_SORT:
1020 break;
1022 default:
1023 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1024 insertAfter->parent != parentItem)
1026 WARN("invalid insert after %p\n", insertAfter);
1027 insertAfter = TVI_LAST;
1031 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1032 (tvItem->mask & TVIF_TEXT)
1033 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKA) ? "<callback>"
1034 : tvItem->pszText)
1035 : "<no label>");
1037 newItem = TREEVIEW_AllocateItem(infoPtr);
1038 if (newItem == NULL)
1039 return (LRESULT)(HTREEITEM)NULL;
1041 newItem->parent = parentItem;
1042 newItem->iIntegral = 1;
1044 if (!TREEVIEW_DoSetItem(infoPtr, newItem, tvItem))
1045 return (LRESULT)(HTREEITEM)NULL;
1047 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1049 infoPtr->uNumItems++;
1051 switch ((DWORD)insertAfter)
1053 case (DWORD)TVI_FIRST:
1054 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1055 if (infoPtr->firstVisible == parentItem->firstChild)
1056 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1057 break;
1059 case (DWORD)TVI_LAST:
1060 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1061 break;
1063 /* hInsertAfter names a specific item we want to insert after */
1064 default:
1065 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1066 break;
1068 case (DWORD)TVI_SORT:
1070 TREEVIEW_ITEM *aChild;
1071 TREEVIEW_ITEM *previousChild = NULL;
1072 BOOL bItemInserted = FALSE;
1074 aChild = parentItem->firstChild;
1076 bTextUpdated = TRUE;
1077 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1079 /* Iterate the parent children to see where we fit in */
1080 while (aChild != NULL)
1082 INT comp;
1084 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1085 comp = lstrcmpA(newItem->pszText, aChild->pszText);
1087 if (comp < 0) /* we are smaller than the current one */
1089 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1090 bItemInserted = TRUE;
1091 break;
1093 else if (comp > 0) /* we are bigger than the current one */
1095 previousChild = aChild;
1097 /* This will help us to exit if there is no more sibling */
1098 aChild = (aChild->nextSibling == 0)
1099 ? NULL
1100 : aChild->nextSibling;
1102 /* Look at the next item */
1103 continue;
1105 else if (comp == 0)
1108 * An item with this name is already existing, therefore,
1109 * we add after the one we found
1111 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1112 bItemInserted = TRUE;
1113 break;
1118 * we reach the end of the child list and the item as not
1119 * yet been inserted, therefore, insert it after the last child.
1121 if ((!bItemInserted) && (aChild == NULL))
1122 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1124 break;
1129 TRACE("new item %p; parent %p, mask %x\n", newItem,
1130 newItem->parent, tvItem->mask);
1132 newItem->iLevel = newItem->parent->iLevel + 1;
1134 if (newItem->parent->cChildren == 0)
1135 newItem->parent->cChildren = 1;
1137 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1139 if (STATEIMAGEINDEX(newItem->state) == 0)
1140 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1143 if (infoPtr->firstVisible == NULL)
1144 infoPtr->firstVisible = newItem;
1146 TREEVIEW_VerifyTree(infoPtr);
1148 if (parentItem == infoPtr->root ||
1149 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1151 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1153 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1154 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1156 if (!bTextUpdated)
1157 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1159 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1160 TREEVIEW_UpdateScrollBars(infoPtr);
1161 TREEVIEW_QueueRefresh(infoPtr);
1163 else
1165 newItem->visibleOrder = -1;
1167 /* refresh treeview if newItem is the first item inserted under parentItem */
1168 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1170 /* parent got '+' - update it */
1171 TREEVIEW_QueueItemRefresh(infoPtr, parentItem);
1175 return (LRESULT)newItem;
1179 static LRESULT
1180 TREEVIEW_InsertItemW(TREEVIEW_INFO *infoPtr, LPARAM lParam)
1182 TVINSERTSTRUCTW *tvisW;
1183 TVINSERTSTRUCTA tvisA;
1184 LRESULT lRes;
1186 tvisW = (LPTVINSERTSTRUCTW) lParam;
1188 tvisA.hParent = tvisW->hParent;
1189 tvisA.hInsertAfter = tvisW->hInsertAfter;
1191 tvisA.DUMMYUNIONNAME.item.mask = tvisW->DUMMYUNIONNAME.item.mask;
1192 tvisA.DUMMYUNIONNAME.item.hItem = tvisW->DUMMYUNIONNAME.item.hItem;
1193 tvisA.DUMMYUNIONNAME.item.state = tvisW->DUMMYUNIONNAME.item.state;
1194 tvisA.DUMMYUNIONNAME.item.stateMask = tvisW->DUMMYUNIONNAME.item.stateMask;
1195 tvisA.DUMMYUNIONNAME.item.cchTextMax =
1196 tvisW->DUMMYUNIONNAME.item.cchTextMax;
1198 if (tvisW->DUMMYUNIONNAME.item.pszText)
1200 if (tvisW->DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKW)
1202 int len = lstrlenW(tvisW->DUMMYUNIONNAME.item.pszText) + 1;
1204 tvisA.DUMMYUNIONNAME.item.pszText = COMCTL32_Alloc(len);
1205 lstrcpyWtoA(tvisA.DUMMYUNIONNAME.item.pszText,
1206 tvisW->DUMMYUNIONNAME.item.pszText);
1208 else
1210 tvisA.DUMMYUNIONNAME.item.pszText = LPSTR_TEXTCALLBACKA;
1211 tvisA.DUMMYUNIONNAME.item.cchTextMax = 0;
1215 tvisA.DUMMYUNIONNAME.item.iImage = tvisW->DUMMYUNIONNAME.item.iImage;
1216 tvisA.DUMMYUNIONNAME.item.iSelectedImage =
1217 tvisW->DUMMYUNIONNAME.item.iSelectedImage;
1218 tvisA.DUMMYUNIONNAME.item.cChildren = tvisW->DUMMYUNIONNAME.item.cChildren;
1219 tvisA.DUMMYUNIONNAME.item.lParam = tvisW->DUMMYUNIONNAME.item.lParam;
1221 lRes = TREEVIEW_InsertItemA(infoPtr, (LPARAM)&tvisA);
1223 if (tvisA.DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKA)
1225 COMCTL32_Free(tvisA.DUMMYUNIONNAME.item.pszText);
1228 return lRes;
1233 /* Item Deletion ************************************************************/
1234 static void
1235 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1237 static void
1238 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1240 TREEVIEW_ITEM *kill = parentItem->firstChild;
1242 while (kill != NULL)
1244 TREEVIEW_ITEM *next = kill->nextSibling;
1246 TREEVIEW_RemoveItem(infoPtr, kill);
1248 kill = next;
1251 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1252 assert(parentItem->firstChild == NULL);
1253 assert(parentItem->lastChild == NULL);
1256 static void
1257 TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1259 TREEVIEW_ITEM *parentItem = item->parent;
1261 assert(item != NULL);
1262 assert(item->parent != NULL); /* i.e. it must not be the root */
1264 if (parentItem->firstChild == item)
1265 parentItem->firstChild = item->nextSibling;
1267 if (parentItem->lastChild == item)
1268 parentItem->lastChild = item->prevSibling;
1270 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1271 && parentItem->cChildren > 0)
1272 parentItem->cChildren = 0;
1274 if (item->prevSibling)
1275 item->prevSibling->nextSibling = item->nextSibling;
1277 if (item->nextSibling)
1278 item->nextSibling->prevSibling = item->prevSibling;
1281 static void
1282 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1284 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1286 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMA,
1287 TVIF_HANDLE | TVIF_PARAM, 0, wineItem, 0);
1289 if (wineItem->firstChild)
1290 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1292 TREEVIEW_UnlinkItem(wineItem);
1294 infoPtr->uNumItems--;
1296 if (wineItem->pszText != LPSTR_TEXTCALLBACKA)
1297 COMCTL32_Free(wineItem->pszText);
1299 TREEVIEW_FreeItem(infoPtr, wineItem);
1303 /* Empty out the tree. */
1304 static void
1305 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1307 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1309 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1312 static LRESULT
1313 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1315 TREEVIEW_ITEM *oldSelection = infoPtr->selectedItem;
1316 TREEVIEW_ITEM *newSelection = oldSelection;
1317 TREEVIEW_ITEM *newFirstVisible = NULL;
1318 TREEVIEW_ITEM *parent, *prev = NULL;
1319 BOOL visible = FALSE;
1321 if (wineItem == TVI_ROOT)
1323 TRACE("TVI_ROOT\n");
1324 parent = infoPtr->root;
1325 newSelection = NULL;
1326 visible = TRUE;
1327 TREEVIEW_RemoveTree(infoPtr);
1329 else
1331 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1332 return FALSE;
1334 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1335 parent = wineItem->parent;
1337 if (ISVISIBLE(wineItem))
1339 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1340 visible = TRUE;
1343 if (infoPtr->selectedItem != NULL
1344 && (wineItem == infoPtr->selectedItem
1345 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1347 if (wineItem->nextSibling)
1348 newSelection = wineItem->nextSibling;
1349 else if (wineItem->parent != infoPtr->root)
1350 newSelection = wineItem->parent;
1353 if (infoPtr->firstVisible == wineItem)
1355 if (wineItem->nextSibling)
1356 newFirstVisible = wineItem->nextSibling;
1357 else if (wineItem->prevSibling)
1358 newFirstVisible = wineItem->prevSibling;
1359 else if (wineItem->parent != infoPtr->root)
1360 newFirstVisible = wineItem->parent;
1362 else
1363 newFirstVisible = infoPtr->firstVisible;
1365 TREEVIEW_RemoveItem(infoPtr, wineItem);
1368 /* Don't change if somebody else already has. */
1369 if (oldSelection == infoPtr->selectedItem)
1371 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1372 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1373 else
1374 infoPtr->selectedItem = 0;
1377 /* Validate insertMark dropItem.
1378 * hotItem ??? - used for comparision only.
1380 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1381 infoPtr->insertMarkItem = 0;
1383 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1384 infoPtr->dropItem = 0;
1386 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1387 newFirstVisible = infoPtr->root->firstChild;
1389 TREEVIEW_VerifyTree(infoPtr);
1392 if (visible)
1394 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1395 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1396 TREEVIEW_UpdateScrollBars(infoPtr);
1397 TREEVIEW_QueueRefresh(infoPtr);
1399 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1401 /* parent lost '+/-' - update it */
1402 TREEVIEW_QueueItemRefresh(infoPtr, parent);
1405 return TRUE;
1409 /* Get/Set Messages *********************************************************/
1410 static LRESULT
1411 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1413 if(wParam)
1414 infoPtr->bRedraw = TRUE;
1415 else
1416 infoPtr->bRedraw = FALSE;
1418 return 0;
1421 static LRESULT
1422 TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1424 TRACE("\n");
1425 return infoPtr->uIndent;
1428 static LRESULT
1429 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1431 TRACE("\n");
1433 if (newIndent < MINIMUM_INDENT)
1434 newIndent = MINIMUM_INDENT;
1436 if (infoPtr->uIndent != newIndent)
1438 infoPtr->uIndent = newIndent;
1439 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1440 TREEVIEW_UpdateScrollBars(infoPtr);
1441 TREEVIEW_QueueRefresh(infoPtr);
1444 return 0;
1448 static LRESULT
1449 TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1451 TRACE("\n");
1452 return infoPtr->hwndToolTip;
1455 static LRESULT
1456 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1458 HWND prevToolTip;
1460 TRACE("\n");
1461 prevToolTip = infoPtr->hwndToolTip;
1462 infoPtr->hwndToolTip = hwndTT;
1464 return prevToolTip;
1468 static LRESULT
1469 TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1471 return infoPtr->uScrollTime;
1474 static LRESULT
1475 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1477 UINT uOldScrollTime = infoPtr->uScrollTime;
1479 infoPtr->uScrollTime = min(uScrollTime, 100);
1481 return uOldScrollTime;
1485 static LRESULT
1486 TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1488 TRACE("\n");
1490 switch (wParam)
1492 case (WPARAM)TVSIL_NORMAL:
1493 return (LRESULT)infoPtr->himlNormal;
1495 case (WPARAM)TVSIL_STATE:
1496 return (LRESULT)infoPtr->himlState;
1498 default:
1499 return 0;
1503 static LRESULT
1504 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1506 HIMAGELIST himlOld = 0;
1507 int oldWidth = infoPtr->normalImageWidth;
1508 int oldHeight = infoPtr->normalImageHeight;
1511 TRACE("%x,%p\n", wParam, himlNew);
1513 switch (wParam)
1515 case (WPARAM)TVSIL_NORMAL:
1516 himlOld = infoPtr->himlNormal;
1517 infoPtr->himlNormal = himlNew;
1519 if (himlNew != NULL)
1520 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1521 &infoPtr->normalImageHeight);
1522 else
1524 infoPtr->normalImageWidth = 0;
1525 infoPtr->normalImageHeight = 0;
1528 break;
1530 case (WPARAM)TVSIL_STATE:
1531 himlOld = infoPtr->himlState;
1532 infoPtr->himlState = himlNew;
1534 if (himlNew != NULL)
1535 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1536 &infoPtr->stateImageHeight);
1537 else
1539 infoPtr->stateImageWidth = 0;
1540 infoPtr->stateImageHeight = 0;
1543 break;
1546 if (oldWidth != infoPtr->normalImageWidth ||
1547 oldHeight != infoPtr->normalImageHeight)
1549 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1550 TREEVIEW_UpdateScrollBars(infoPtr);
1553 TREEVIEW_QueueRefresh(infoPtr);
1555 return (LRESULT)himlOld;
1558 /* Compute the natural height (based on the font size) for items. */
1559 static UINT
1560 TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1562 TEXTMETRICA tm;
1563 HDC hdc = GetDC(0);
1564 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1566 GetTextMetricsA(hdc, &tm);
1568 SelectObject(hdc, hOldFont);
1569 ReleaseDC(0, hdc);
1571 /* The 16 is a hack because our fonts are tiny. */
1572 return max(16, tm.tmHeight + tm.tmExternalLeading);
1575 static LRESULT
1576 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1578 INT prevHeight = infoPtr->uItemHeight;
1580 TRACE("%d \n", newHeight);
1581 if (newHeight == -1)
1583 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1584 infoPtr->bHeightSet = FALSE;
1586 else
1588 infoPtr->uItemHeight = newHeight;
1589 infoPtr->bHeightSet = TRUE;
1592 /* Round down, unless we support odd ("non even") heights. */
1593 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1594 infoPtr->uItemHeight &= ~1;
1596 if (infoPtr->uItemHeight != prevHeight)
1598 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1599 TREEVIEW_UpdateScrollBars(infoPtr);
1600 TREEVIEW_QueueRefresh(infoPtr);
1603 return prevHeight;
1606 static LRESULT
1607 TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1609 TRACE("\n");
1610 return infoPtr->uItemHeight;
1614 static LRESULT
1615 TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1617 TRACE("%x\n", infoPtr->hFont);
1618 return infoPtr->hFont;
1622 static INT CALLBACK
1623 TREEVIEW_ResetTextWidth(LPVOID pItem, DWORD unused)
1625 (void)unused;
1627 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1629 return 1;
1632 static LRESULT
1633 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1635 UINT uHeight = infoPtr->uItemHeight;
1637 TRACE("%x %i\n", hFont, bRedraw);
1639 infoPtr->hFont = hFont ? hFont : GetStockObject(SYSTEM_FONT);
1641 DeleteObject(infoPtr->hBoldFont);
1642 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1644 if (!infoPtr->bHeightSet)
1645 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1647 if (uHeight != infoPtr->uItemHeight)
1648 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1650 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1652 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1653 TREEVIEW_UpdateScrollBars(infoPtr);
1655 if (bRedraw)
1656 TREEVIEW_QueueRefresh(infoPtr);
1658 return 0;
1662 static LRESULT
1663 TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1665 TRACE("\n");
1666 return (LRESULT)infoPtr->clrLine;
1669 static LRESULT
1670 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1672 COLORREF prevColor = infoPtr->clrLine;
1674 TRACE("\n");
1675 infoPtr->clrLine = color;
1676 return (LRESULT)prevColor;
1680 static LRESULT
1681 TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1683 TRACE("\n");
1684 return (LRESULT)infoPtr->clrText;
1687 static LRESULT
1688 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1690 COLORREF prevColor = infoPtr->clrText;
1692 TRACE("\n");
1693 infoPtr->clrText = color;
1695 if (infoPtr->clrText != prevColor)
1696 TREEVIEW_QueueRefresh(infoPtr);
1698 return (LRESULT)prevColor;
1702 static LRESULT
1703 TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1705 TRACE("\n");
1706 return (LRESULT)infoPtr->clrBk;
1709 static LRESULT
1710 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1712 COLORREF prevColor = infoPtr->clrBk;
1714 TRACE("\n");
1715 infoPtr->clrBk = newColor;
1717 if (newColor != prevColor)
1718 TREEVIEW_QueueRefresh(infoPtr);
1720 return (LRESULT)prevColor;
1724 static LRESULT
1725 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1727 TRACE("\n");
1728 return (LRESULT)infoPtr->clrInsertMark;
1731 static LRESULT
1732 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1734 COLORREF prevColor = infoPtr->clrInsertMark;
1736 TRACE("%lx\n", color);
1737 infoPtr->clrInsertMark = color;
1739 return (LRESULT)prevColor;
1743 static LRESULT
1744 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1746 TRACE("%d %p\n", wParam, item);
1748 if (!TREEVIEW_ValidItem(infoPtr, item))
1749 return 0;
1751 infoPtr->insertBeforeorAfter = wParam;
1752 infoPtr->insertMarkItem = item;
1754 TREEVIEW_QueueRefresh(infoPtr);
1756 return 1;
1760 /************************************************************************
1761 * Some serious braindamage here. lParam is a pointer to both the
1762 * input HTREEITEM and the output RECT.
1764 static LRESULT
1765 TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1767 TREEVIEW_ITEM *wineItem;
1768 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1770 TRACE("\n");
1772 * validate parameters
1774 if (pItem == NULL)
1775 return FALSE;
1777 wineItem = *pItem;
1778 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
1779 return FALSE;
1782 * If wParam is TRUE return the text size otherwise return
1783 * the whole item size
1785 if (fTextRect)
1787 /* Windows does not send TVN_GETDISPINFO here. */
1789 lpRect->top = wineItem->rect.top;
1790 lpRect->bottom = wineItem->rect.bottom;
1792 lpRect->left = wineItem->textOffset;
1793 lpRect->right = wineItem->textOffset + wineItem->textWidth;
1795 else
1797 *lpRect = wineItem->rect;
1800 TRACE("%s [L:%d R:%d T:%d B:%d]\n", fTextRect ? "text" : "item",
1801 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
1803 return TRUE;
1806 static inline LRESULT
1807 TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
1809 /* Suprise! This does not take integral height into account. */
1810 return infoPtr->clientHeight / infoPtr->uItemHeight;
1814 static LRESULT
1815 TREEVIEW_GetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1817 TREEVIEW_ITEM *wineItem;
1819 wineItem = tvItem->hItem;
1820 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1821 return FALSE;
1823 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
1825 if (tvItem->mask & TVIF_CHILDREN)
1826 tvItem->cChildren = wineItem->cChildren;
1828 if (tvItem->mask & TVIF_HANDLE)
1829 tvItem->hItem = wineItem;
1831 if (tvItem->mask & TVIF_IMAGE)
1832 tvItem->iImage = wineItem->iImage;
1834 if (tvItem->mask & TVIF_INTEGRAL)
1835 tvItem->iIntegral = wineItem->iIntegral;
1837 /* undocumented: windows ignores TVIF_PARAM and
1838 * * always sets lParam
1840 tvItem->lParam = wineItem->lParam;
1842 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1843 tvItem->iSelectedImage = wineItem->iSelectedImage;
1845 if (tvItem->mask & TVIF_STATE)
1846 tvItem->state = wineItem->state & tvItem->stateMask;
1848 if (tvItem->mask & TVIF_TEXT)
1849 lstrcpynA(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
1851 TRACE("item <%p>, txt %p, img %p, mask %x\n",
1852 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
1854 return TRUE;
1857 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
1858 * which is wrong. */
1859 static LRESULT
1860 TREEVIEW_SetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1862 TREEVIEW_ITEM *wineItem;
1863 TREEVIEW_ITEM originalItem;
1865 wineItem = tvItem->hItem;
1867 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
1868 tvItem->mask);
1870 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1871 return FALSE;
1873 if (!TREEVIEW_DoSetItem(infoPtr, wineItem, tvItem))
1874 return FALSE;
1876 /* store the orignal item values */
1877 originalItem = *wineItem;
1879 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
1880 if ((tvItem->mask & TVIF_TEXT
1881 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
1882 && ISVISIBLE(wineItem))
1884 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
1885 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
1888 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
1890 /* The refresh updates everything, but we can't wait until then. */
1891 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
1893 /* if any of the items values changed, redraw the item */
1894 if(memcmp(&originalItem, wineItem, sizeof(TREEVIEW_ITEM)))
1896 if (tvItem->mask & TVIF_INTEGRAL)
1898 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
1899 TREEVIEW_UpdateScrollBars(infoPtr);
1901 TREEVIEW_QueueRefresh(infoPtr);
1903 else
1905 TREEVIEW_UpdateScrollBars(infoPtr);
1906 TREEVIEW_QueueItemRefresh(infoPtr, wineItem);
1911 return TRUE;
1914 static LRESULT
1915 TREEVIEW_GetItemW(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1917 TREEVIEW_ITEM *wineItem;
1918 INT iItem;
1919 iItem = (INT)tvItem->hItem;
1921 wineItem = tvItem->hItem;
1922 if(!TREEVIEW_ValidItem (infoPtr, wineItem))
1923 return FALSE;
1925 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
1927 if (tvItem->mask & TVIF_CHILDREN) {
1928 if (TVIF_CHILDREN==I_CHILDRENCALLBACK)
1929 FIXME("I_CHILDRENCALLBACK not supported\n");
1930 tvItem->cChildren = wineItem->cChildren;
1933 if (tvItem->mask & TVIF_HANDLE) {
1934 tvItem->hItem = wineItem;
1936 if (tvItem->mask & TVIF_IMAGE) {
1937 tvItem->iImage = wineItem->iImage;
1939 if (tvItem->mask & TVIF_INTEGRAL) {
1940 tvItem->iIntegral = wineItem->iIntegral;
1942 /* undocumented: windows ignores TVIF_PARAM and
1943 * always sets lParam */
1944 tvItem->lParam = wineItem->lParam;
1945 if (tvItem->mask & TVIF_SELECTEDIMAGE) {
1946 tvItem->iSelectedImage = wineItem->iSelectedImage;
1948 if (tvItem->mask & TVIF_STATE) {
1949 tvItem->state = wineItem->state & tvItem->stateMask;
1951 #if 0
1952 if (tvItem->mask & TVIF_TEXT) {
1953 if (wineItem->pszText == LPSTR_TEXTCALLBACKW) {
1954 tvItem->pszText = LPSTR_TEXTCALLBACKW; /* FIXME:send notification? */
1955 ERR(" GetItem called with LPSTR_TEXTCALLBACK\n");
1957 else if (wineItem->pszText) {
1958 lstrcpynAtoW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
1961 #endif
1962 wineItem->pszText = NULL;
1963 TRACE("item %d<%p>, txt %p, img %p, action %x\n",
1964 iItem, tvItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
1965 return TRUE;
1968 static LRESULT
1969 TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
1971 TRACE("\n");
1973 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
1974 return 0;
1976 return (wineItem->state & mask);
1979 static LRESULT
1980 TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
1982 TREEVIEW_ITEM *retval;
1984 retval = 0;
1986 /* handle all the global data here */
1987 switch (which)
1989 case TVGN_CHILD: /* Special case: child of 0 is root */
1990 if (wineItem)
1991 break;
1992 /* fall through */
1993 case TVGN_ROOT:
1994 retval = infoPtr->root->firstChild;
1995 break;
1997 case TVGN_CARET:
1998 retval = infoPtr->selectedItem;
1999 break;
2001 case TVGN_FIRSTVISIBLE:
2002 retval = infoPtr->firstVisible;
2003 break;
2005 case TVGN_DROPHILITE:
2006 retval = infoPtr->dropItem;
2007 break;
2009 case TVGN_LASTVISIBLE:
2010 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2011 break;
2014 if (retval)
2016 TRACE("flags:%x, returns %p\n", which, retval);
2017 return (LRESULT)retval;
2020 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2021 return FALSE;
2023 switch (which)
2025 case TVGN_NEXT:
2026 retval = wineItem->nextSibling;
2027 break;
2028 case TVGN_PREVIOUS:
2029 retval = wineItem->prevSibling;
2030 break;
2031 case TVGN_PARENT:
2032 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2033 break;
2034 case TVGN_CHILD:
2035 retval = wineItem->firstChild;
2036 break;
2037 case TVGN_NEXTVISIBLE:
2038 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2039 break;
2040 case TVGN_PREVIOUSVISIBLE:
2041 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2042 break;
2043 default:
2044 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2045 break;
2048 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2049 return (LRESULT)retval;
2053 static LRESULT
2054 TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2056 TRACE(" %d\n", infoPtr->uNumItems);
2057 return (LRESULT)infoPtr->uNumItems;
2060 static VOID
2061 TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2063 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2065 static const unsigned int state_table[] = { 0, 2, 1 };
2067 unsigned int state;
2069 state = STATEIMAGEINDEX(item->state);
2070 TRACE("state:%x\n", state);
2071 item->state &= ~TVIS_STATEIMAGEMASK;
2073 if (state < 3)
2074 state = state_table[state];
2076 item->state |= INDEXTOSTATEIMAGEMASK(state);
2078 TRACE("state:%x\n", state);
2079 TREEVIEW_QueueItemRefresh(infoPtr, item);
2084 /* Painting *************************************************************/
2086 /* Draw the lines and expand button for an item. Also draws one section
2087 * of the line from item's parent to item's parent's next sibling. */
2088 static void
2089 TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2091 LONG centerx, centery;
2092 BOOL lar = ((infoPtr->dwStyle
2093 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2094 > TVS_LINESATROOT);
2096 if (!lar && item->iLevel == 0)
2097 return;
2099 centerx = (item->linesOffset + item->stateOffset) / 2;
2100 centery = (item->rect.top + item->rect.bottom) / 2;
2102 if (infoPtr->dwStyle & TVS_HASLINES)
2104 HPEN hOldPen, hNewPen;
2105 HTREEITEM parent;
2108 * Get a dotted grey pen
2110 hNewPen = CreatePen(PS_DOT, 0, infoPtr->clrLine);
2111 hOldPen = SelectObject(hdc, hNewPen);
2113 MoveToEx(hdc, item->stateOffset, centery, NULL);
2114 LineTo(hdc, centerx - 1, centery);
2116 if (item->prevSibling || item->parent != infoPtr->root)
2118 MoveToEx(hdc, centerx, item->rect.top, NULL);
2119 LineTo(hdc, centerx, centery);
2122 if (item->nextSibling)
2124 MoveToEx(hdc, centerx, centery, NULL);
2125 LineTo(hdc, centerx, item->rect.bottom + 1);
2128 /* Draw the line from our parent to its next sibling. */
2129 parent = item->parent;
2130 while (parent != infoPtr->root)
2132 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2134 if (parent->nextSibling
2135 /* skip top-levels unless TVS_LINESATROOT */
2136 && parent->stateOffset > parent->linesOffset)
2138 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2139 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2142 parent = parent->parent;
2145 SelectObject(hdc, hOldPen);
2146 DeleteObject(hNewPen);
2150 * Display the (+/-) signs
2153 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2155 if (item->cChildren)
2157 LONG height = item->rect.bottom - item->rect.top;
2158 LONG width = item->stateOffset - item->linesOffset;
2159 LONG rectsize = min(height, width) / 4;
2160 /* plussize = ceil(rectsize * 3/4) */
2161 LONG plussize = (rectsize + 1) * 3 / 4;
2163 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2164 HPEN hOldPen = SelectObject(hdc, hNewPen);
2165 HBRUSH hbr = CreateSolidBrush(infoPtr->clrBk);
2166 HBRUSH hbrOld = SelectObject(hdc, hbr);
2168 Rectangle(hdc, centerx - rectsize, centery - rectsize,
2169 centerx + rectsize + 1, centery + rectsize + 1);
2171 SelectObject(hdc, hbrOld);
2172 DeleteObject(hbr);
2174 SelectObject(hdc, hOldPen);
2175 DeleteObject(hNewPen);
2177 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2178 LineTo(hdc, centerx + plussize, centery);
2180 if (!(item->state & TVIS_EXPANDED))
2182 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2183 LineTo(hdc, centerx, centery + plussize);
2189 static void
2190 TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2192 INT cditem;
2193 HFONT hOldFont;
2194 int centery;
2196 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2198 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2200 /* The custom draw handler can query the text rectangle,
2201 * so get ready. */
2202 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2204 cditem = 0;
2206 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2208 cditem = TREEVIEW_SendCustomDrawItemNotify
2209 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT);
2210 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2212 if (cditem & CDRF_SKIPDEFAULT)
2214 SelectObject(hdc, hOldFont);
2215 return;
2219 if (cditem & CDRF_NEWFONT)
2220 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2222 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2224 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2227 * Display the images associated with this item
2230 INT imageIndex;
2232 /* State images are displayed to the left of the Normal image
2233 * image number is in state; zero should be `display no image'.
2235 imageIndex = STATEIMAGEINDEX(wineItem->state);
2237 if (infoPtr->himlState && imageIndex)
2239 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2240 wineItem->stateOffset,
2241 centery - infoPtr->stateImageHeight / 2,
2242 ILD_NORMAL);
2245 /* Now, draw the normal image; can be either selected or
2246 * non-selected image.
2249 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage))
2251 /* The item is curently selected */
2252 imageIndex = wineItem->iSelectedImage;
2254 else
2256 /* The item is not selected */
2257 imageIndex = wineItem->iImage;
2260 if (infoPtr->himlNormal)
2262 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2264 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2265 wineItem->imageOffset,
2266 centery - infoPtr->normalImageHeight / 2,
2267 ILD_NORMAL | ovlIdx);
2273 * Display the text associated with this item
2276 /* Don't paint item's text if it's being edited */
2277 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2279 if (wineItem->pszText)
2281 COLORREF oldTextColor = 0;
2282 INT oldBkMode;
2283 HBRUSH hbrBk = 0;
2284 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2285 RECT rcText;
2287 oldBkMode = SetBkMode(hdc, TRANSPARENT);
2289 /* - If item is drop target or it is selected and window is in focus -
2290 * use blue background (COLOR_HIGHLIGHT).
2291 * - If item is selected, window is not in focus, but it has style
2292 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2293 * - Otherwise - don't fill background
2295 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2296 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2297 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2299 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2301 hbrBk = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
2302 oldTextColor =
2303 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2305 else
2307 hbrBk = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
2309 if (infoPtr->clrText == -1)
2310 oldTextColor =
2311 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2312 else
2313 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2316 else
2318 if (infoPtr->clrText == -1)
2319 oldTextColor =
2320 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2321 else
2322 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2325 rcText.top = wineItem->rect.top;
2326 rcText.bottom = wineItem->rect.bottom;
2327 rcText.left = wineItem->textOffset;
2328 rcText.right = rcText.left + wineItem->textWidth + 4;
2330 if (hbrBk)
2332 FillRect(hdc, &rcText, hbrBk);
2333 DeleteObject(hbrBk);
2336 /* Draw the box arround the selected item */
2337 if ((wineItem == infoPtr->selectedItem) && inFocus)
2339 HPEN hNewPen = CreatePen(PS_DOT, 0,
2340 GetSysColor(COLOR_WINDOWTEXT));
2341 HPEN hOldPen = SelectObject(hdc, hNewPen);
2342 INT rop = SetROP2(hdc, R2_XORPEN);
2343 POINT points[5];
2345 points[4].x = points[0].x = rcText.left;
2346 points[4].y = points[0].y = rcText.top;
2347 points[1].x = rcText.right - 1;
2348 points[1].y = rcText.top;
2349 points[2].x = rcText.right - 1;
2350 points[2].y = rcText.bottom - 1;
2351 points[3].x = rcText.left;
2352 points[3].y = rcText.bottom - 1;
2354 Polyline(hdc, points, 5);
2356 SetROP2(hdc, rop);
2357 SelectObject(hdc, hOldPen);
2358 DeleteObject(hNewPen);
2361 rcText.left += 2;
2362 rcText.right -= 2;
2364 /* Draw it */
2365 DrawTextA(hdc,
2366 wineItem->pszText,
2367 lstrlenA(wineItem->pszText),
2368 &rcText,
2369 DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
2371 /* Restore the hdc state */
2372 SetTextColor(hdc, oldTextColor);
2374 if (oldBkMode != TRANSPARENT)
2375 SetBkMode(hdc, oldBkMode);
2379 /* Draw insertion mark if necessary */
2381 if (infoPtr->insertMarkItem)
2382 TRACE("item:%d,mark:%d\n",
2383 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2384 (int)infoPtr->insertMarkItem);
2386 if (wineItem == infoPtr->insertMarkItem)
2388 HPEN hNewPen, hOldPen;
2389 int offset;
2390 int left, right;
2392 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2393 hOldPen = SelectObject(hdc, hNewPen);
2395 if (infoPtr->insertBeforeorAfter)
2396 offset = wineItem->rect.bottom - 1;
2397 else
2398 offset = wineItem->rect.top + 1;
2400 left = wineItem->textOffset - 2;
2401 right = wineItem->textOffset + wineItem->textWidth + 2;
2403 MoveToEx(hdc, left, offset - 3, NULL);
2404 LineTo(hdc, left, offset + 4);
2406 MoveToEx(hdc, left, offset, NULL);
2407 LineTo(hdc, right + 1, offset);
2409 MoveToEx(hdc, right, offset + 3, NULL);
2410 LineTo(hdc, right, offset - 4);
2412 SelectObject(hdc, hOldPen);
2413 DeleteObject(hNewPen);
2416 if (cditem & CDRF_NOTIFYPOSTPAINT)
2418 cditem = TREEVIEW_SendCustomDrawItemNotify
2419 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT);
2420 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2423 SelectObject(hdc, hOldFont);
2426 /* Computes treeHeight and treeWidth and updates the scroll bars.
2428 static void
2429 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2431 TREEVIEW_ITEM *wineItem;
2432 HWND hwnd = infoPtr->hwnd;
2433 BOOL vert = FALSE;
2434 BOOL horz = FALSE;
2435 SCROLLINFO si;
2436 LONG scrollX = infoPtr->scrollX;
2438 infoPtr->treeWidth = 0;
2439 infoPtr->treeHeight = 0;
2441 /* We iterate through all visible items in order to get the tree height
2442 * and width */
2443 wineItem = infoPtr->root->firstChild;
2445 while (wineItem != NULL)
2447 if (ISVISIBLE(wineItem))
2449 /* actually we draw text at textOffset + 2 */
2450 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2451 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2453 /* This is scroll-adjusted, but we fix this below. */
2454 infoPtr->treeHeight = wineItem->rect.bottom;
2457 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2460 /* Fix the scroll adjusted treeHeight and treeWidth. */
2461 if (infoPtr->root->firstChild)
2462 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2464 infoPtr->treeWidth += infoPtr->scrollX;
2466 /* Adding one scroll bar may take up enough space that it forces us
2467 * to add the other as well. */
2468 if (infoPtr->treeHeight > infoPtr->clientHeight)
2470 vert = TRUE;
2472 if (infoPtr->treeWidth
2473 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2474 horz = TRUE;
2476 else if (infoPtr->treeWidth > infoPtr->clientWidth)
2477 horz = TRUE;
2479 if (!vert && horz && infoPtr->treeHeight
2480 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2481 vert = TRUE;
2483 si.cbSize = sizeof(SCROLLINFO);
2484 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2485 si.nMin = 0;
2487 if (vert)
2489 infoPtr->uInternalStatus |= TV_VSCROLL;
2491 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2492 si.nPos = infoPtr->firstVisible->visibleOrder;
2493 si.nMax = infoPtr->maxVisibleOrder - 1;
2495 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2497 else
2499 if (infoPtr->uInternalStatus & TV_VSCROLL)
2500 ShowScrollBar(hwnd, SB_VERT, FALSE);
2502 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2505 if (horz)
2507 infoPtr->uInternalStatus |= TV_HSCROLL;
2509 si.nPage = infoPtr->clientWidth;
2510 si.nPos = infoPtr->scrollX;
2511 si.nMax = infoPtr->treeWidth - 1;
2513 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2515 si.nPos = si.nMax - max( si.nPage-1, 0 );
2516 scrollX = si.nPos;
2519 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2521 else
2523 if (infoPtr->uInternalStatus & TV_HSCROLL)
2524 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2526 scrollX = 0;
2529 if (infoPtr->scrollX != scrollX)
2531 TREEVIEW_HScroll(infoPtr,
2532 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2535 if (!horz)
2536 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2539 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2540 static LRESULT
2541 TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2543 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2544 RECT rect;
2546 GetClientRect(infoPtr->hwnd, &rect);
2547 FillRect(hDC, &rect, hBrush);
2548 DeleteObject(hBrush);
2550 return 1;
2553 static void
2554 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2556 HWND hwnd = infoPtr->hwnd;
2557 RECT rect = *rc;
2558 TREEVIEW_ITEM *wineItem;
2560 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2562 TRACE("empty window\n");
2563 return;
2566 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2567 hdc, rect);
2569 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2571 ReleaseDC(hwnd, hdc);
2572 return;
2575 for (wineItem = infoPtr->root->firstChild;
2576 wineItem != NULL;
2577 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2579 if (ISVISIBLE(wineItem))
2581 /* Avoid unneeded calculations */
2582 if (wineItem->rect.top > rect.bottom)
2583 break;
2584 if (wineItem->rect.bottom < rect.top)
2585 continue;
2587 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2591 TREEVIEW_UpdateScrollBars(infoPtr);
2593 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2594 infoPtr->cdmode =
2595 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2598 static void
2599 TREEVIEW_QueueRefresh(TREEVIEW_INFO *infoPtr)
2601 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2604 /* It be that item->rect is out of date. If so, we invalidate the wrong area,
2605 * but then whoever updates item->rect knows that they must invalidate after
2606 * correcting it. */
2607 static void
2608 TREEVIEW_QueueItemRefresh(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2610 if (item != NULL)
2611 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2614 static LRESULT
2615 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2617 HDC hdc;
2618 PAINTSTRUCT ps;
2619 RECT rc;
2621 TRACE("\n");
2623 if (wParam)
2625 hdc = (HDC)wParam;
2626 GetUpdateRect(infoPtr->hwnd, &rc, TRUE);
2628 else
2630 hdc = BeginPaint(infoPtr->hwnd, &ps);
2631 rc = ps.rcPaint;
2634 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2635 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2637 if (!wParam)
2638 EndPaint(infoPtr->hwnd, &ps);
2640 return 0;
2644 /* Sorting **************************************************************/
2646 /***************************************************************************
2647 * Forward the DPA local callback to the treeview owner callback
2649 static INT WINAPI
2650 TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2652 /* Forward the call to the client-defined callback */
2653 return pCallBackSort->lpfnCompare(first->lParam,
2654 second->lParam,
2655 pCallBackSort->lParam);
2658 /***************************************************************************
2659 * Treeview native sort routine: sort on item text.
2661 static INT WINAPI
2662 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2663 TREEVIEW_INFO *infoPtr)
2665 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2666 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2668 return strcasecmp(first->pszText, second->pszText);
2671 /* Returns the number of physical children belonging to item. */
2672 static INT
2673 TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2675 INT cChildren = 0;
2676 HTREEITEM hti;
2678 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2679 cChildren++;
2681 return cChildren;
2684 /* Returns a DPA containing a pointer to each physical child of item in
2685 * sibling order. If item has no children, an empty DPA is returned. */
2686 static HDPA
2687 TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2689 HTREEITEM child = item->firstChild;
2691 HDPA list = DPA_Create(8);
2692 if (list == 0) return NULL;
2694 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2696 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2698 DPA_Destroy(list);
2699 return NULL;
2703 return list;
2706 /***************************************************************************
2707 * Setup the treeview structure with regards of the sort method
2708 * and sort the children of the TV item specified in lParam
2709 * fRecurse: currently unused. Should be zero.
2710 * parent: if pSort!=NULL, should equal pSort->hParent.
2711 * otherwise, item which child items are to be sorted.
2712 * pSort: sort method info. if NULL, sort on item text.
2713 * if non-NULL, sort on item's lParam content, and let the
2714 * application decide what that means. See also TVM_SORTCHILDRENCB.
2717 static LRESULT
2718 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2719 LPTVSORTCB pSort)
2721 INT cChildren;
2722 PFNDPACOMPARE pfnCompare;
2723 LPARAM lpCompare;
2725 /* undocumented feature: TVI_ROOT means `sort the whole tree' */
2726 if (parent == TVI_ROOT)
2727 parent = infoPtr->root;
2729 /* Check for a valid handle to the parent item */
2730 if (!TREEVIEW_ValidItem(infoPtr, parent))
2732 ERR("invalid item hParent=%x\n", (INT)parent);
2733 return FALSE;
2736 if (pSort)
2738 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2739 lpCompare = (LPARAM)pSort;
2741 else
2743 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2744 lpCompare = (LPARAM)infoPtr;
2747 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2749 /* Make sure there is something to sort */
2750 if (cChildren > 1)
2752 /* TREEVIEW_ITEM rechaining */
2753 INT count = 0;
2754 HTREEITEM item = 0;
2755 HTREEITEM nextItem = 0;
2756 HTREEITEM prevItem = 0;
2758 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
2760 if (sortList == NULL)
2761 return FALSE;
2763 /* let DPA sort the list */
2764 DPA_Sort(sortList, pfnCompare, lpCompare);
2766 /* The order of DPA entries has been changed, so fixup the
2767 * nextSibling and prevSibling pointers. */
2769 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
2770 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
2772 /* link the two current item toghether */
2773 item->nextSibling = nextItem;
2774 nextItem->prevSibling = item;
2776 if (prevItem == NULL)
2778 /* this is the first item, update the parent */
2779 parent->firstChild = item;
2780 item->prevSibling = NULL;
2782 else
2784 /* fix the back chaining */
2785 item->prevSibling = prevItem;
2788 /* get ready for the next one */
2789 prevItem = item;
2790 item = nextItem;
2793 /* the last item is pointed to by item and never has a sibling */
2794 item->nextSibling = NULL;
2795 parent->lastChild = item;
2797 DPA_Destroy(sortList);
2799 TREEVIEW_VerifyTree(infoPtr);
2801 if (parent->state & TVIS_EXPANDED)
2803 int visOrder = infoPtr->firstVisible->visibleOrder;
2805 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
2807 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
2809 TREEVIEW_ITEM *item;
2811 for (item = infoPtr->root->firstChild; item != NULL;
2812 item = TREEVIEW_GetNextListItem(infoPtr, item))
2814 if (item->visibleOrder == visOrder)
2815 break;
2818 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
2821 TREEVIEW_QueueRefresh(infoPtr);
2824 return TRUE;
2826 return FALSE;
2830 /***************************************************************************
2831 * Setup the treeview structure with regards of the sort method
2832 * and sort the children of the TV item specified in lParam
2834 static LRESULT
2835 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
2837 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
2841 /***************************************************************************
2842 * Sort the children of the TV item specified in lParam.
2844 static LRESULT
2845 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2847 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
2851 /* Expansion/Collapse ***************************************************/
2853 static BOOL
2854 TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2855 UINT action)
2857 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGA, action,
2858 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
2859 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
2860 0, wineItem);
2863 static VOID
2864 TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2865 UINT action)
2867 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDA, action,
2868 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
2869 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
2870 0, wineItem);
2874 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
2875 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
2876 static BOOL
2877 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2878 BOOL bRemoveChildren, BOOL bUser)
2880 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
2881 BOOL bSetSelection, bSetFirstVisible;
2883 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
2885 if (!(wineItem->state & TVIS_EXPANDED) || wineItem->firstChild == NULL)
2886 return FALSE;
2888 if (bUser)
2889 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
2891 wineItem->state &= ~TVIS_EXPANDED;
2893 if (bUser)
2894 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
2896 bSetSelection = (infoPtr->selectedItem != NULL
2897 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
2899 bSetFirstVisible = (infoPtr->firstVisible != NULL
2900 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
2902 if (bRemoveChildren)
2904 TRACE("TVE_COLLAPSERESET\n");
2905 wineItem->state &= ~TVIS_EXPANDEDONCE;
2906 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
2909 if (wineItem->firstChild)
2911 TREEVIEW_ITEM *item, *sibling;
2913 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2915 for (item = wineItem->firstChild; item != sibling;
2916 item = TREEVIEW_GetNextListItem(infoPtr, item))
2918 item->visibleOrder = -1;
2922 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2924 TREEVIEW_SetFirstVisible(infoPtr, bSetFirstVisible ? wineItem
2925 : infoPtr->firstVisible, TRUE);
2927 if (bSetSelection)
2929 /* Don't call DoSelectItem, it sends notifications. */
2930 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
2931 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
2932 wineItem->state |= TVIS_SELECTED;
2933 infoPtr->selectedItem = wineItem;
2935 TREEVIEW_EnsureVisible(infoPtr, wineItem, FALSE);
2938 TREEVIEW_UpdateScrollBars(infoPtr);
2939 TREEVIEW_QueueRefresh(infoPtr);
2941 return TRUE;
2944 static BOOL
2945 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
2946 BOOL bExpandPartial, BOOL bUser)
2948 TRACE("\n");
2950 if (!TREEVIEW_HasChildren(infoPtr, wineItem)
2951 || wineItem->state & TVIS_EXPANDED)
2952 return FALSE;
2954 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
2956 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
2958 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
2960 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
2961 return FALSE;
2964 wineItem->state |= TVIS_EXPANDED;
2965 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
2966 wineItem->state |= TVIS_EXPANDEDONCE;
2968 else
2970 /* this item has already been expanded */
2971 wineItem->state |= TVIS_EXPANDED;
2974 if (bExpandPartial)
2975 FIXME("TVE_EXPANDPARTIAL not implemented\n");
2977 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2978 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
2979 TREEVIEW_UpdateScrollBars(infoPtr);
2981 /* Scroll up so that as many children as possible are visible.
2982 * This looses when expanding causes an HScroll bar to appear, but we
2983 * don't know that yet, so the last item is obscured. */
2984 if (wineItem->firstChild != NULL)
2986 int nChildren = wineItem->lastChild->visibleOrder
2987 - wineItem->firstChild->visibleOrder + 1;
2989 int visible_pos = wineItem->visibleOrder
2990 - infoPtr->firstVisible->visibleOrder;
2992 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
2994 if (visible_pos > 0 && nChildren > rows_below)
2996 int scroll = nChildren - rows_below;
2998 if (scroll > visible_pos)
2999 scroll = visible_pos;
3001 if (scroll > 0)
3003 TREEVIEW_ITEM *newFirstVisible
3004 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3005 scroll);
3008 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3013 TREEVIEW_QueueRefresh(infoPtr);
3015 return TRUE;
3018 static BOOL
3019 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3021 TRACE("\n");
3023 if (wineItem->state & TVIS_EXPANDED)
3024 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3025 else
3026 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3029 static VOID
3030 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3032 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3034 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3036 if (TREEVIEW_HasChildren(infoPtr, item))
3037 TREEVIEW_ExpandAll(infoPtr, item);
3041 /* Note:If the specified item is the child of a collapsed parent item,
3042 the parent's list of child items is (recursively) expanded to reveal the
3043 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3044 know if it also applies here.
3047 static LRESULT
3048 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3050 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3051 return 0;
3053 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3054 TREEVIEW_ItemName(wineItem), flag,
3055 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3057 switch (flag & TVE_TOGGLE)
3059 case TVE_COLLAPSE:
3060 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3061 FALSE);
3063 case TVE_EXPAND:
3064 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3065 FALSE);
3067 case TVE_TOGGLE:
3068 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3070 default:
3071 return 0;
3074 #if 0
3075 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3076 #endif
3079 /* Hit-Testing **********************************************************/
3081 static TREEVIEW_ITEM *
3082 TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3084 TREEVIEW_ITEM *wineItem;
3085 LONG row;
3087 if (!infoPtr->firstVisible)
3088 return NULL;
3090 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3092 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3093 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3095 if (row >= wineItem->visibleOrder
3096 && row < wineItem->visibleOrder + wineItem->iIntegral)
3097 break;
3100 return wineItem;
3103 static LRESULT
3104 TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3106 TREEVIEW_ITEM *wineItem;
3107 RECT rect;
3108 UINT status;
3109 LONG x, y;
3111 lpht->hItem = 0;
3112 GetClientRect(infoPtr->hwnd, &rect);
3113 status = 0;
3114 x = lpht->pt.x;
3115 y = lpht->pt.y;
3117 if (x < rect.left)
3119 status |= TVHT_TOLEFT;
3121 else if (x > rect.right)
3123 status |= TVHT_TORIGHT;
3126 if (y < rect.top)
3128 status |= TVHT_ABOVE;
3130 else if (y > rect.bottom)
3132 status |= TVHT_BELOW;
3135 if (status)
3137 lpht->flags = status;
3138 return (LRESULT)(HTREEITEM)NULL;
3141 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3142 if (!wineItem)
3144 lpht->flags = TVHT_NOWHERE;
3145 return (LRESULT)(HTREEITEM)NULL;
3148 if (x >= wineItem->textOffset + wineItem->textWidth)
3150 lpht->flags = TVHT_ONITEMRIGHT;
3152 else if (x >= wineItem->textOffset)
3154 lpht->flags = TVHT_ONITEMLABEL;
3156 else if (x >= wineItem->imageOffset)
3158 lpht->flags = TVHT_ONITEMICON;
3160 else if (x >= wineItem->stateOffset)
3162 lpht->flags = TVHT_ONITEMSTATEICON;
3164 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3166 lpht->flags = TVHT_ONITEMBUTTON;
3168 else
3170 lpht->flags = TVHT_ONITEMINDENT;
3173 lpht->hItem = wineItem;
3174 TRACE("(%ld,%ld):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3176 return (LRESULT)wineItem;
3179 /* Item Label Editing ***************************************************/
3181 static LRESULT
3182 TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3184 return infoPtr->hwndEdit;
3187 static LRESULT CALLBACK
3188 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3190 TREEVIEW_INFO *infoPtr;
3191 BOOL bCancel = FALSE;
3193 switch (uMsg)
3195 case WM_PAINT:
3197 LRESULT rc;
3198 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3200 TRACE("WM_PAINT start\n");
3201 rc = CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3202 lParam);
3203 TRACE("WM_PAINT done\n");
3204 return rc;
3207 case WM_KILLFOCUS:
3209 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3210 if (infoPtr->bIgnoreEditKillFocus)
3211 return TRUE;
3213 break;
3216 case WM_GETDLGCODE:
3217 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3219 case WM_KEYDOWN:
3220 if (wParam == (WPARAM)VK_ESCAPE)
3222 bCancel = TRUE;
3223 break;
3225 else if (wParam == (WPARAM)VK_RETURN)
3227 break;
3230 /* fall through */
3231 default:
3233 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3235 return CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3236 lParam);
3240 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3241 /* eg. Using a messagebox */
3243 infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3244 infoPtr->bIgnoreEditKillFocus = TRUE;
3245 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3246 infoPtr->bIgnoreEditKillFocus = FALSE;
3248 return 0;
3252 /* should handle edit control messages here */
3254 static LRESULT
3255 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3257 TRACE("%x %ld\n", wParam, lParam);
3259 switch (HIWORD(wParam))
3261 case EN_UPDATE:
3264 * Adjust the edit window size
3266 char buffer[1024];
3267 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3268 HDC hdc = GetDC(infoPtr->hwndEdit);
3269 SIZE sz;
3270 int len;
3271 HFONT hFont, hOldFont = 0;
3273 infoPtr->bLabelChanged = TRUE;
3275 len = GetWindowTextA(infoPtr->hwndEdit, buffer, sizeof(buffer));
3277 /* Select font to get the right dimension of the string */
3278 hFont = SendMessageA(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3279 if (hFont != 0)
3281 hOldFont = SelectObject(hdc, hFont);
3284 if (GetTextExtentPoint32A(hdc, buffer, strlen(buffer), &sz))
3286 TEXTMETRICA textMetric;
3288 /* Add Extra spacing for the next character */
3289 GetTextMetricsA(hdc, &textMetric);
3290 sz.cx += (textMetric.tmMaxCharWidth * 2);
3292 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3293 sz.cx = min(sz.cx,
3294 infoPtr->clientWidth - editItem->textOffset + 2);
3296 SetWindowPos(infoPtr->hwndEdit,
3297 HWND_TOP,
3300 sz.cx,
3301 editItem->rect.bottom - editItem->rect.top + 3,
3302 SWP_NOMOVE | SWP_DRAWFRAME);
3305 if (hFont != 0)
3307 SelectObject(hdc, hOldFont);
3310 ReleaseDC(infoPtr->hwnd, hdc);
3311 break;
3314 default:
3315 return SendMessageA(GetParent(infoPtr->hwnd), WM_COMMAND, wParam, lParam);
3318 return 0;
3321 static HWND
3322 TREEVIEW_EditLabelA(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3324 HWND hwnd = infoPtr->hwnd;
3325 HWND hwndEdit;
3326 SIZE sz;
3327 TREEVIEW_ITEM *editItem = hItem;
3328 HINSTANCE hinst = GetWindowLongA(hwnd, GWL_HINSTANCE);
3329 HDC hdc;
3330 HFONT hOldFont=0;
3331 TEXTMETRICA textMetric;
3333 TRACE("%x %p\n", (unsigned)hwnd, hItem);
3334 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3335 return (HWND)NULL;
3337 if (infoPtr->hwndEdit)
3338 return infoPtr->hwndEdit;
3340 infoPtr->bLabelChanged = FALSE;
3342 /* Make sure that edit item is selected */
3343 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3344 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3346 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3348 hdc = GetDC(hwnd);
3349 /* Select the font to get appropriate metric dimensions */
3350 if (infoPtr->hFont != 0)
3352 hOldFont = SelectObject(hdc, infoPtr->hFont);
3355 /*Get String Lenght in pixels */
3356 GetTextExtentPoint32A(hdc, editItem->pszText, strlen(editItem->pszText),
3357 &sz);
3359 /*Add Extra spacing for the next character */
3360 GetTextMetricsA(hdc, &textMetric);
3361 sz.cx += (textMetric.tmMaxCharWidth * 2);
3363 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3364 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3366 if (infoPtr->hFont != 0)
3368 SelectObject(hdc, hOldFont);
3371 ReleaseDC(hwnd, hdc);
3372 hwndEdit = CreateWindowExA(WS_EX_LEFT,
3373 "EDIT",
3375 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3376 WS_CLIPSIBLINGS | ES_WANTRETURN |
3377 ES_LEFT, editItem->textOffset - 2,
3378 editItem->rect.top - 1, sz.cx + 3,
3379 editItem->rect.bottom -
3380 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3381 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3383 infoPtr->hwndEdit = hwndEdit;
3385 /* Get a 2D border. */
3386 SetWindowLongA(hwndEdit, GWL_EXSTYLE,
3387 GetWindowLongA(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3388 SetWindowLongA(hwndEdit, GWL_STYLE,
3389 GetWindowLongA(hwndEdit, GWL_STYLE) | WS_BORDER);
3391 SendMessageA(hwndEdit, WM_SETFONT, TREEVIEW_FontForItem(infoPtr, editItem),
3392 FALSE);
3394 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongA(hwndEdit, GWL_WNDPROC,
3395 (DWORD)
3396 TREEVIEW_Edit_SubclassProc);
3398 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3400 DestroyWindow(hwndEdit);
3401 infoPtr->hwndEdit = 0;
3402 return (HWND)NULL;
3405 infoPtr->selectedItem = hItem;
3406 SetWindowTextA(hwndEdit, editItem->pszText);
3407 SetFocus(hwndEdit);
3408 SendMessageA(hwndEdit, EM_SETSEL, 0, -1);
3409 ShowWindow(hwndEdit, SW_SHOW);
3411 return hwndEdit;
3415 static LRESULT
3416 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3418 HWND hwnd = infoPtr->hwnd;
3419 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3420 NMTVDISPINFOA tvdi;
3421 BOOL bCommit;
3422 char tmpText[1024] = { '\0' };
3423 int iLength = 0;
3425 if (!infoPtr->hwndEdit)
3426 return FALSE;
3428 tvdi.hdr.hwndFrom = hwnd;
3429 tvdi.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
3430 tvdi.hdr.code = TVN_ENDLABELEDITA;
3431 tvdi.item.mask = 0;
3432 tvdi.item.hItem = editedItem;
3433 tvdi.item.state = editedItem->state;
3434 tvdi.item.lParam = editedItem->lParam;
3436 if (!bCancel)
3438 iLength = GetWindowTextA(infoPtr->hwndEdit, tmpText, 1023);
3440 if (iLength >= 1023)
3442 ERR("Insuficient space to retrieve new item label.");
3445 tvdi.item.pszText = tmpText;
3446 tvdi.item.cchTextMax = iLength + 1;
3448 else
3450 tvdi.item.pszText = NULL;
3451 tvdi.item.cchTextMax = 0;
3454 bCommit = (BOOL)SendMessageA(GetParent(hwnd),
3455 WM_NOTIFY,
3456 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3458 if (!bCancel && bCommit) /* Apply the changes */
3460 if (strcmp(tmpText, editedItem->pszText) != 0)
3462 if (NULL == COMCTL32_ReAlloc(editedItem->pszText, iLength + 1))
3464 ERR("OutOfMemory, cannot allocate space for label");
3465 DestroyWindow(infoPtr->hwndEdit);
3466 infoPtr->hwndEdit = 0;
3467 return FALSE;
3469 else
3471 editedItem->cchTextMax = iLength + 1;
3472 lstrcpyA(editedItem->pszText, tmpText);
3477 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3478 DestroyWindow(infoPtr->hwndEdit);
3479 infoPtr->hwndEdit = 0;
3480 return TRUE;
3483 static LRESULT
3484 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3486 if (wParam != TV_EDIT_TIMER)
3488 ERR("got unknown timer\n");
3489 return 1;
3492 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3493 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3495 TREEVIEW_EditLabelA(infoPtr, infoPtr->selectedItem);
3497 return 0;
3501 /* Mouse Tracking/Drag **************************************************/
3503 /***************************************************************************
3504 * This is quite unusual piece of code, but that's how it's implemented in
3505 * Windows.
3507 static LRESULT
3508 TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3510 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3511 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3512 RECT r;
3513 MSG msg;
3515 r.top = pt.y - cyDrag;
3516 r.left = pt.x - cxDrag;
3517 r.bottom = pt.y + cyDrag;
3518 r.right = pt.x + cxDrag;
3520 SetCapture(infoPtr->hwnd);
3522 while (1)
3524 if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3526 if (msg.message == WM_MOUSEMOVE)
3528 pt.x = (LONG)(INT16)LOWORD(msg.lParam);
3529 pt.y = (LONG)(INT16)HIWORD(msg.lParam);
3530 if (PtInRect(&r, pt))
3531 continue;
3532 else
3534 ReleaseCapture();
3535 return 1;
3538 else if (msg.message >= WM_LBUTTONDOWN &&
3539 msg.message <= WM_RBUTTONDBLCLK)
3541 if (msg.message == WM_RBUTTONUP)
3542 TREEVIEW_RButtonUp(infoPtr, &pt);
3543 break;
3546 DispatchMessageA(&msg);
3549 if (GetCapture() != infoPtr->hwnd)
3550 return 0;
3553 ReleaseCapture();
3554 return 0;
3558 static LRESULT
3559 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3561 TREEVIEW_ITEM *wineItem;
3562 TVHITTESTINFO hit;
3564 TRACE("\n");
3565 SetFocus(infoPtr->hwnd);
3567 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3569 /* If there is pending 'edit label' event - kill it now */
3570 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3573 hit.pt.x = (LONG)(INT16)LOWORD(lParam);
3574 hit.pt.y = (LONG)(INT16)HIWORD(lParam);
3576 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3577 if (!wineItem)
3578 return 0;
3579 TRACE("item %d \n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3581 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3582 { /* FIXME! */
3583 switch (hit.flags)
3585 case TVHT_ONITEMRIGHT:
3586 /* FIXME: we should not have send NM_DBLCLK in this case. */
3587 break;
3589 case TVHT_ONITEMINDENT:
3590 if (!(infoPtr->dwStyle & TVS_HASLINES))
3592 break;
3594 else
3596 int level = hit.pt.x / infoPtr->uIndent;
3597 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3599 while (wineItem->iLevel > level)
3601 wineItem = wineItem->parent;
3604 /* fall through */
3607 case TVHT_ONITEMLABEL:
3608 case TVHT_ONITEMICON:
3609 case TVHT_ONITEMBUTTON:
3610 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3611 break;
3613 case TVHT_ONITEMSTATEICON:
3614 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3615 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3616 else
3617 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3618 break;
3621 return TRUE;
3625 static LRESULT
3626 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3628 HWND hwnd = infoPtr->hwnd;
3629 TVHITTESTINFO ht;
3630 BOOL bTrack;
3631 HTREEITEM tempItem;
3633 /* If Edit control is active - kill it and return.
3634 * The best way to do it is to set focus to itself.
3635 * Edit control subclassed procedure will automatically call
3636 * EndEditLabelNow.
3638 if (infoPtr->hwndEdit)
3640 SetFocus(hwnd);
3641 return 0;
3644 ht.pt.x = (LONG)(INT16)LOWORD(lParam);
3645 ht.pt.y = (LONG)(INT16)HIWORD(lParam);
3647 TREEVIEW_HitTest(infoPtr, &ht);
3648 TRACE("item %d \n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3650 /* update focusedItem and redraw both items */
3651 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3653 infoPtr->focusedItem = ht.hItem;
3654 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3656 if(infoPtr->selectedItem)
3657 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3660 bTrack = (ht.flags & TVHT_ONITEM)
3661 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3663 /* Send NM_CLICK right away */
3664 if (!bTrack)
3665 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3666 goto setfocus;
3668 if (ht.flags & TVHT_ONITEMBUTTON)
3670 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
3671 goto setfocus;
3673 else if (bTrack)
3674 { /* if TREEVIEW_TrackMouse == 1 dragging occured and the cursor left the dragged item's rectangle */
3675 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3677 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGA, ht.hItem,
3678 ht.pt);
3679 infoPtr->dropItem = ht.hItem;
3681 /* clean up focuedItem as we dragged and won't select this item */
3682 if(infoPtr->focusedItem)
3684 /* refresh the item that was focused */
3685 tempItem = infoPtr->focusedItem;
3686 infoPtr->focusedItem = 0;
3687 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
3689 /* refresh the selected item to return the filled background */
3690 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
3693 return 0;
3697 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3698 goto setfocus;
3701 * If the style allow editing and the node is already selected
3702 * and the click occured on the item label...
3704 if ((infoPtr->dwStyle & TVS_EDITLABELS) &&
3705 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem))
3707 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3708 KillTimer(hwnd, TV_EDIT_TIMER);
3710 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
3711 infoPtr->Timer |= TV_EDIT_TIMER_SET;
3713 else if (ht.flags & TVHT_ONITEM) /* select the item if the hit was inside of the icon or text */
3716 * if we are TVS_SINGLEEXPAND then we want this single click to
3717 * do a bunch of things.
3719 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
3720 (infoPtr->hwndEdit == 0))
3722 TREEVIEW_ITEM *SelItem;
3725 * Send the notification
3727 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVIF_HANDLE | TVIF_PARAM,
3728 0, ht.hItem, 0);
3731 * Close the previous selection all the way to the root
3732 * as long as the new selection is not a child
3734 if((infoPtr->selectedItem)
3735 && (infoPtr->selectedItem != ht.hItem))
3737 BOOL closeit = TRUE;
3738 SelItem = ht.hItem;
3740 /* determine of the hitItem is a child of the currently selected item */
3741 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3743 closeit = (SelItem != infoPtr->selectedItem);
3744 SelItem = SelItem->parent;
3747 if(closeit)
3749 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3750 SelItem = infoPtr->selectedItem;
3752 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3754 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3755 SelItem = SelItem->parent;
3761 * Expand the current item
3763 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
3766 /* Select the current item */
3767 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
3769 else if (ht.flags & TVHT_ONITEMSTATEICON)
3771 /* TVS_CHECKBOXES requires us to toggle the current state */
3772 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3773 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
3776 setfocus:
3777 SetFocus(hwnd);
3778 return 0;
3782 static LRESULT
3783 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3785 TVHITTESTINFO ht;
3787 if (infoPtr->hwndEdit)
3789 SetFocus(infoPtr->hwnd);
3790 return 0;
3793 ht.pt.x = (LONG)(INT16)LOWORD(lParam);
3794 ht.pt.y = (LONG)(INT16)HIWORD(lParam);
3796 TREEVIEW_HitTest(infoPtr, &ht);
3798 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3800 if (ht.hItem)
3802 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGA, ht.hItem,
3803 ht.pt);
3804 infoPtr->dropItem = ht.hItem;
3807 else
3809 SetFocus(infoPtr->hwnd);
3810 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
3813 return 0;
3816 static LRESULT
3817 TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
3819 HWND hwnd = infoPtr->hwnd;
3820 POINT pt = *pPt;
3822 /* Change to screen coordinate for WM_CONTEXTMENU */
3823 ClientToScreen(hwnd, &pt);
3825 SendMessageA(hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, MAKELPARAM(pt.x, pt.y));
3826 return 0;
3830 static LRESULT
3831 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3833 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
3834 INT cx, cy;
3835 HDC hdc, htopdc;
3836 HWND hwtop;
3837 HBITMAP hbmp, hOldbmp;
3838 SIZE size;
3839 RECT rc;
3840 HFONT hOldFont;
3842 TRACE("\n");
3844 if (!(infoPtr->himlNormal))
3845 return 0;
3847 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
3848 return 0;
3850 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
3852 hwtop = GetDesktopWindow();
3853 htopdc = GetDC(hwtop);
3854 hdc = CreateCompatibleDC(htopdc);
3856 hOldFont = SelectObject(hdc, infoPtr->hFont);
3857 GetTextExtentPoint32A(hdc, dragItem->pszText, lstrlenA(dragItem->pszText),
3858 &size);
3859 TRACE("%ld %ld %s %d\n", size.cx, size.cy, dragItem->pszText,
3860 lstrlenA(dragItem->pszText));
3861 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
3862 hOldbmp = SelectObject(hdc, hbmp);
3864 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
3865 size.cx += cx;
3866 if (cy > size.cy)
3867 size.cy = cy;
3869 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
3870 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
3871 ILD_NORMAL);
3874 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
3875 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
3878 /* draw item text */
3880 SetRect(&rc, cx, 0, size.cx, size.cy);
3881 DrawTextA(hdc, dragItem->pszText, lstrlenA(dragItem->pszText), &rc,
3882 DT_LEFT);
3883 SelectObject(hdc, hOldFont);
3884 SelectObject(hdc, hOldbmp);
3886 ImageList_Add(infoPtr->dragList, hbmp, 0);
3888 DeleteDC(hdc);
3889 DeleteObject(hbmp);
3890 ReleaseDC(hwtop, htopdc);
3892 return (LRESULT)infoPtr->dragList;
3895 /* Selection ************************************************************/
3897 static LRESULT
3898 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
3899 INT cause)
3901 TREEVIEW_ITEM *prevSelect;
3902 RECT rcFocused;
3904 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
3906 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
3907 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
3908 newSelect ? newSelect->state : 0);
3910 /* reset and redraw focusedItem if focusedItem was set so we don't */
3911 /* have to worry about the previously focused item when we set a new one */
3912 if(infoPtr->focusedItem)
3914 rcFocused = (infoPtr->focusedItem)->rect;
3915 infoPtr->focusedItem = 0;
3916 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
3919 switch (action)
3921 case TVGN_CARET:
3922 prevSelect = infoPtr->selectedItem;
3924 if (prevSelect == newSelect)
3925 return FALSE;
3927 if (TREEVIEW_SendTreeviewNotify(infoPtr,
3928 TVN_SELCHANGINGA,
3929 cause,
3930 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
3931 prevSelect,
3932 newSelect))
3933 return FALSE;
3935 if (prevSelect)
3936 prevSelect->state &= ~TVIS_SELECTED;
3937 if (newSelect)
3938 newSelect->state |= TVIS_SELECTED;
3940 infoPtr->selectedItem = newSelect;
3942 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
3944 TREEVIEW_SendTreeviewNotify(infoPtr,
3945 TVN_SELCHANGEDA,
3946 cause,
3947 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
3948 prevSelect,
3949 newSelect);
3950 TREEVIEW_QueueItemRefresh(infoPtr, prevSelect);
3951 TREEVIEW_QueueItemRefresh(infoPtr, newSelect);
3952 break;
3954 case TVGN_DROPHILITE:
3955 prevSelect = infoPtr->dropItem;
3957 if (prevSelect)
3958 prevSelect->state &= ~TVIS_DROPHILITED;
3960 infoPtr->dropItem = newSelect;
3962 if (newSelect)
3963 newSelect->state |= TVIS_DROPHILITED;
3965 TREEVIEW_QueueItemRefresh(infoPtr, prevSelect);
3966 TREEVIEW_QueueItemRefresh(infoPtr, newSelect);
3967 break;
3969 case TVGN_FIRSTVISIBLE:
3970 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
3971 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
3972 TREEVIEW_QueueRefresh(infoPtr);
3973 break;
3976 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
3977 return TRUE;
3980 /* FIXME: handle NM_KILLFOCUS etc */
3981 static LRESULT
3982 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
3984 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
3985 return FALSE;
3987 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
3989 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
3990 return FALSE;
3992 return TRUE;
3995 /* Scrolling ************************************************************/
3997 static LRESULT
3998 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4000 HTREEITEM newFirstVisible = NULL;
4001 int visible_pos;
4003 if (!TREEVIEW_ValidItem(infoPtr, item))
4004 return FALSE;
4006 if (!ISVISIBLE(item))
4008 /* Expand parents as necessary. */
4009 HTREEITEM parent;
4011 /* see if we are trying to ensure that root is vislble */
4012 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4013 parent = item->parent;
4014 else
4015 parent = item; /* this item is the topmost item */
4017 while (parent != infoPtr->root)
4019 if (!(parent->state & TVIS_EXPANDED))
4020 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4022 parent = parent->parent;
4026 TRACE("%p (%s) %ld - %ld\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4027 infoPtr->firstVisible->visibleOrder);
4029 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4031 if (visible_pos < 0)
4033 /* item is before the start of the list: put it at the top. */
4034 newFirstVisible = item;
4036 else if (visible_pos >= TREEVIEW_GetVisibleCount(infoPtr)
4037 /* Sometimes, before we are displayed, GVC is 0, causing us to
4038 * spuriously scroll up. */
4039 && visible_pos > 0)
4041 /* item is past the end of the list. */
4042 int scroll = visible_pos - TREEVIEW_GetVisibleCount(infoPtr);
4044 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4045 scroll + 1);
4048 if (bHScroll)
4050 /* Scroll window so item's text is visible as much as possible */
4051 /* Calculation of amount of extra space is taken from EditLabel code */
4052 INT pos, x;
4053 TEXTMETRICA textMetric;
4054 HDC hdc = GetWindowDC(infoPtr->hwnd);
4056 x = item->textWidth;
4058 GetTextMetricsA(hdc, &textMetric);
4059 ReleaseDC(infoPtr->hwnd, hdc);
4061 x += (textMetric.tmMaxCharWidth * 2);
4062 x = max(x, textMetric.tmMaxCharWidth * 3);
4064 if (item->textOffset < 0)
4065 pos = item->textOffset;
4066 else if (item->textOffset + x > infoPtr->clientWidth)
4068 if (x > infoPtr->clientWidth)
4069 pos = item->textOffset;
4070 else
4071 pos = item->textOffset + x - infoPtr->clientWidth;
4073 else
4074 pos = 0;
4076 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4079 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4081 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4083 return TRUE;
4086 return FALSE;
4089 static VOID
4090 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4091 TREEVIEW_ITEM *newFirstVisible,
4092 BOOL bUpdateScrollPos)
4094 int gap_size;
4096 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4098 if (newFirstVisible != NULL)
4100 /* Prevent an empty gap from appearing at the bottom... */
4101 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4102 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4104 if (gap_size > 0)
4106 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4107 -gap_size);
4109 /* ... unless we just don't have enough items. */
4110 if (newFirstVisible == NULL)
4111 newFirstVisible = infoPtr->root->firstChild;
4115 if (infoPtr->firstVisible != newFirstVisible)
4117 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4119 infoPtr->firstVisible = newFirstVisible;
4120 TREEVIEW_QueueRefresh(infoPtr);
4122 else
4124 TREEVIEW_ITEM *item;
4125 int scroll = infoPtr->uItemHeight *
4126 (infoPtr->firstVisible->visibleOrder
4127 - newFirstVisible->visibleOrder);
4129 infoPtr->firstVisible = newFirstVisible;
4131 for (item = infoPtr->root->firstChild; item != NULL;
4132 item = TREEVIEW_GetNextListItem(infoPtr, item))
4134 item->rect.top += scroll;
4135 item->rect.bottom += scroll;
4138 if (bUpdateScrollPos)
4139 SetScrollPos(infoPtr->hwnd, SB_VERT,
4140 newFirstVisible->visibleOrder, TRUE);
4142 ScrollWindow(infoPtr->hwnd, 0, scroll, NULL, NULL);
4143 UpdateWindow(infoPtr->hwnd);
4148 /************************************************************************
4149 * VScroll is always in units of visible items. i.e. we always have a
4150 * visible item aligned to the top of the control. (Unless we have no
4151 * items at all.)
4153 static LRESULT
4154 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4156 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4157 TREEVIEW_ITEM *newFirstVisible = NULL;
4159 int nScrollCode = LOWORD(wParam);
4161 TRACE("wp %x\n", wParam);
4163 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4164 return 0;
4166 if (infoPtr->hwndEdit)
4167 SetFocus(infoPtr->hwnd);
4169 if (!oldFirstVisible)
4171 assert(infoPtr->root->firstChild == NULL);
4172 return 0;
4175 switch (nScrollCode)
4177 case SB_TOP:
4178 newFirstVisible = infoPtr->root->firstChild;
4179 break;
4181 case SB_BOTTOM:
4182 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4183 break;
4185 case SB_LINEUP:
4186 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4187 break;
4189 case SB_LINEDOWN:
4190 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4191 break;
4193 case SB_PAGEUP:
4194 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4195 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4196 break;
4198 case SB_PAGEDOWN:
4199 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4200 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4201 break;
4203 case SB_THUMBTRACK:
4204 case SB_THUMBPOSITION:
4205 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4206 infoPtr->root->firstChild,
4207 (LONG)(INT16)HIWORD(wParam));
4208 break;
4210 case SB_ENDSCROLL:
4211 return 0;
4214 if (newFirstVisible != NULL)
4216 if (newFirstVisible != oldFirstVisible)
4217 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4218 nScrollCode != SB_THUMBTRACK);
4219 else if (nScrollCode == SB_THUMBPOSITION)
4220 SetScrollPos(infoPtr->hwnd, SB_VERT,
4221 newFirstVisible->visibleOrder, TRUE);
4224 return 0;
4227 static LRESULT
4228 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4230 int maxWidth;
4231 int scrollX = infoPtr->scrollX;
4232 int nScrollCode = LOWORD(wParam);
4234 TRACE("wp %x\n", wParam);
4236 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4237 return FALSE;
4239 if (infoPtr->hwndEdit)
4240 SetFocus(infoPtr->hwnd);
4242 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4243 /* shall never occure */
4244 if (maxWidth <= 0)
4246 scrollX = 0;
4247 goto scroll;
4250 switch (nScrollCode)
4252 case SB_LINELEFT:
4253 scrollX -= infoPtr->uItemHeight;
4254 break;
4255 case SB_LINERIGHT:
4256 scrollX += infoPtr->uItemHeight;
4257 break;
4258 case SB_PAGELEFT:
4259 scrollX -= infoPtr->clientWidth;
4260 break;
4261 case SB_PAGERIGHT:
4262 scrollX += infoPtr->clientWidth;
4263 break;
4265 case SB_THUMBTRACK:
4266 case SB_THUMBPOSITION:
4267 scrollX = (int)(INT16)HIWORD(wParam);
4268 break;
4270 case SB_ENDSCROLL:
4271 return 0;
4274 if (scrollX > maxWidth)
4275 scrollX = maxWidth;
4276 else if (scrollX < 0)
4277 scrollX = 0;
4279 scroll:
4280 if (scrollX != infoPtr->scrollX)
4282 TREEVIEW_ITEM *item;
4283 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4285 for (item = infoPtr->root->firstChild; item != NULL;
4286 item = TREEVIEW_GetNextListItem(infoPtr, item))
4288 item->linesOffset += scroll_pixels;
4289 item->stateOffset += scroll_pixels;
4290 item->imageOffset += scroll_pixels;
4291 item->textOffset += scroll_pixels;
4294 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4295 infoPtr->scrollX = scrollX;
4296 UpdateWindow(infoPtr->hwnd);
4299 if (nScrollCode != SB_THUMBTRACK)
4300 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4302 return 0;
4305 static LRESULT
4306 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4308 short gcWheelDelta;
4309 UINT pulScrollLines = 3;
4311 if (infoPtr->firstVisible == NULL)
4312 return TRUE;
4314 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4316 gcWheelDelta = -(short)HIWORD(wParam);
4317 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4319 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4321 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4322 int maxDy = infoPtr->maxVisibleOrder;
4324 if (newDy > maxDy)
4325 newDy = maxDy;
4327 if (newDy < 0)
4328 newDy = 0;
4330 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4332 return TRUE;
4335 /* Create/Destroy *******************************************************/
4337 static LRESULT
4338 TREEVIEW_Create(HWND hwnd)
4340 RECT rcClient;
4341 TREEVIEW_INFO *infoPtr;
4343 TRACE("wnd %x, style %lx\n", hwnd, GetWindowLongA(hwnd, GWL_STYLE));
4345 infoPtr = (TREEVIEW_INFO *)COMCTL32_Alloc(sizeof(TREEVIEW_INFO));
4347 if (infoPtr == NULL)
4349 ERR("could not allocate info memory!\n");
4350 return 0;
4353 SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
4355 infoPtr->hwnd = hwnd;
4356 infoPtr->dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
4357 infoPtr->uInternalStatus = 0;
4358 infoPtr->Timer = 0;
4359 infoPtr->uNumItems = 0;
4360 infoPtr->cdmode = 0;
4361 infoPtr->uScrollTime = 300; /* milliseconds */
4362 infoPtr->bRedraw = TRUE;
4364 GetClientRect(hwnd, &rcClient);
4366 /* No scroll bars yet. */
4367 infoPtr->clientWidth = rcClient.right;
4368 infoPtr->clientHeight = rcClient.bottom;
4370 infoPtr->treeWidth = 0;
4371 infoPtr->treeHeight = 0;
4373 infoPtr->uIndent = 19;
4374 infoPtr->selectedItem = 0;
4375 infoPtr->focusedItem = 0;
4376 /* hotItem? */
4377 infoPtr->firstVisible = 0;
4378 infoPtr->maxVisibleOrder = 0;
4379 infoPtr->dropItem = 0;
4380 infoPtr->insertMarkItem = 0;
4381 infoPtr->insertBeforeorAfter = 0;
4382 /* dragList */
4384 infoPtr->scrollX = 0;
4386 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4387 infoPtr->clrText = -1; /* use system color */
4388 infoPtr->clrLine = RGB(128, 128, 128);
4389 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4391 /* hwndToolTip */
4393 infoPtr->hwndEdit = 0;
4394 infoPtr->wpEditOrig = NULL;
4395 infoPtr->bIgnoreEditKillFocus = FALSE;
4396 infoPtr->bLabelChanged = FALSE;
4398 infoPtr->himlNormal = NULL;
4399 infoPtr->himlState = NULL;
4400 infoPtr->normalImageWidth = 0;
4401 infoPtr->normalImageHeight = 0;
4402 infoPtr->stateImageWidth = 0;
4403 infoPtr->stateImageHeight = 0;
4405 infoPtr->items = DPA_Create(16);
4407 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
4408 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4410 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4412 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4413 infoPtr->root->state = TVIS_EXPANDED;
4414 infoPtr->root->iLevel = -1;
4415 infoPtr->root->visibleOrder = -1;
4417 #if 0
4418 infoPtr->hwndNotify = GetParent32 (hwnd);
4419 infoPtr->bTransparent = ( GetWindowLongA( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4420 #endif
4422 infoPtr->hwndToolTip = 0;
4423 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4424 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4426 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4428 RECT rc;
4429 HBITMAP hbm, hbmOld;
4430 HDC hdc;
4431 int nIndex;
4433 infoPtr->himlState =
4434 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4436 hdc = CreateCompatibleDC(0);
4437 hbm = CreateCompatibleBitmap(hdc, 48, 16);
4438 hbmOld = SelectObject(hdc, hbm);
4440 rc.left = 0; rc.top = 0;
4441 rc.right = 48; rc.bottom = 16;
4442 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4444 rc.left = 18; rc.top = 2;
4445 rc.right = 30; rc.bottom = 14;
4446 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4447 DFCS_BUTTONCHECK|DFCS_FLAT);
4449 rc.left = 34; rc.right = 46;
4450 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4451 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4453 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4454 GetSysColor(COLOR_WINDOW));
4455 TRACE("chckbox index %d\n", nIndex);
4456 SelectObject(hdc, hbmOld);
4457 DeleteObject(hbm);
4458 DeleteDC(hdc);
4460 infoPtr->stateImageWidth = 16;
4461 infoPtr->stateImageHeight = 16;
4463 return 0;
4467 static LRESULT
4468 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4470 TRACE("\n");
4472 TREEVIEW_RemoveTree(infoPtr);
4474 /* tool tip is automatically destroyed: we are its owner */
4476 /* Restore original windproc. */
4477 if (infoPtr->hwndEdit)
4478 SetWindowLongA(infoPtr->hwndEdit, GWL_WNDPROC,
4479 (LONG)infoPtr->wpEditOrig);
4481 /* Deassociate treeview from the window before doing anything drastic. */
4482 SetWindowLongA(infoPtr->hwnd, 0, (LONG)NULL);
4484 DeleteObject(infoPtr->hBoldFont);
4485 COMCTL32_Free(infoPtr);
4487 return 0;
4490 /* Miscellaneous Messages ***********************************************/
4492 static LRESULT
4493 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
4495 static const struct
4497 unsigned char code;
4499 scroll[] =
4501 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4502 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
4503 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
4504 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
4505 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
4506 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
4507 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
4508 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
4509 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
4510 #undef SCROLL_ENTRY
4513 if (key >= VK_PRIOR && key <= VK_DOWN)
4515 unsigned char code = scroll[key - VK_PRIOR].code;
4517 (((code & (1 << 7)) == (SB_HORZ << 7))
4518 ? TREEVIEW_HScroll
4519 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
4522 return 0;
4525 /************************************************************************
4526 * TREEVIEW_KeyDown
4528 * VK_UP Move selection to the previous non-hidden item.
4529 * VK_DOWN Move selection to the next non-hidden item.
4530 * VK_HOME Move selection to the first item.
4531 * VK_END Move selection to the last item.
4532 * VK_LEFT If expanded then collapse, otherwise move to parent.
4533 * VK_RIGHT If collapsed then expand, otherwise move to first child.
4534 * VK_ADD Expand.
4535 * VK_SUBTRACT Collapse.
4536 * VK_MULTIPLY Expand all.
4537 * VK_PRIOR Move up GetVisibleCount items.
4538 * VK_NEXT Move down GetVisibleCount items.
4539 * VK_BACK Move to parent.
4540 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
4542 static LRESULT
4543 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4545 /* If it is non-NULL and different, it will be selected and visible. */
4546 TREEVIEW_ITEM *newSelection = NULL;
4548 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
4550 TRACE("%x\n", wParam);
4552 if (prevItem == NULL)
4553 return FALSE;
4555 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
4556 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
4558 switch (wParam)
4560 case VK_UP:
4561 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
4562 if (!newSelection)
4563 newSelection = infoPtr->root->firstChild;
4564 break;
4566 case VK_DOWN:
4567 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
4568 break;
4570 case VK_HOME:
4571 newSelection = infoPtr->root->firstChild;
4572 break;
4574 case VK_END:
4575 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4576 break;
4578 case VK_LEFT:
4579 if (prevItem->state & TVIS_EXPANDED)
4581 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4583 else if (prevItem->parent != infoPtr->root)
4585 newSelection = prevItem->parent;
4587 break;
4589 case VK_RIGHT:
4590 if (TREEVIEW_HasChildren(infoPtr, prevItem))
4592 if (!(prevItem->state & TVIS_EXPANDED))
4593 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4594 else
4596 newSelection = prevItem->firstChild;
4600 break;
4602 case VK_MULTIPLY:
4603 TREEVIEW_ExpandAll(infoPtr, prevItem);
4604 break;
4606 case VK_ADD:
4607 if (!(prevItem->state & TVIS_EXPANDED))
4608 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4609 break;
4611 case VK_SUBTRACT:
4612 if (prevItem->state & TVIS_EXPANDED)
4613 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4614 break;
4616 case VK_PRIOR:
4617 newSelection
4618 = TREEVIEW_GetListItem(infoPtr, prevItem,
4619 -TREEVIEW_GetVisibleCount(infoPtr));
4620 break;
4622 case VK_NEXT:
4623 newSelection
4624 = TREEVIEW_GetListItem(infoPtr, prevItem,
4625 TREEVIEW_GetVisibleCount(infoPtr));
4626 break;
4628 case VK_BACK:
4629 newSelection = prevItem->parent;
4630 if (newSelection == infoPtr->root)
4631 newSelection = NULL;
4632 break;
4634 case VK_SPACE:
4635 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4636 TREEVIEW_ToggleItemState(infoPtr, prevItem);
4637 break;
4640 if (newSelection && newSelection != prevItem)
4642 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
4643 TVC_BYKEYBOARD))
4645 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
4649 return FALSE;
4652 static LRESULT
4653 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4655 if (wParam == SIZE_RESTORED)
4657 infoPtr->clientWidth = (LONG)(INT16)LOWORD(lParam);
4658 infoPtr->clientHeight = (LONG)(INT16)HIWORD(lParam);
4660 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
4661 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
4662 TREEVIEW_UpdateScrollBars(infoPtr);
4664 else
4666 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
4669 TREEVIEW_QueueRefresh(infoPtr);
4670 return 0;
4673 static LRESULT
4674 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4676 TRACE("(%x %lx)\n", wParam, lParam);
4678 if (wParam == GWL_STYLE)
4680 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
4682 /* we have to take special care about tooltips */
4683 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
4685 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
4687 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
4688 TRACE("\n");
4690 else
4692 DestroyWindow(infoPtr->hwndToolTip);
4693 infoPtr->hwndToolTip = 0;
4697 infoPtr->dwStyle = dwNewStyle;
4700 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
4701 TREEVIEW_UpdateScrollBars(infoPtr);
4702 TREEVIEW_QueueRefresh(infoPtr);
4704 return 0;
4707 static LRESULT
4708 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
4710 TRACE("\n");
4712 if (!infoPtr->selectedItem)
4714 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
4715 TVC_UNKNOWN);
4718 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
4719 TREEVIEW_QueueItemRefresh(infoPtr, infoPtr->selectedItem);
4720 return 0;
4723 static LRESULT
4724 TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
4726 TRACE("\n");
4728 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
4729 TREEVIEW_QueueItemRefresh(infoPtr, infoPtr->selectedItem);
4730 return 0;
4734 static LRESULT WINAPI
4735 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
4737 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
4738 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
4739 else
4741 if (uMsg == WM_CREATE)
4742 TREEVIEW_Create(hwnd);
4743 else
4744 goto def;
4747 switch (uMsg)
4749 case TVM_CREATEDRAGIMAGE:
4750 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
4752 case TVM_DELETEITEM:
4753 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
4755 case TVM_EDITLABELA:
4756 return TREEVIEW_EditLabelA(infoPtr, (HTREEITEM)lParam);
4758 case TVM_EDITLABELW:
4759 FIXME("Unimplemented msg TVM_EDITLABELW\n");
4760 return 0;
4762 case TVM_ENDEDITLABELNOW:
4763 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
4765 case TVM_ENSUREVISIBLE:
4766 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
4768 case TVM_EXPAND:
4769 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
4771 case TVM_GETBKCOLOR:
4772 return TREEVIEW_GetBkColor(infoPtr);
4774 case TVM_GETCOUNT:
4775 return TREEVIEW_GetCount(infoPtr);
4777 case TVM_GETEDITCONTROL:
4778 return TREEVIEW_GetEditControl(infoPtr);
4780 case TVM_GETIMAGELIST:
4781 return TREEVIEW_GetImageList(infoPtr, wParam);
4783 case TVM_GETINDENT:
4784 return TREEVIEW_GetIndent(infoPtr);
4786 case TVM_GETINSERTMARKCOLOR:
4787 return TREEVIEW_GetInsertMarkColor(infoPtr);
4789 case TVM_GETISEARCHSTRINGA:
4790 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
4791 return 0;
4793 case TVM_GETISEARCHSTRINGW:
4794 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
4795 return 0;
4797 case TVM_GETITEMA:
4798 return TREEVIEW_GetItemA(infoPtr, (LPTVITEMEXA)lParam);
4800 case TVM_GETITEMW:
4801 return TREEVIEW_GetItemW(infoPtr, (LPTVITEMEXA)lParam);
4803 case TVM_GETITEMHEIGHT:
4804 return TREEVIEW_GetItemHeight(infoPtr);
4806 case TVM_GETITEMRECT:
4807 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
4809 case TVM_GETITEMSTATE:
4810 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
4812 case TVM_GETLINECOLOR:
4813 return TREEVIEW_GetLineColor(infoPtr);
4815 case TVM_GETNEXTITEM:
4816 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
4818 case TVM_GETSCROLLTIME:
4819 return TREEVIEW_GetScrollTime(infoPtr);
4821 case TVM_GETTEXTCOLOR:
4822 return TREEVIEW_GetTextColor(infoPtr);
4824 case TVM_GETTOOLTIPS:
4825 return TREEVIEW_GetToolTips(infoPtr);
4827 case TVM_GETUNICODEFORMAT:
4828 FIXME("Unimplemented msg TVM_GETUNICODEFORMAT\n");
4829 return 0;
4831 case TVM_GETVISIBLECOUNT:
4832 return TREEVIEW_GetVisibleCount(infoPtr);
4834 case TVM_HITTEST:
4835 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
4837 case TVM_INSERTITEMA:
4838 return TREEVIEW_InsertItemA(infoPtr, lParam);
4840 case TVM_INSERTITEMW:
4841 return TREEVIEW_InsertItemW(infoPtr, lParam);
4843 case TVM_SELECTITEM:
4844 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
4846 case TVM_SETBKCOLOR:
4847 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
4849 case TVM_SETIMAGELIST:
4850 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
4852 case TVM_SETINDENT:
4853 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
4855 case TVM_SETINSERTMARK:
4856 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
4858 case TVM_SETINSERTMARKCOLOR:
4859 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
4861 case TVM_SETITEMA:
4862 return TREEVIEW_SetItemA(infoPtr, (LPTVITEMEXA)lParam);
4864 case TVM_SETITEMW:
4865 FIXME("Unimplemented msg TVM_SETITEMW\n");
4866 return 0;
4868 case TVM_SETLINECOLOR:
4869 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
4871 case TVM_SETITEMHEIGHT:
4872 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
4874 case TVM_SETSCROLLTIME:
4875 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
4877 case TVM_SETTEXTCOLOR:
4878 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
4880 case TVM_SETTOOLTIPS:
4881 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
4883 case TVM_SETUNICODEFORMAT:
4884 FIXME("Unimplemented msg TVM_SETUNICODEFORMAT\n");
4885 return 0;
4887 case TVM_SORTCHILDREN:
4888 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
4890 case TVM_SORTCHILDRENCB:
4891 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
4893 /* WM_CHAR */
4895 case WM_COMMAND:
4896 return TREEVIEW_Command(infoPtr, wParam, lParam);
4898 case WM_DESTROY:
4899 return TREEVIEW_Destroy(infoPtr);
4901 /* WM_ENABLE */
4903 case WM_ERASEBKGND:
4904 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
4906 case WM_GETDLGCODE:
4907 return DLGC_WANTARROWS | DLGC_WANTCHARS;
4909 case WM_GETFONT:
4910 return TREEVIEW_GetFont(infoPtr);
4912 case WM_HSCROLL:
4913 return TREEVIEW_HScroll(infoPtr, wParam);
4915 case WM_KEYDOWN:
4916 return TREEVIEW_KeyDown(infoPtr, wParam);
4918 case WM_KILLFOCUS:
4919 return TREEVIEW_KillFocus(infoPtr);
4921 case WM_LBUTTONDBLCLK:
4922 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
4924 case WM_LBUTTONDOWN:
4925 return TREEVIEW_LButtonDown(infoPtr, lParam);
4927 /* WM_MBUTTONDOWN */
4929 /* WM_MOUSEMOVE */
4931 /* WM_NOTIFY */
4933 /* WM_NOTIFYFORMAT */
4935 case WM_PAINT:
4936 return TREEVIEW_Paint(infoPtr, wParam);
4938 /* WM_PRINTCLIENT */
4940 case WM_RBUTTONDOWN:
4941 return TREEVIEW_RButtonDown(infoPtr, lParam);
4943 case WM_SETFOCUS:
4944 return TREEVIEW_SetFocus(infoPtr);
4946 case WM_SETFONT:
4947 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
4949 case WM_SETREDRAW:
4950 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
4952 case WM_SIZE:
4953 return TREEVIEW_Size(infoPtr, wParam, lParam);
4955 case WM_STYLECHANGED:
4956 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
4958 /* WM_SYSCOLORCHANGE */
4960 /* WM_SYSKEYDOWN */
4962 case WM_TIMER:
4963 return TREEVIEW_HandleTimer(infoPtr, wParam);
4965 case WM_VSCROLL:
4966 return TREEVIEW_VScroll(infoPtr, wParam);
4968 /* WM_WININICHANGE */
4970 case WM_MOUSEWHEEL:
4971 if (wParam & (MK_SHIFT | MK_CONTROL))
4972 goto def;
4973 return TREEVIEW_MouseWheel(infoPtr, wParam);
4975 case WM_DRAWITEM:
4976 TRACE("drawItem\n");
4977 goto def;
4979 default:
4980 /* This mostly catches MFC and Delphi messages. :( */
4981 if (uMsg >= WM_USER)
4982 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
4983 def:
4984 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
4986 return 0;
4990 /* Class Registration ***************************************************/
4992 VOID
4993 TREEVIEW_Register(void)
4995 WNDCLASSA wndClass;
4997 TRACE("\n");
4999 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
5000 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5001 wndClass.lpfnWndProc = (WNDPROC)TREEVIEW_WindowProc;
5002 wndClass.cbClsExtra = 0;
5003 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5005 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
5006 wndClass.hbrBackground = 0;
5007 wndClass.lpszClassName = WC_TREEVIEWA;
5009 RegisterClassA(&wndClass);
5013 VOID
5014 TREEVIEW_Unregister(void)
5016 UnregisterClassA(WC_TREEVIEWA, (HINSTANCE) NULL);
5020 /* Tree Verification ****************************************************/
5022 #ifndef NDEBUG
5023 static inline void
5024 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5026 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5027 TREEVIEW_ITEM *item)
5029 assert(infoPtr != NULL);
5030 assert(item != NULL);
5032 /* both NULL, or both non-null */
5033 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5035 assert(item->firstChild != item);
5036 assert(item->lastChild != item);
5038 if (item->firstChild)
5040 assert(item->firstChild->parent == item);
5041 assert(item->firstChild->prevSibling == NULL);
5044 if (item->lastChild)
5046 assert(item->lastChild->parent == item);
5047 assert(item->lastChild->nextSibling == NULL);
5050 assert(item->nextSibling != item);
5051 if (item->nextSibling)
5053 assert(item->nextSibling->parent == item->parent);
5054 assert(item->nextSibling->prevSibling == item);
5057 assert(item->prevSibling != item);
5058 if (item->prevSibling)
5060 assert(item->prevSibling->parent == item->parent);
5061 assert(item->prevSibling->nextSibling == item);
5065 static inline void
5066 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5068 assert(item != NULL);
5070 assert(item->parent != NULL);
5071 assert(item->parent != item);
5072 assert(item->iLevel == item->parent->iLevel + 1);
5074 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5076 TREEVIEW_VerifyItemCommon(infoPtr, item);
5078 TREEVIEW_VerifyChildren(infoPtr, item);
5081 static inline void
5082 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5084 TREEVIEW_ITEM *child;
5085 assert(item != NULL);
5087 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5088 TREEVIEW_VerifyItem(infoPtr, child);
5091 static inline void
5092 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5094 TREEVIEW_ITEM *root = infoPtr->root;
5096 assert(root != NULL);
5097 assert(root->iLevel == -1);
5098 assert(root->parent == NULL);
5099 assert(root->prevSibling == NULL);
5101 TREEVIEW_VerifyItemCommon(infoPtr, root);
5103 TREEVIEW_VerifyChildren(infoPtr, root);
5106 static void
5107 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5109 assert(infoPtr != NULL);
5111 TREEVIEW_VerifyRoot(infoPtr);
5113 #endif