comctl32: Remove a useless assert from TREEVIEW_GetItemIndex.
[wine/wine64.git] / dlls / comctl32 / treeview.c
blob89d7218242b4652df59a95b6c07c7f223c83299a
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
6 * Copyright 2002 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTES
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
26 * Note2: All items always! have valid (allocated) pszText field.
27 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
28 * of size TEXT_CALLBACK_SIZE in DoSetItem.
29 * We use callbackMask to keep track of fields to be updated.
31 * TODO:
32 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
33 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
35 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
37 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
43 #include "config.h"
44 #include "wine/port.h"
46 #include <assert.h>
47 #include <ctype.h>
48 #include <stdarg.h>
49 #include <string.h>
50 #include <limits.h>
51 #include <stdlib.h>
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
55 #include "windef.h"
56 #include "winbase.h"
57 #include "wingdi.h"
58 #include "winuser.h"
59 #include "winnls.h"
60 #include "commctrl.h"
61 #include "comctl32.h"
62 #include "uxtheme.h"
63 #include "tmschema.h"
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 /* internal structures */
69 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
71 UINT callbackMask;
72 UINT state;
73 UINT stateMask;
74 LPWSTR pszText;
75 int cchTextMax;
76 int iImage;
77 int iSelectedImage;
78 int cChildren;
79 LPARAM lParam;
80 int iIntegral; /* item height multiplier (1 is normal) */
81 int iLevel; /* indentation level:0=root level */
82 HTREEITEM parent; /* handle to parent or 0 if at root */
83 HTREEITEM firstChild; /* handle to first child or 0 if no child */
84 HTREEITEM lastChild;
85 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
86 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
87 RECT rect;
88 LONG linesOffset;
89 LONG stateOffset;
90 LONG imageOffset;
91 LONG textOffset;
92 LONG textWidth; /* horizontal text extent for pszText */
93 LONG visibleOrder; /* visible ordering, 0 is first visible item */
94 } TREEVIEW_ITEM;
97 typedef struct tagTREEVIEW_INFO
99 HWND hwnd;
100 HWND hwndNotify; /* Owner window to send notifications to */
101 DWORD dwStyle;
102 HTREEITEM root;
103 UINT uInternalStatus;
104 INT Timer;
105 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
106 INT cdmode; /* last custom draw setting */
107 UINT uScrollTime; /* max. time for scrolling in milliseconds */
108 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
110 UINT uItemHeight; /* item height */
111 BOOL bHeightSet;
113 LONG clientWidth; /* width of control window */
114 LONG clientHeight; /* height of control window */
116 LONG treeWidth; /* width of visible tree items */
117 LONG treeHeight; /* height of visible tree items */
119 UINT uIndent; /* indentation in pixels */
120 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
121 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
122 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
124 HTREEITEM firstVisible; /* handle to first visible item */
125 LONG maxVisibleOrder;
126 HTREEITEM dropItem; /* handle to item selected by drag cursor */
127 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
128 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
129 HIMAGELIST dragList; /* Bitmap of dragged item */
130 LONG scrollX;
131 COLORREF clrBk;
132 COLORREF clrText;
133 COLORREF clrLine;
134 COLORREF clrInsertMark;
135 HFONT hFont;
136 HFONT hDefaultFont;
137 HFONT hBoldFont;
138 HFONT hUnderlineFont;
139 HCURSOR hcurHand;
140 HWND hwndToolTip;
142 HWND hwndEdit;
143 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
144 BOOL bIgnoreEditKillFocus;
145 BOOL bLabelChanged;
147 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
148 HIMAGELIST himlNormal;
149 int normalImageHeight;
150 int normalImageWidth;
151 HIMAGELIST himlState;
152 int stateImageHeight;
153 int stateImageWidth;
154 HDPA items;
156 DWORD lastKeyPressTimestamp; /* Added */
157 WPARAM charCode; /* Added */
158 INT nSearchParamLength; /* Added */
159 WCHAR szSearchParam[ MAX_PATH ]; /* Added */
160 } TREEVIEW_INFO;
163 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
164 #define KEY_DELAY 450
166 /* bitflags for infoPtr->uInternalStatus */
168 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
169 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
170 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
171 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
172 #define TV_RDRAG 0x10 /* ditto Rbutton */
173 #define TV_RDRAGGING 0x20
175 /* bitflags for infoPtr->timer */
177 #define TV_EDIT_TIMER 2
178 #define TV_EDIT_TIMER_SET 2
181 VOID TREEVIEW_Register (VOID);
182 VOID TREEVIEW_Unregister (VOID);
185 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
188 #define TEXT_CALLBACK_SIZE 260
190 #define TREEVIEW_LEFT_MARGIN 8
192 #define MINIMUM_INDENT 19
194 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
196 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
197 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
198 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
201 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
204 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
207 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
209 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
210 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
211 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
212 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
213 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
214 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
215 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
216 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND wParam, UINT lParam);
219 /* Random Utilities *****************************************************/
221 #ifndef NDEBUG
222 static inline void
223 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
225 (void)infoPtr;
227 #else
228 /* The definition is at the end of the file. */
229 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
230 #endif
232 /* Returns the treeview private data if hwnd is a treeview.
233 * Otherwise returns an undefined value. */
234 static TREEVIEW_INFO *
235 TREEVIEW_GetInfoPtr(HWND hwnd)
237 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
240 /* Don't call this. Nothing wants an item index. */
241 static inline int
242 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
244 return DPA_GetPtrIndex(infoPtr->items, handle);
247 /* Checks if item has changed and needs to be redrawn */
248 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
249 const TVITEMEXW *tvChange)
251 /* Number of children has changed */
252 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
253 return TRUE;
255 /* Image has changed and it's not a callback */
256 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
257 tiNew->iImage != I_IMAGECALLBACK)
258 return TRUE;
260 /* Selected image has changed and it's not a callback */
261 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
262 tiNew->iSelectedImage != I_IMAGECALLBACK)
263 return TRUE;
265 /* Text has changed and it's not a callback */
266 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
267 tiNew->pszText != LPSTR_TEXTCALLBACKW)
268 return TRUE;
270 /* Indent has changed */
271 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
272 return TRUE;
274 /* Item state has changed */
275 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
276 return TRUE;
278 return FALSE;
281 /***************************************************************************
282 * This method checks that handle is an item for this tree.
284 static BOOL
285 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
287 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
289 TRACE("invalid item %p\n", handle);
290 return FALSE;
292 else
293 return TRUE;
296 static HFONT
297 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
299 LOGFONTW font;
301 GetObjectW(hOrigFont, sizeof(font), &font);
302 font.lfWeight = FW_BOLD;
303 return CreateFontIndirectW(&font);
306 static HFONT
307 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
309 LOGFONTW font;
311 GetObjectW(hOrigFont, sizeof(font), &font);
312 font.lfUnderline = TRUE;
313 return CreateFontIndirectW(&font);
316 static inline HFONT
317 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
319 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
320 return infoPtr->hUnderlineFont;
321 if (item->state & TVIS_BOLD)
322 return infoPtr->hBoldFont;
323 return infoPtr->hFont;
326 /* for trace/debugging purposes only */
327 static const char *
328 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
330 if (item == NULL) return "<null item>";
331 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
332 if (item->pszText == NULL) return "<null>";
333 return debugstr_w(item->pszText);
336 /* An item is not a child of itself. */
337 static BOOL
338 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
342 child = child->parent;
343 if (child == parent) return TRUE;
344 } while (child != NULL);
346 return FALSE;
350 /* Tree Traversal *******************************************************/
352 /***************************************************************************
353 * This method returns the last expanded sibling or child child item
354 * of a tree node
356 static TREEVIEW_ITEM *
357 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
359 if (!wineItem)
360 return NULL;
362 while (wineItem->lastChild)
364 if (wineItem->state & TVIS_EXPANDED)
365 wineItem = wineItem->lastChild;
366 else
367 break;
370 if (wineItem == infoPtr->root)
371 return NULL;
373 return wineItem;
376 /***************************************************************************
377 * This method returns the previous non-hidden item in the list not
378 * considering the tree hierarchy.
380 static TREEVIEW_ITEM *
381 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
383 if (tvItem->prevSibling)
385 /* This item has a prevSibling, get the last item in the sibling's tree. */
386 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
388 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
389 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
390 else
391 return upItem;
393 else
395 /* this item does not have a prevSibling, get the parent */
396 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
401 /***************************************************************************
402 * This method returns the next physical item in the treeview not
403 * considering the tree hierarchy.
405 static TREEVIEW_ITEM *
406 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
408 assert(tvItem != NULL);
411 * If this item has children and is expanded, return the first child
413 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
415 return tvItem->firstChild;
420 * try to get the sibling
422 if (tvItem->nextSibling)
423 return tvItem->nextSibling;
426 * Otherwise, get the parent's sibling.
428 while (tvItem->parent)
430 tvItem = tvItem->parent;
432 if (tvItem->nextSibling)
433 return tvItem->nextSibling;
436 return NULL;
439 /***************************************************************************
440 * This method returns the nth item starting at the given item. It returns
441 * the last item (or first) we we run out of items.
443 * Will scroll backward if count is <0.
444 * forward if count is >0.
446 static TREEVIEW_ITEM *
447 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
448 LONG count)
450 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
451 TREEVIEW_ITEM *previousItem;
453 assert(wineItem != NULL);
455 if (count > 0)
457 next_item = TREEVIEW_GetNextListItem;
459 else if (count < 0)
461 count = -count;
462 next_item = TREEVIEW_GetPrevListItem;
464 else
465 return wineItem;
469 previousItem = wineItem;
470 wineItem = next_item(infoPtr, wineItem);
472 } while (--count && wineItem != NULL);
475 return wineItem ? wineItem : previousItem;
478 /* Notifications ************************************************************/
480 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
482 if (!infoPtr->bNtfUnicode) {
483 switch (code) {
484 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
485 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
486 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
487 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
488 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
489 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
490 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
491 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
492 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
493 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
494 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
495 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
498 return code;
501 static LRESULT
502 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
504 TRACE("wParam=%ld, lParam=%ld\n", wParam, lParam);
505 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
508 static BOOL
509 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
511 NMHDR nmhdr;
512 HWND hwnd = infoPtr->hwnd;
514 TRACE("%d\n", code);
515 nmhdr.hwndFrom = hwnd;
516 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
517 nmhdr.code = get_notifycode(infoPtr, code);
519 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, (LPARAM)&nmhdr);
522 static VOID
523 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
525 tvItem->mask = mask;
526 tvItem->hItem = item;
527 tvItem->state = item->state;
528 tvItem->stateMask = 0;
529 tvItem->iImage = item->iImage;
530 tvItem->iSelectedImage = item->iSelectedImage;
531 tvItem->cChildren = item->cChildren;
532 tvItem->lParam = item->lParam;
534 if(mask & TVIF_TEXT)
536 if (!infoPtr->bNtfUnicode)
538 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
539 tvItem->pszText = Alloc (tvItem->cchTextMax);
540 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
542 else
544 tvItem->cchTextMax = item->cchTextMax;
545 tvItem->pszText = item->pszText;
548 else
550 tvItem->cchTextMax = 0;
551 tvItem->pszText = NULL;
555 static BOOL
556 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
557 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
559 HWND hwnd = infoPtr->hwnd;
560 NMTREEVIEWW nmhdr;
561 BOOL ret;
563 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
564 code, action, oldItem, newItem);
566 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
568 nmhdr.hdr.hwndFrom = hwnd;
569 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
570 nmhdr.hdr.code = get_notifycode(infoPtr, code);
571 nmhdr.action = action;
573 if (oldItem)
574 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
576 if (newItem)
577 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
579 nmhdr.ptDrag.x = 0;
580 nmhdr.ptDrag.y = 0;
582 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
583 if (!infoPtr->bNtfUnicode)
585 Free(nmhdr.itemOld.pszText);
586 Free(nmhdr.itemNew.pszText);
588 return ret;
591 static BOOL
592 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
593 HTREEITEM dragItem, POINT pt)
595 HWND hwnd = infoPtr->hwnd;
596 NMTREEVIEWW nmhdr;
598 TRACE("code:%d dragitem:%p\n", code, dragItem);
600 nmhdr.hdr.hwndFrom = hwnd;
601 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
602 nmhdr.hdr.code = get_notifycode(infoPtr, code);
603 nmhdr.action = 0;
604 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
605 nmhdr.itemNew.hItem = dragItem;
606 nmhdr.itemNew.state = dragItem->state;
607 nmhdr.itemNew.lParam = dragItem->lParam;
609 nmhdr.ptDrag.x = pt.x;
610 nmhdr.ptDrag.y = pt.y;
612 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
616 static BOOL
617 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
618 HDC hdc, RECT rc)
620 HWND hwnd = infoPtr->hwnd;
621 NMTVCUSTOMDRAW nmcdhdr;
622 LPNMCUSTOMDRAW nmcd;
624 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
626 nmcd = &nmcdhdr.nmcd;
627 nmcd->hdr.hwndFrom = hwnd;
628 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
629 nmcd->hdr.code = NM_CUSTOMDRAW;
630 nmcd->dwDrawStage = dwDrawStage;
631 nmcd->hdc = hdc;
632 nmcd->rc = rc;
633 nmcd->dwItemSpec = 0;
634 nmcd->uItemState = 0;
635 nmcd->lItemlParam = 0;
636 nmcdhdr.clrText = infoPtr->clrText;
637 nmcdhdr.clrTextBk = infoPtr->clrBk;
638 nmcdhdr.iLevel = 0;
640 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)&nmcdhdr);
645 /* FIXME: need to find out when the flags in uItemState need to be set */
647 static BOOL
648 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
649 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
650 NMTVCUSTOMDRAW *nmcdhdr)
652 HWND hwnd = infoPtr->hwnd;
653 LPNMCUSTOMDRAW nmcd;
654 DWORD dwDrawStage;
655 DWORD_PTR dwItemSpec;
656 UINT uItemState;
657 INT retval;
659 dwDrawStage = CDDS_ITEM | uItemDrawState;
660 dwItemSpec = (DWORD_PTR)wineItem;
661 uItemState = 0;
662 if (wineItem->state & TVIS_SELECTED)
663 uItemState |= CDIS_SELECTED;
664 if (wineItem == infoPtr->selectedItem)
665 uItemState |= CDIS_FOCUS;
666 if (wineItem == infoPtr->hotItem)
667 uItemState |= CDIS_HOT;
669 nmcd = &nmcdhdr->nmcd;
670 nmcd->hdr.hwndFrom = hwnd;
671 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
672 nmcd->hdr.code = NM_CUSTOMDRAW;
673 nmcd->dwDrawStage = dwDrawStage;
674 nmcd->hdc = hdc;
675 nmcd->rc = wineItem->rect;
676 nmcd->dwItemSpec = dwItemSpec;
677 nmcd->uItemState = uItemState;
678 nmcd->lItemlParam = wineItem->lParam;
679 nmcdhdr->iLevel = wineItem->iLevel;
681 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
682 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
683 nmcd->uItemState, nmcd->lItemlParam);
685 retval = TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)nmcdhdr);
687 return retval;
690 static BOOL
691 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
693 HWND hwnd = infoPtr->hwnd;
694 NMTVDISPINFOW tvdi;
695 BOOL ret;
697 tvdi.hdr.hwndFrom = hwnd;
698 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
699 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
701 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
702 &tvdi.item, editItem);
704 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
706 if (!infoPtr->bNtfUnicode)
707 Free(tvdi.item.pszText);
709 return ret;
712 static void
713 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
714 UINT mask)
716 NMTVDISPINFOW callback;
717 HWND hwnd = infoPtr->hwnd;
719 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
720 mask &= wineItem->callbackMask;
722 if (mask == 0) return;
724 callback.hdr.hwndFrom = hwnd;
725 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
726 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
728 /* 'state' always contains valid value, as well as 'lParam'.
729 * All other parameters are uninitialized.
731 callback.item.pszText = wineItem->pszText;
732 callback.item.cchTextMax = wineItem->cchTextMax;
733 callback.item.mask = mask;
734 callback.item.hItem = wineItem;
735 callback.item.state = wineItem->state;
736 callback.item.lParam = wineItem->lParam;
738 /* If text is changed we need to recalculate textWidth */
739 if (mask & TVIF_TEXT)
740 wineItem->textWidth = 0;
742 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, (LPARAM)&callback);
744 /* It may have changed due to a call to SetItem. */
745 mask &= wineItem->callbackMask;
747 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
749 /* Instead of copying text into our buffer user specified its own */
750 if (!infoPtr->bNtfUnicode) {
751 LPWSTR newText;
752 int buflen;
753 int len = MultiByteToWideChar( CP_ACP, 0,
754 (LPSTR)callback.item.pszText, -1,
755 NULL, 0);
756 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
757 newText = (LPWSTR)ReAlloc(wineItem->pszText, buflen);
759 TRACE("returned str %s, len=%d, buflen=%d\n",
760 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
762 if (newText)
764 wineItem->pszText = newText;
765 MultiByteToWideChar( CP_ACP, 0,
766 (LPSTR)callback.item.pszText, -1,
767 wineItem->pszText, buflen/sizeof(WCHAR));
768 wineItem->cchTextMax = buflen/sizeof(WCHAR);
770 /* If ReAlloc fails we have nothing to do, but keep original text */
772 else {
773 int len = max(lstrlenW(callback.item.pszText) + 1,
774 TEXT_CALLBACK_SIZE);
775 LPWSTR newText = ReAlloc(wineItem->pszText, len);
777 TRACE("returned wstr %s, len=%d\n",
778 debugstr_w(callback.item.pszText), len);
780 if (newText)
782 wineItem->pszText = newText;
783 strcpyW(wineItem->pszText, callback.item.pszText);
784 wineItem->cchTextMax = len;
786 /* If ReAlloc fails we have nothing to do, but keep original text */
789 else if (mask & TVIF_TEXT) {
790 /* User put text into our buffer, that is ok unless A string */
791 if (!infoPtr->bNtfUnicode) {
792 LPWSTR newText;
793 LPWSTR oldText = NULL;
794 int buflen;
795 int len = MultiByteToWideChar( CP_ACP, 0,
796 (LPSTR)callback.item.pszText, -1,
797 NULL, 0);
798 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
799 newText = (LPWSTR)Alloc(buflen);
801 TRACE("same buffer str %s, len=%d, buflen=%d\n",
802 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
804 if (newText)
806 oldText = wineItem->pszText;
807 wineItem->pszText = newText;
808 MultiByteToWideChar( CP_ACP, 0,
809 (LPSTR)callback.item.pszText, -1,
810 wineItem->pszText, buflen/sizeof(WCHAR));
811 wineItem->cchTextMax = buflen/sizeof(WCHAR);
812 Free(oldText);
817 if (mask & TVIF_IMAGE)
818 wineItem->iImage = callback.item.iImage;
820 if (mask & TVIF_SELECTEDIMAGE)
821 wineItem->iSelectedImage = callback.item.iSelectedImage;
823 if (mask & TVIF_CHILDREN)
824 wineItem->cChildren = callback.item.cChildren;
826 /* These members are now permanently set. */
827 if (callback.item.mask & TVIF_DI_SETITEM)
828 wineItem->callbackMask &= ~callback.item.mask;
831 /***************************************************************************
832 * This function uses cChildren field to decide whether the item has
833 * children or not.
834 * Note: if this returns TRUE, the child items may not actually exist,
835 * they could be virtual.
837 * Just use wineItem->firstChild to check for physical children.
839 static BOOL
840 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
842 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
844 return wineItem->cChildren > 0;
848 /* Item Position ********************************************************/
850 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
851 static VOID
852 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
854 /* Same effect, different optimisation. */
855 #if 0
856 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
857 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
858 #else
859 BOOL lar = ((infoPtr->dwStyle
860 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
861 > TVS_LINESATROOT);
862 #endif
864 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
865 - infoPtr->scrollX;
866 item->stateOffset = item->linesOffset + infoPtr->uIndent;
867 item->imageOffset = item->stateOffset
868 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
869 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
872 static VOID
873 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
875 HDC hdc;
876 HFONT hOldFont=0;
877 SIZE sz;
879 /* DRAW's OM docker creates items like this */
880 if (item->pszText == NULL)
882 item->textWidth = 0;
883 return;
886 if (hDC != 0)
888 hdc = hDC;
890 else
892 hdc = GetDC(infoPtr->hwnd);
893 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
896 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
897 item->textWidth = sz.cx;
899 if (hDC == 0)
901 SelectObject(hdc, hOldFont);
902 ReleaseDC(0, hdc);
906 static VOID
907 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
909 item->rect.top = infoPtr->uItemHeight *
910 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
912 item->rect.bottom = item->rect.top
913 + infoPtr->uItemHeight * item->iIntegral - 1;
915 item->rect.left = 0;
916 item->rect.right = infoPtr->clientWidth;
919 /* We know that only items after start need their order updated. */
920 static void
921 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
923 TREEVIEW_ITEM *item;
924 int order;
926 if (!start)
928 start = infoPtr->root->firstChild;
929 order = 0;
931 else
932 order = start->visibleOrder;
934 for (item = start; item != NULL;
935 item = TREEVIEW_GetNextListItem(infoPtr, item))
937 if (!ISVISIBLE(item) && order > 0)
938 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
939 item->visibleOrder = order;
940 order += item->iIntegral;
943 infoPtr->maxVisibleOrder = order;
945 for (item = start; item != NULL;
946 item = TREEVIEW_GetNextListItem(infoPtr, item))
948 TREEVIEW_ComputeItemRect(infoPtr, item);
953 /* Update metrics of all items in selected subtree.
954 * root must be expanded
956 static VOID
957 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
959 TREEVIEW_ITEM *sibling;
960 HDC hdc;
961 HFONT hOldFont;
963 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
964 return;
966 root->state &= ~TVIS_EXPANDED;
967 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
968 root->state |= TVIS_EXPANDED;
970 hdc = GetDC(infoPtr->hwnd);
971 hOldFont = SelectObject(hdc, infoPtr->hFont);
973 for (; root != sibling;
974 root = TREEVIEW_GetNextListItem(infoPtr, root))
976 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
978 if (root->callbackMask & TVIF_TEXT)
979 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
981 if (root->textWidth == 0)
983 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
984 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
988 SelectObject(hdc, hOldFont);
989 ReleaseDC(infoPtr->hwnd, hdc);
992 /* Item Allocation **********************************************************/
994 static TREEVIEW_ITEM *
995 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
997 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
999 if (!newItem)
1000 return NULL;
1002 /* I_IMAGENONE would make more sense but this is neither what is
1003 * documented (MSDN doesn't specify) nor what Windows actually does
1004 * (it sets it to zero)... and I can so imagine an application using
1005 * inc/dec to toggle the images. */
1006 newItem->iImage = 0;
1007 newItem->iSelectedImage = 0;
1009 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1011 Free(newItem);
1012 return NULL;
1015 return newItem;
1018 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1019 * free item->pszText. */
1020 static void
1021 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1023 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1024 Free(item);
1025 if (infoPtr->selectedItem == item)
1026 infoPtr->selectedItem = NULL;
1027 if (infoPtr->hotItem == item)
1028 infoPtr->hotItem = NULL;
1029 if (infoPtr->focusedItem == item)
1030 infoPtr->focusedItem = NULL;
1031 if (infoPtr->firstVisible == item)
1032 infoPtr->firstVisible = NULL;
1033 if (infoPtr->dropItem == item)
1034 infoPtr->dropItem = NULL;
1035 if (infoPtr->insertMarkItem == item)
1036 infoPtr->insertMarkItem = NULL;
1040 /* Item Insertion *******************************************************/
1042 /***************************************************************************
1043 * This method inserts newItem before sibling as a child of parent.
1044 * sibling can be NULL, but only if parent has no children.
1046 static void
1047 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1048 TREEVIEW_ITEM *parent)
1050 assert(newItem != NULL);
1051 assert(parent != NULL);
1053 if (sibling != NULL)
1055 assert(sibling->parent == parent);
1057 if (sibling->prevSibling != NULL)
1058 sibling->prevSibling->nextSibling = newItem;
1060 newItem->prevSibling = sibling->prevSibling;
1061 sibling->prevSibling = newItem;
1063 else
1064 newItem->prevSibling = NULL;
1066 newItem->nextSibling = sibling;
1068 if (parent->firstChild == sibling)
1069 parent->firstChild = newItem;
1071 if (parent->lastChild == NULL)
1072 parent->lastChild = newItem;
1075 /***************************************************************************
1076 * This method inserts newItem after sibling as a child of parent.
1077 * sibling can be NULL, but only if parent has no children.
1079 static void
1080 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1081 TREEVIEW_ITEM *parent)
1083 assert(newItem != NULL);
1084 assert(parent != NULL);
1086 if (sibling != NULL)
1088 assert(sibling->parent == parent);
1090 if (sibling->nextSibling != NULL)
1091 sibling->nextSibling->prevSibling = newItem;
1093 newItem->nextSibling = sibling->nextSibling;
1094 sibling->nextSibling = newItem;
1096 else
1097 newItem->nextSibling = NULL;
1099 newItem->prevSibling = sibling;
1101 if (parent->lastChild == sibling)
1102 parent->lastChild = newItem;
1104 if (parent->firstChild == NULL)
1105 parent->firstChild = newItem;
1108 static BOOL
1109 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1110 const TVITEMEXW *tvItem, BOOL isW)
1112 UINT callbackClear = 0;
1113 UINT callbackSet = 0;
1115 TRACE("item %p\n", wineItem);
1116 /* Do this first in case it fails. */
1117 if (tvItem->mask & TVIF_TEXT)
1119 wineItem->textWidth = 0; /* force width recalculation */
1120 if (tvItem->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
1122 int len;
1123 LPWSTR newText;
1124 if (isW)
1125 len = lstrlenW(tvItem->pszText) + 1;
1126 else
1127 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1129 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1131 if (newText == NULL) return FALSE;
1133 callbackClear |= TVIF_TEXT;
1135 wineItem->pszText = newText;
1136 wineItem->cchTextMax = len;
1137 if (isW)
1138 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1139 else
1140 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1141 wineItem->pszText, len);
1143 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1145 else
1147 callbackSet |= TVIF_TEXT;
1149 wineItem->pszText = ReAlloc(wineItem->pszText,
1150 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1151 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1152 TRACE("setting callback, item %p\n", wineItem);
1156 if (tvItem->mask & TVIF_CHILDREN)
1158 wineItem->cChildren = tvItem->cChildren;
1160 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1161 callbackSet |= TVIF_CHILDREN;
1162 else
1163 callbackClear |= TVIF_CHILDREN;
1166 if (tvItem->mask & TVIF_IMAGE)
1168 wineItem->iImage = tvItem->iImage;
1170 if (wineItem->iImage == I_IMAGECALLBACK)
1171 callbackSet |= TVIF_IMAGE;
1172 else
1173 callbackClear |= TVIF_IMAGE;
1176 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1178 wineItem->iSelectedImage = tvItem->iSelectedImage;
1180 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1181 callbackSet |= TVIF_SELECTEDIMAGE;
1182 else
1183 callbackClear |= TVIF_SELECTEDIMAGE;
1186 if (tvItem->mask & TVIF_PARAM)
1187 wineItem->lParam = tvItem->lParam;
1189 /* If the application sets TVIF_INTEGRAL without
1190 * supplying a TVITEMEX structure, it's toast. */
1191 if (tvItem->mask & TVIF_INTEGRAL)
1192 wineItem->iIntegral = tvItem->iIntegral;
1194 if (tvItem->mask & TVIF_STATE)
1196 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1197 tvItem->stateMask);
1198 wineItem->state &= ~tvItem->stateMask;
1199 wineItem->state |= (tvItem->state & tvItem->stateMask);
1202 wineItem->callbackMask |= callbackSet;
1203 wineItem->callbackMask &= ~callbackClear;
1205 return TRUE;
1208 /* Note that the new item is pre-zeroed. */
1209 static LRESULT
1210 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1212 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1213 HTREEITEM insertAfter;
1214 TREEVIEW_ITEM *newItem, *parentItem;
1215 BOOL bTextUpdated = FALSE;
1217 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1219 parentItem = infoPtr->root;
1221 else
1223 parentItem = ptdi->hParent;
1225 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1227 WARN("invalid parent %p\n", parentItem);
1228 return (LRESULT)(HTREEITEM)NULL;
1232 insertAfter = ptdi->hInsertAfter;
1234 /* Validate this now for convenience. */
1235 switch ((DWORD_PTR)insertAfter)
1237 case (DWORD_PTR)TVI_FIRST:
1238 case (DWORD_PTR)TVI_LAST:
1239 case (DWORD_PTR)TVI_SORT:
1240 break;
1242 default:
1243 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1244 insertAfter->parent != parentItem)
1246 WARN("invalid insert after %p\n", insertAfter);
1247 insertAfter = TVI_LAST;
1251 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1252 (tvItem->mask & TVIF_TEXT)
1253 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1254 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1255 : "<no label>");
1257 newItem = TREEVIEW_AllocateItem(infoPtr);
1258 if (newItem == NULL)
1259 return (LRESULT)(HTREEITEM)NULL;
1261 newItem->parent = parentItem;
1262 newItem->iIntegral = 1;
1264 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1265 return (LRESULT)(HTREEITEM)NULL;
1267 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1269 infoPtr->uNumItems++;
1271 switch ((DWORD_PTR)insertAfter)
1273 case (DWORD_PTR)TVI_FIRST:
1275 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1276 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1277 if (infoPtr->firstVisible == originalFirst)
1278 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1280 break;
1282 case (DWORD_PTR)TVI_LAST:
1283 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1284 break;
1286 /* hInsertAfter names a specific item we want to insert after */
1287 default:
1288 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1289 break;
1291 case (DWORD_PTR)TVI_SORT:
1293 TREEVIEW_ITEM *aChild;
1294 TREEVIEW_ITEM *previousChild = NULL;
1295 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1296 BOOL bItemInserted = FALSE;
1298 aChild = parentItem->firstChild;
1300 bTextUpdated = TRUE;
1301 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1303 /* Iterate the parent children to see where we fit in */
1304 while (aChild != NULL)
1306 INT comp;
1308 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1309 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1311 if (comp < 0) /* we are smaller than the current one */
1313 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1314 if (infoPtr->firstVisible == originalFirst &&
1315 aChild == originalFirst)
1316 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1317 bItemInserted = TRUE;
1318 break;
1320 else if (comp > 0) /* we are bigger than the current one */
1322 previousChild = aChild;
1324 /* This will help us to exit if there is no more sibling */
1325 aChild = (aChild->nextSibling == 0)
1326 ? NULL
1327 : aChild->nextSibling;
1329 /* Look at the next item */
1330 continue;
1332 else if (comp == 0)
1335 * An item with this name is already existing, therefore,
1336 * we add after the one we found
1338 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1339 bItemInserted = TRUE;
1340 break;
1345 * we reach the end of the child list and the item has not
1346 * yet been inserted, therefore, insert it after the last child.
1348 if ((!bItemInserted) && (aChild == NULL))
1349 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1351 break;
1356 TRACE("new item %p; parent %p, mask %x\n", newItem,
1357 newItem->parent, tvItem->mask);
1359 newItem->iLevel = newItem->parent->iLevel + 1;
1361 if (newItem->parent->cChildren == 0)
1362 newItem->parent->cChildren = 1;
1364 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1366 if (STATEIMAGEINDEX(newItem->state) == 0)
1367 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1370 if (infoPtr->firstVisible == NULL)
1371 infoPtr->firstVisible = newItem;
1373 TREEVIEW_VerifyTree(infoPtr);
1375 if (parentItem == infoPtr->root ||
1376 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1378 TREEVIEW_ITEM *item;
1379 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1381 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1382 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1384 if (!bTextUpdated)
1385 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1387 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1388 TREEVIEW_UpdateScrollBars(infoPtr);
1390 * if the item was inserted in a visible part of the tree,
1391 * invalidate it, as well as those after it
1393 for (item = newItem;
1394 item != NULL;
1395 item = TREEVIEW_GetNextListItem(infoPtr, item))
1396 TREEVIEW_Invalidate(infoPtr, item);
1398 else
1400 newItem->visibleOrder = -1;
1402 /* refresh treeview if newItem is the first item inserted under parentItem */
1403 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1405 /* parent got '+' - update it */
1406 TREEVIEW_Invalidate(infoPtr, parentItem);
1410 return (LRESULT)newItem;
1413 /* Item Deletion ************************************************************/
1414 static void
1415 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1417 static void
1418 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1420 TREEVIEW_ITEM *kill = parentItem->firstChild;
1422 while (kill != NULL)
1424 TREEVIEW_ITEM *next = kill->nextSibling;
1426 TREEVIEW_RemoveItem(infoPtr, kill);
1428 kill = next;
1431 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1432 assert(parentItem->firstChild == NULL);
1433 assert(parentItem->lastChild == NULL);
1436 static void
1437 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1439 TREEVIEW_ITEM *parentItem = item->parent;
1441 assert(item != NULL);
1442 assert(item->parent != NULL); /* i.e. it must not be the root */
1444 if (parentItem->firstChild == item)
1445 parentItem->firstChild = item->nextSibling;
1447 if (parentItem->lastChild == item)
1448 parentItem->lastChild = item->prevSibling;
1450 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1451 && parentItem->cChildren > 0)
1452 parentItem->cChildren = 0;
1454 if (item->prevSibling)
1455 item->prevSibling->nextSibling = item->nextSibling;
1457 if (item->nextSibling)
1458 item->nextSibling->prevSibling = item->prevSibling;
1461 static void
1462 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1464 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1466 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1467 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1469 if (wineItem->firstChild)
1470 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1472 TREEVIEW_UnlinkItem(wineItem);
1474 infoPtr->uNumItems--;
1476 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1477 Free(wineItem->pszText);
1479 TREEVIEW_FreeItem(infoPtr, wineItem);
1483 /* Empty out the tree. */
1484 static void
1485 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1487 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1489 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1492 static LRESULT
1493 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1495 TREEVIEW_ITEM *newSelection = NULL;
1496 TREEVIEW_ITEM *newFirstVisible = NULL;
1497 TREEVIEW_ITEM *parent, *prev = NULL;
1498 BOOL visible = FALSE;
1500 if (wineItem == TVI_ROOT)
1502 TRACE("TVI_ROOT\n");
1503 parent = infoPtr->root;
1504 newSelection = NULL;
1505 visible = TRUE;
1506 TREEVIEW_RemoveTree(infoPtr);
1508 else
1510 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1511 return FALSE;
1513 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1514 parent = wineItem->parent;
1516 if (ISVISIBLE(wineItem))
1518 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1519 visible = TRUE;
1522 if (infoPtr->selectedItem != NULL
1523 && (wineItem == infoPtr->selectedItem
1524 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1526 if (wineItem->nextSibling)
1527 newSelection = wineItem->nextSibling;
1528 else if (wineItem->parent != infoPtr->root)
1529 newSelection = wineItem->parent;
1530 else
1531 newSelection = wineItem->prevSibling;
1532 TRACE("newSelection = %p\n", newSelection);
1535 if (infoPtr->firstVisible == wineItem)
1537 if (wineItem->nextSibling)
1538 newFirstVisible = wineItem->nextSibling;
1539 else if (wineItem->prevSibling)
1540 newFirstVisible = wineItem->prevSibling;
1541 else if (wineItem->parent != infoPtr->root)
1542 newFirstVisible = wineItem->parent;
1543 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1545 else
1546 newFirstVisible = infoPtr->firstVisible;
1548 TREEVIEW_RemoveItem(infoPtr, wineItem);
1551 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1552 if (!infoPtr->selectedItem && newSelection)
1554 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1555 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1558 /* Validate insertMark dropItem.
1559 * hotItem ??? - used for comparison only.
1561 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1562 infoPtr->insertMarkItem = 0;
1564 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1565 infoPtr->dropItem = 0;
1567 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1568 newFirstVisible = infoPtr->root->firstChild;
1570 TREEVIEW_VerifyTree(infoPtr);
1573 if (visible)
1575 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1576 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1577 TREEVIEW_UpdateScrollBars(infoPtr);
1578 TREEVIEW_Invalidate(infoPtr, NULL);
1580 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1582 /* parent lost '+/-' - update it */
1583 TREEVIEW_Invalidate(infoPtr, parent);
1586 return TRUE;
1590 /* Get/Set Messages *********************************************************/
1591 static LRESULT
1592 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1594 if(wParam)
1595 infoPtr->bRedraw = TRUE;
1596 else
1597 infoPtr->bRedraw = FALSE;
1599 return 0;
1602 static LRESULT
1603 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1605 TRACE("\n");
1606 return infoPtr->uIndent;
1609 static LRESULT
1610 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1612 TRACE("\n");
1614 if (newIndent < MINIMUM_INDENT)
1615 newIndent = MINIMUM_INDENT;
1617 if (infoPtr->uIndent != newIndent)
1619 infoPtr->uIndent = newIndent;
1620 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1621 TREEVIEW_UpdateScrollBars(infoPtr);
1622 TREEVIEW_Invalidate(infoPtr, NULL);
1625 return 0;
1629 static LRESULT
1630 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1632 TRACE("\n");
1633 return (LRESULT)infoPtr->hwndToolTip;
1636 static LRESULT
1637 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1639 HWND prevToolTip;
1641 TRACE("\n");
1642 prevToolTip = infoPtr->hwndToolTip;
1643 infoPtr->hwndToolTip = hwndTT;
1645 return (LRESULT)prevToolTip;
1648 static LRESULT
1649 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1651 BOOL rc = infoPtr->bNtfUnicode;
1652 infoPtr->bNtfUnicode = fUnicode;
1653 return rc;
1656 static LRESULT
1657 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1659 return infoPtr->bNtfUnicode;
1662 static LRESULT
1663 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1665 return infoPtr->uScrollTime;
1668 static LRESULT
1669 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1671 UINT uOldScrollTime = infoPtr->uScrollTime;
1673 infoPtr->uScrollTime = min(uScrollTime, 100);
1675 return uOldScrollTime;
1679 static LRESULT
1680 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1682 TRACE("\n");
1684 switch (wParam)
1686 case (WPARAM)TVSIL_NORMAL:
1687 return (LRESULT)infoPtr->himlNormal;
1689 case (WPARAM)TVSIL_STATE:
1690 return (LRESULT)infoPtr->himlState;
1692 default:
1693 return 0;
1697 #define TVHEIGHT_MIN 16
1698 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1700 /* Compute the natural height for items. */
1701 static UINT
1702 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1704 TEXTMETRICW tm;
1705 HDC hdc = GetDC(0);
1706 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1707 UINT height;
1709 /* Height is the maximum of:
1710 * 16 (a hack because our fonts are tiny), and
1711 * The text height + border & margin, and
1712 * The size of the normal image list
1714 GetTextMetricsW(hdc, &tm);
1715 SelectObject(hdc, hOldFont);
1716 ReleaseDC(0, hdc);
1718 height = TVHEIGHT_MIN;
1719 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1720 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1721 if (height < infoPtr->normalImageHeight)
1722 height = infoPtr->normalImageHeight;
1724 /* Round down, unless we support odd ("non even") heights. */
1725 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1726 height &= ~1;
1728 return height;
1731 static LRESULT
1732 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1734 HIMAGELIST himlOld = 0;
1735 int oldWidth = infoPtr->normalImageWidth;
1736 int oldHeight = infoPtr->normalImageHeight;
1739 TRACE("%lx,%p\n", wParam, himlNew);
1741 switch (wParam)
1743 case (WPARAM)TVSIL_NORMAL:
1744 himlOld = infoPtr->himlNormal;
1745 infoPtr->himlNormal = himlNew;
1747 if (himlNew != NULL)
1748 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1749 &infoPtr->normalImageHeight);
1750 else
1752 infoPtr->normalImageWidth = 0;
1753 infoPtr->normalImageHeight = 0;
1756 break;
1758 case (WPARAM)TVSIL_STATE:
1759 himlOld = infoPtr->himlState;
1760 infoPtr->himlState = himlNew;
1762 if (himlNew != NULL)
1763 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1764 &infoPtr->stateImageHeight);
1765 else
1767 infoPtr->stateImageWidth = 0;
1768 infoPtr->stateImageHeight = 0;
1771 break;
1774 if (oldWidth != infoPtr->normalImageWidth ||
1775 oldHeight != infoPtr->normalImageHeight)
1777 BOOL bRecalcVisible = FALSE;
1779 if (oldHeight != infoPtr->normalImageHeight &&
1780 !infoPtr->bHeightSet)
1782 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1783 bRecalcVisible = TRUE;
1786 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1787 infoPtr->normalImageWidth != infoPtr->uIndent)
1789 infoPtr->uIndent = infoPtr->normalImageWidth;
1790 bRecalcVisible = TRUE;
1793 if (bRecalcVisible)
1794 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1796 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1797 TREEVIEW_UpdateScrollBars(infoPtr);
1800 TREEVIEW_Invalidate(infoPtr, NULL);
1802 return (LRESULT)himlOld;
1805 static LRESULT
1806 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1808 INT prevHeight = infoPtr->uItemHeight;
1810 TRACE("%d\n", newHeight);
1811 if (newHeight == -1)
1813 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1814 infoPtr->bHeightSet = FALSE;
1816 else
1818 infoPtr->uItemHeight = newHeight;
1819 infoPtr->bHeightSet = TRUE;
1822 /* Round down, unless we support odd ("non even") heights. */
1823 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1824 infoPtr->uItemHeight &= ~1;
1826 if (infoPtr->uItemHeight != prevHeight)
1828 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1829 TREEVIEW_UpdateScrollBars(infoPtr);
1830 TREEVIEW_Invalidate(infoPtr, NULL);
1833 return prevHeight;
1836 static LRESULT
1837 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1839 TRACE("\n");
1840 return infoPtr->uItemHeight;
1844 static LRESULT
1845 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1847 TRACE("%p\n", infoPtr->hFont);
1848 return (LRESULT)infoPtr->hFont;
1852 static INT CALLBACK
1853 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1855 (void)unused;
1857 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1859 return 1;
1862 static LRESULT
1863 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1865 UINT uHeight = infoPtr->uItemHeight;
1867 TRACE("%p %i\n", hFont, bRedraw);
1869 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1871 DeleteObject(infoPtr->hBoldFont);
1872 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1873 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1875 if (!infoPtr->bHeightSet)
1876 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1878 if (uHeight != infoPtr->uItemHeight)
1879 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1881 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1883 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1884 TREEVIEW_UpdateScrollBars(infoPtr);
1886 if (bRedraw)
1887 TREEVIEW_Invalidate(infoPtr, NULL);
1889 return 0;
1893 static LRESULT
1894 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1896 TRACE("\n");
1897 return (LRESULT)infoPtr->clrLine;
1900 static LRESULT
1901 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1903 COLORREF prevColor = infoPtr->clrLine;
1905 TRACE("\n");
1906 infoPtr->clrLine = color;
1907 return (LRESULT)prevColor;
1911 static LRESULT
1912 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1914 TRACE("\n");
1915 return (LRESULT)infoPtr->clrText;
1918 static LRESULT
1919 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1921 COLORREF prevColor = infoPtr->clrText;
1923 TRACE("\n");
1924 infoPtr->clrText = color;
1926 if (infoPtr->clrText != prevColor)
1927 TREEVIEW_Invalidate(infoPtr, NULL);
1929 return (LRESULT)prevColor;
1933 static LRESULT
1934 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1936 TRACE("\n");
1937 return (LRESULT)infoPtr->clrBk;
1940 static LRESULT
1941 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1943 COLORREF prevColor = infoPtr->clrBk;
1945 TRACE("\n");
1946 infoPtr->clrBk = newColor;
1948 if (newColor != prevColor)
1949 TREEVIEW_Invalidate(infoPtr, NULL);
1951 return (LRESULT)prevColor;
1955 static LRESULT
1956 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1958 TRACE("\n");
1959 return (LRESULT)infoPtr->clrInsertMark;
1962 static LRESULT
1963 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1965 COLORREF prevColor = infoPtr->clrInsertMark;
1967 TRACE("%x\n", color);
1968 infoPtr->clrInsertMark = color;
1970 return (LRESULT)prevColor;
1974 static LRESULT
1975 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1977 TRACE("%d %p\n", wParam, item);
1979 if (!TREEVIEW_ValidItem(infoPtr, item))
1980 return 0;
1982 infoPtr->insertBeforeorAfter = wParam;
1983 infoPtr->insertMarkItem = item;
1985 TREEVIEW_Invalidate(infoPtr, NULL);
1987 return 1;
1991 /************************************************************************
1992 * Some serious braindamage here. lParam is a pointer to both the
1993 * input HTREEITEM and the output RECT.
1995 static LRESULT
1996 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1998 TREEVIEW_ITEM *wineItem;
1999 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2001 TRACE("\n");
2003 * validate parameters
2005 if (pItem == NULL)
2006 return FALSE;
2008 wineItem = *pItem;
2009 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2010 return FALSE;
2013 * If wParam is TRUE return the text size otherwise return
2014 * the whole item size
2016 if (fTextRect)
2018 /* Windows does not send TVN_GETDISPINFO here. */
2020 lpRect->top = wineItem->rect.top;
2021 lpRect->bottom = wineItem->rect.bottom;
2023 lpRect->left = wineItem->textOffset;
2024 if (!wineItem->textWidth)
2025 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2027 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2029 else
2031 *lpRect = wineItem->rect;
2034 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2036 return TRUE;
2039 static inline LRESULT
2040 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2042 /* Surprise! This does not take integral height into account. */
2043 return infoPtr->clientHeight / infoPtr->uItemHeight;
2047 static LRESULT
2048 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2050 TREEVIEW_ITEM *wineItem;
2052 wineItem = tvItem->hItem;
2053 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2054 return FALSE;
2056 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2058 if (tvItem->mask & TVIF_CHILDREN)
2060 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2061 FIXME("I_CHILDRENCALLBACK not supported\n");
2062 tvItem->cChildren = wineItem->cChildren;
2065 if (tvItem->mask & TVIF_HANDLE)
2066 tvItem->hItem = wineItem;
2068 if (tvItem->mask & TVIF_IMAGE)
2069 tvItem->iImage = wineItem->iImage;
2071 if (tvItem->mask & TVIF_INTEGRAL)
2072 tvItem->iIntegral = wineItem->iIntegral;
2074 /* undocumented: windows ignores TVIF_PARAM and
2075 * * always sets lParam
2077 tvItem->lParam = wineItem->lParam;
2079 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2080 tvItem->iSelectedImage = wineItem->iSelectedImage;
2082 if (tvItem->mask & TVIF_STATE)
2083 /* Careful here - Windows ignores the stateMask when you get the state
2084 That contradicts the documentation, but makes more common sense, masking
2085 retrieval in this way seems overkill */
2086 tvItem->state = wineItem->state;
2088 if (tvItem->mask & TVIF_TEXT)
2090 if (isW)
2092 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2094 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2095 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2097 else
2099 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2102 else
2104 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2106 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2107 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2109 else
2111 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2112 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2116 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2117 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2119 return TRUE;
2122 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2123 * which is wrong. */
2124 static LRESULT
2125 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2127 TREEVIEW_ITEM *wineItem;
2128 TREEVIEW_ITEM originalItem;
2130 wineItem = tvItem->hItem;
2132 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2133 tvItem->mask);
2135 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2136 return FALSE;
2138 /* store the original item values */
2139 originalItem = *wineItem;
2141 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2142 return FALSE;
2144 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2145 if ((tvItem->mask & TVIF_TEXT
2146 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2147 && ISVISIBLE(wineItem))
2149 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2150 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2153 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2155 /* The refresh updates everything, but we can't wait until then. */
2156 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2158 /* if any of the item's values changed and it's not a callback, redraw the item */
2159 if (item_changed(&originalItem, wineItem, tvItem))
2161 if (tvItem->mask & TVIF_INTEGRAL)
2163 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2164 TREEVIEW_UpdateScrollBars(infoPtr);
2166 TREEVIEW_Invalidate(infoPtr, NULL);
2168 else
2170 TREEVIEW_UpdateScrollBars(infoPtr);
2171 TREEVIEW_Invalidate(infoPtr, wineItem);
2176 return TRUE;
2179 static LRESULT
2180 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2182 TRACE("\n");
2184 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2185 return 0;
2187 return (wineItem->state & mask);
2190 static LRESULT
2191 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2193 TREEVIEW_ITEM *retval;
2195 retval = 0;
2197 /* handle all the global data here */
2198 switch (which)
2200 case TVGN_CHILD: /* Special case: child of 0 is root */
2201 if (wineItem)
2202 break;
2203 /* fall through */
2204 case TVGN_ROOT:
2205 retval = infoPtr->root->firstChild;
2206 break;
2208 case TVGN_CARET:
2209 retval = infoPtr->selectedItem;
2210 break;
2212 case TVGN_FIRSTVISIBLE:
2213 retval = infoPtr->firstVisible;
2214 break;
2216 case TVGN_DROPHILITE:
2217 retval = infoPtr->dropItem;
2218 break;
2220 case TVGN_LASTVISIBLE:
2221 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2222 break;
2225 if (retval)
2227 TRACE("flags:%x, returns %p\n", which, retval);
2228 return (LRESULT)retval;
2231 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2233 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2234 return FALSE;
2236 switch (which)
2238 case TVGN_NEXT:
2239 retval = wineItem->nextSibling;
2240 break;
2241 case TVGN_PREVIOUS:
2242 retval = wineItem->prevSibling;
2243 break;
2244 case TVGN_PARENT:
2245 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2246 break;
2247 case TVGN_CHILD:
2248 retval = wineItem->firstChild;
2249 break;
2250 case TVGN_NEXTVISIBLE:
2251 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2252 break;
2253 case TVGN_PREVIOUSVISIBLE:
2254 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2255 break;
2256 default:
2257 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2258 break;
2261 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2262 return (LRESULT)retval;
2266 static LRESULT
2267 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2269 TRACE(" %d\n", infoPtr->uNumItems);
2270 return (LRESULT)infoPtr->uNumItems;
2273 static VOID
2274 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2276 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2278 static const unsigned int state_table[] = { 0, 2, 1 };
2280 unsigned int state;
2282 state = STATEIMAGEINDEX(item->state);
2283 TRACE("state:%x\n", state);
2284 item->state &= ~TVIS_STATEIMAGEMASK;
2286 if (state < 3)
2287 state = state_table[state];
2289 item->state |= INDEXTOSTATEIMAGEMASK(state);
2291 TRACE("state:%x\n", state);
2292 TREEVIEW_Invalidate(infoPtr, item);
2297 /* Painting *************************************************************/
2299 /* Draw the lines and expand button for an item. Also draws one section
2300 * of the line from item's parent to item's parent's next sibling. */
2301 static void
2302 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2304 LONG centerx, centery;
2305 BOOL lar = ((infoPtr->dwStyle
2306 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2307 > TVS_LINESATROOT);
2308 HBRUSH hbr, hbrOld;
2310 if (!lar && item->iLevel == 0)
2311 return;
2313 hbr = CreateSolidBrush(infoPtr->clrBk);
2314 hbrOld = SelectObject(hdc, hbr);
2316 centerx = (item->linesOffset + item->stateOffset) / 2;
2317 centery = (item->rect.top + item->rect.bottom) / 2;
2319 if (infoPtr->dwStyle & TVS_HASLINES)
2321 HPEN hOldPen, hNewPen;
2322 HTREEITEM parent;
2323 LOGBRUSH lb;
2325 /* Get a dotted grey pen */
2326 lb.lbStyle = BS_SOLID;
2327 lb.lbColor = infoPtr->clrLine;
2328 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2329 hOldPen = SelectObject(hdc, hNewPen);
2331 /* Make sure the center is on a dot (using +2 instead
2332 * of +1 gives us pixel-by-pixel compat with native) */
2333 centery = (centery + 2) & ~1;
2335 MoveToEx(hdc, item->stateOffset, centery, NULL);
2336 LineTo(hdc, centerx - 1, centery);
2338 if (item->prevSibling || item->parent != infoPtr->root)
2340 MoveToEx(hdc, centerx, item->rect.top, NULL);
2341 LineTo(hdc, centerx, centery);
2344 if (item->nextSibling)
2346 MoveToEx(hdc, centerx, centery, NULL);
2347 LineTo(hdc, centerx, item->rect.bottom + 1);
2350 /* Draw the line from our parent to its next sibling. */
2351 parent = item->parent;
2352 while (parent != infoPtr->root)
2354 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2356 if (parent->nextSibling
2357 /* skip top-levels unless TVS_LINESATROOT */
2358 && parent->stateOffset > parent->linesOffset)
2360 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2361 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2364 parent = parent->parent;
2367 SelectObject(hdc, hOldPen);
2368 DeleteObject(hNewPen);
2372 * Display the (+/-) signs
2375 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2377 if (item->cChildren)
2379 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2380 if (theme)
2382 RECT glyphRect = item->rect;
2383 glyphRect.left = item->linesOffset;
2384 glyphRect.right = item->stateOffset;
2385 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2386 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2387 &glyphRect, NULL);
2389 else
2391 LONG height = item->rect.bottom - item->rect.top;
2392 LONG width = item->stateOffset - item->linesOffset;
2393 LONG rectsize = min(height, width) / 4;
2394 /* plussize = ceil(rectsize * 3/4) */
2395 LONG plussize = (rectsize + 1) * 3 / 4;
2397 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2398 HPEN hOldPen = SelectObject(hdc, hNewPen);
2400 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2401 centerx + rectsize + 2, centery + rectsize + 2);
2403 SelectObject(hdc, hOldPen);
2404 DeleteObject(hNewPen);
2406 if (height < 18 || width < 18)
2408 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2409 LineTo(hdc, centerx + plussize, centery);
2411 if (!(item->state & TVIS_EXPANDED))
2413 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2414 LineTo(hdc, centerx, centery + plussize);
2417 else
2419 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2420 centerx + plussize, centery + 2);
2422 if (!(item->state & TVIS_EXPANDED))
2424 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2425 centerx + 2, centery + plussize);
2426 SetPixel(hdc, centerx - 1, centery, infoPtr->clrBk);
2427 SetPixel(hdc, centerx + 1, centery, infoPtr->clrBk);
2433 SelectObject(hdc, hbrOld);
2434 DeleteObject(hbr);
2437 static void
2438 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2440 INT cditem;
2441 HFONT hOldFont;
2442 COLORREF oldTextColor, oldTextBkColor;
2443 int centery;
2444 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2445 NMTVCUSTOMDRAW nmcdhdr;
2447 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2449 /* - If item is drop target or it is selected and window is in focus -
2450 * use blue background (COLOR_HIGHLIGHT).
2451 * - If item is selected, window is not in focus, but it has style
2452 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2453 * - Otherwise - use background color
2455 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2456 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2457 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2459 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2461 nmcdhdr.clrTextBk = GetSysColor(COLOR_HIGHLIGHT);
2462 nmcdhdr.clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
2464 else
2466 nmcdhdr.clrTextBk = GetSysColor(COLOR_BTNFACE);
2467 if (infoPtr->clrText == -1)
2468 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2469 else
2470 nmcdhdr.clrText = infoPtr->clrText;
2473 else
2475 nmcdhdr.clrTextBk = infoPtr->clrBk;
2476 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2477 nmcdhdr.clrText = comctl32_color.clrHighlight;
2478 else if (infoPtr->clrText == -1)
2479 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2480 else
2481 nmcdhdr.clrText = infoPtr->clrText;
2484 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2486 /* The custom draw handler can query the text rectangle,
2487 * so get ready. */
2488 /* should already be known, set to 0 when changed */
2489 if (!wineItem->textWidth)
2490 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2492 cditem = 0;
2494 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2496 cditem = TREEVIEW_SendCustomDrawItemNotify
2497 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2498 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2500 if (cditem & CDRF_SKIPDEFAULT)
2502 SelectObject(hdc, hOldFont);
2503 return;
2507 if (cditem & CDRF_NEWFONT)
2508 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2510 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2512 /* Set colors. Custom draw handler can change these so we do this after it. */
2513 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2514 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2516 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2519 * Display the images associated with this item
2522 INT imageIndex;
2524 /* State images are displayed to the left of the Normal image
2525 * image number is in state; zero should be `display no image'.
2527 imageIndex = STATEIMAGEINDEX(wineItem->state);
2529 if (infoPtr->himlState && imageIndex)
2531 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2532 wineItem->stateOffset,
2533 centery - infoPtr->stateImageHeight / 2,
2534 ILD_NORMAL);
2537 /* Now, draw the normal image; can be either selected or
2538 * non-selected image.
2541 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2543 /* The item is currently selected */
2544 imageIndex = wineItem->iSelectedImage;
2546 else
2548 /* The item is not selected */
2549 imageIndex = wineItem->iImage;
2552 if (infoPtr->himlNormal)
2554 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2556 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2557 wineItem->imageOffset,
2558 centery - infoPtr->normalImageHeight / 2,
2559 ILD_NORMAL | ovlIdx);
2565 * Display the text associated with this item
2568 /* Don't paint item's text if it's being edited */
2569 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2571 if (wineItem->pszText)
2573 RECT rcText;
2575 rcText.top = wineItem->rect.top;
2576 rcText.bottom = wineItem->rect.bottom;
2577 rcText.left = wineItem->textOffset;
2578 rcText.right = rcText.left + wineItem->textWidth + 4;
2580 TRACE("drawing text %s at (%s)\n",
2581 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2583 /* Draw it */
2584 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2585 ETO_CLIPPED | ETO_OPAQUE,
2586 &rcText,
2587 wineItem->pszText,
2588 lstrlenW(wineItem->pszText),
2589 NULL);
2591 /* Draw the box around the selected item */
2592 if ((wineItem == infoPtr->selectedItem) && inFocus)
2594 DrawFocusRect(hdc,&rcText);
2600 /* Draw insertion mark if necessary */
2602 if (infoPtr->insertMarkItem)
2603 TRACE("item:%d,mark:%p\n",
2604 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2605 infoPtr->insertMarkItem);
2607 if (wineItem == infoPtr->insertMarkItem)
2609 HPEN hNewPen, hOldPen;
2610 int offset;
2611 int left, right;
2613 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2614 hOldPen = SelectObject(hdc, hNewPen);
2616 if (infoPtr->insertBeforeorAfter)
2617 offset = wineItem->rect.bottom - 1;
2618 else
2619 offset = wineItem->rect.top + 1;
2621 left = wineItem->textOffset - 2;
2622 right = wineItem->textOffset + wineItem->textWidth + 2;
2624 MoveToEx(hdc, left, offset - 3, NULL);
2625 LineTo(hdc, left, offset + 4);
2627 MoveToEx(hdc, left, offset, NULL);
2628 LineTo(hdc, right + 1, offset);
2630 MoveToEx(hdc, right, offset + 3, NULL);
2631 LineTo(hdc, right, offset - 4);
2633 SelectObject(hdc, hOldPen);
2634 DeleteObject(hNewPen);
2637 if (cditem & CDRF_NOTIFYPOSTPAINT)
2639 cditem = TREEVIEW_SendCustomDrawItemNotify
2640 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2641 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2644 /* Restore the hdc state */
2645 SetTextColor(hdc, oldTextColor);
2646 SetBkColor(hdc, oldTextBkColor);
2647 SelectObject(hdc, hOldFont);
2650 /* Computes treeHeight and treeWidth and updates the scroll bars.
2652 static void
2653 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2655 TREEVIEW_ITEM *wineItem;
2656 HWND hwnd = infoPtr->hwnd;
2657 BOOL vert = FALSE;
2658 BOOL horz = FALSE;
2659 SCROLLINFO si;
2660 LONG scrollX = infoPtr->scrollX;
2662 infoPtr->treeWidth = 0;
2663 infoPtr->treeHeight = 0;
2665 /* We iterate through all visible items in order to get the tree height
2666 * and width */
2667 wineItem = infoPtr->root->firstChild;
2669 while (wineItem != NULL)
2671 if (ISVISIBLE(wineItem))
2673 /* actually we draw text at textOffset + 2 */
2674 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2675 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2677 /* This is scroll-adjusted, but we fix this below. */
2678 infoPtr->treeHeight = wineItem->rect.bottom;
2681 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2684 /* Fix the scroll adjusted treeHeight and treeWidth. */
2685 if (infoPtr->root->firstChild)
2686 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2688 infoPtr->treeWidth += infoPtr->scrollX;
2690 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2692 /* Adding one scroll bar may take up enough space that it forces us
2693 * to add the other as well. */
2694 if (infoPtr->treeHeight > infoPtr->clientHeight)
2696 vert = TRUE;
2698 if (infoPtr->treeWidth
2699 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2700 horz = TRUE;
2702 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2703 horz = TRUE;
2705 if (!vert && horz && infoPtr->treeHeight
2706 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2707 vert = TRUE;
2709 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2711 si.cbSize = sizeof(SCROLLINFO);
2712 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2713 si.nMin = 0;
2715 if (vert)
2717 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2718 if ( si.nPage && NULL != infoPtr->firstVisible)
2720 si.nPos = infoPtr->firstVisible->visibleOrder;
2721 si.nMax = infoPtr->maxVisibleOrder - 1;
2723 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2725 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2726 ShowScrollBar(hwnd, SB_VERT, TRUE);
2727 infoPtr->uInternalStatus |= TV_VSCROLL;
2729 else
2731 if (infoPtr->uInternalStatus & TV_VSCROLL)
2732 ShowScrollBar(hwnd, SB_VERT, FALSE);
2733 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2736 else
2738 if (infoPtr->uInternalStatus & TV_VSCROLL)
2739 ShowScrollBar(hwnd, SB_VERT, FALSE);
2740 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2743 if (horz)
2745 si.nPage = infoPtr->clientWidth;
2746 si.nPos = infoPtr->scrollX;
2747 si.nMax = infoPtr->treeWidth - 1;
2749 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2751 si.nPos = si.nMax - max( si.nPage-1, 0 );
2752 scrollX = si.nPos;
2755 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2756 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2757 infoPtr->uInternalStatus |= TV_HSCROLL;
2759 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2760 TREEVIEW_HScroll(infoPtr,
2761 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2763 else
2765 if (infoPtr->uInternalStatus & TV_HSCROLL)
2766 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2767 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2769 scrollX = 0;
2770 if (infoPtr->scrollX != 0)
2772 TREEVIEW_HScroll(infoPtr,
2773 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2777 if (!horz)
2778 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2781 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2782 static LRESULT
2783 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hDC)
2785 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2786 RECT rect;
2788 GetClientRect(infoPtr->hwnd, &rect);
2789 FillRect(hDC, &rect, hBrush);
2790 DeleteObject(hBrush);
2792 return 1;
2795 static void
2796 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2798 HWND hwnd = infoPtr->hwnd;
2799 RECT rect = *rc;
2800 TREEVIEW_ITEM *wineItem;
2802 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2804 TRACE("empty window\n");
2805 return;
2808 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2809 hdc, rect);
2811 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2813 ReleaseDC(hwnd, hdc);
2814 return;
2817 for (wineItem = infoPtr->root->firstChild;
2818 wineItem != NULL;
2819 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2821 if (ISVISIBLE(wineItem))
2823 /* Avoid unneeded calculations */
2824 if (wineItem->rect.top > rect.bottom)
2825 break;
2826 if (wineItem->rect.bottom < rect.top)
2827 continue;
2829 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2833 TREEVIEW_UpdateScrollBars(infoPtr);
2835 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2836 infoPtr->cdmode =
2837 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2840 static void
2841 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2843 if (item != NULL)
2844 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2845 else
2846 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2849 static LRESULT
2850 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2852 HDC hdc;
2853 PAINTSTRUCT ps;
2854 RECT rc;
2856 TRACE("\n");
2858 if (wParam)
2860 hdc = (HDC)wParam;
2861 GetClientRect(infoPtr->hwnd, &rc);
2862 TREEVIEW_EraseBackground(infoPtr, hdc);
2864 else
2866 hdc = BeginPaint(infoPtr->hwnd, &ps);
2867 rc = ps.rcPaint;
2870 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2871 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2873 if (!wParam)
2874 EndPaint(infoPtr->hwnd, &ps);
2876 return 0;
2880 /* Sorting **************************************************************/
2882 /***************************************************************************
2883 * Forward the DPA local callback to the treeview owner callback
2885 static INT WINAPI
2886 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2887 const TVSORTCB *pCallBackSort)
2889 /* Forward the call to the client-defined callback */
2890 return pCallBackSort->lpfnCompare(first->lParam,
2891 second->lParam,
2892 pCallBackSort->lParam);
2895 /***************************************************************************
2896 * Treeview native sort routine: sort on item text.
2898 static INT WINAPI
2899 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2900 const TREEVIEW_INFO *infoPtr)
2902 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2903 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2905 if(first->pszText && second->pszText)
2906 return lstrcmpiW(first->pszText, second->pszText);
2907 else if(first->pszText)
2908 return -1;
2909 else if(second->pszText)
2910 return 1;
2911 else
2912 return 0;
2915 /* Returns the number of physical children belonging to item. */
2916 static INT
2917 TREEVIEW_CountChildren(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2919 INT cChildren = 0;
2920 HTREEITEM hti;
2922 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2923 cChildren++;
2925 return cChildren;
2928 /* Returns a DPA containing a pointer to each physical child of item in
2929 * sibling order. If item has no children, an empty DPA is returned. */
2930 static HDPA
2931 TREEVIEW_BuildChildDPA(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2933 HTREEITEM child = item->firstChild;
2935 HDPA list = DPA_Create(8);
2936 if (list == 0) return NULL;
2938 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2940 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2942 DPA_Destroy(list);
2943 return NULL;
2947 return list;
2950 /***************************************************************************
2951 * Setup the treeview structure with regards of the sort method
2952 * and sort the children of the TV item specified in lParam
2953 * fRecurse: currently unused. Should be zero.
2954 * parent: if pSort!=NULL, should equal pSort->hParent.
2955 * otherwise, item which child items are to be sorted.
2956 * pSort: sort method info. if NULL, sort on item text.
2957 * if non-NULL, sort on item's lParam content, and let the
2958 * application decide what that means. See also TVM_SORTCHILDRENCB.
2961 static LRESULT
2962 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2963 LPTVSORTCB pSort)
2965 INT cChildren;
2966 PFNDPACOMPARE pfnCompare;
2967 LPARAM lpCompare;
2969 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2970 if (parent == TVI_ROOT || parent == NULL)
2971 parent = infoPtr->root;
2973 /* Check for a valid handle to the parent item */
2974 if (!TREEVIEW_ValidItem(infoPtr, parent))
2976 ERR("invalid item hParent=%p\n", parent);
2977 return FALSE;
2980 if (pSort)
2982 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2983 lpCompare = (LPARAM)pSort;
2985 else
2987 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2988 lpCompare = (LPARAM)infoPtr;
2991 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2993 /* Make sure there is something to sort */
2994 if (cChildren > 1)
2996 /* TREEVIEW_ITEM rechaining */
2997 INT count = 0;
2998 HTREEITEM item = 0;
2999 HTREEITEM nextItem = 0;
3000 HTREEITEM prevItem = 0;
3002 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
3004 if (sortList == NULL)
3005 return FALSE;
3007 /* let DPA sort the list */
3008 DPA_Sort(sortList, pfnCompare, lpCompare);
3010 /* The order of DPA entries has been changed, so fixup the
3011 * nextSibling and prevSibling pointers. */
3013 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
3014 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
3016 /* link the two current item together */
3017 item->nextSibling = nextItem;
3018 nextItem->prevSibling = item;
3020 if (prevItem == NULL)
3022 /* this is the first item, update the parent */
3023 parent->firstChild = item;
3024 item->prevSibling = NULL;
3026 else
3028 /* fix the back chaining */
3029 item->prevSibling = prevItem;
3032 /* get ready for the next one */
3033 prevItem = item;
3034 item = nextItem;
3037 /* the last item is pointed to by item and never has a sibling */
3038 item->nextSibling = NULL;
3039 parent->lastChild = item;
3041 DPA_Destroy(sortList);
3043 TREEVIEW_VerifyTree(infoPtr);
3045 if (parent->state & TVIS_EXPANDED)
3047 int visOrder = infoPtr->firstVisible->visibleOrder;
3049 if (parent == infoPtr->root)
3050 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3051 else
3052 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3054 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3056 TREEVIEW_ITEM *item;
3058 for (item = infoPtr->root->firstChild; item != NULL;
3059 item = TREEVIEW_GetNextListItem(infoPtr, item))
3061 if (item->visibleOrder == visOrder)
3062 break;
3065 if (!item) item = parent->firstChild;
3066 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3069 TREEVIEW_Invalidate(infoPtr, NULL);
3072 return TRUE;
3074 return FALSE;
3078 /***************************************************************************
3079 * Setup the treeview structure with regards of the sort method
3080 * and sort the children of the TV item specified in lParam
3082 static LRESULT
3083 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3085 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3089 /***************************************************************************
3090 * Sort the children of the TV item specified in lParam.
3092 static LRESULT
3093 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3095 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3099 /* Expansion/Collapse ***************************************************/
3101 static BOOL
3102 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3103 UINT action)
3105 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3106 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3107 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3108 0, wineItem);
3111 static VOID
3112 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3113 UINT action)
3115 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3116 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3117 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3118 0, wineItem);
3122 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3123 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3124 static BOOL
3125 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3126 BOOL bRemoveChildren, BOOL bUser)
3128 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3129 BOOL bSetSelection, bSetFirstVisible;
3130 RECT scrollRect;
3131 LONG scrollDist = 0;
3132 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3134 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3136 if (!(wineItem->state & TVIS_EXPANDED))
3137 return FALSE;
3139 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3140 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3142 if (wineItem->firstChild == NULL)
3143 return FALSE;
3145 wineItem->state &= ~TVIS_EXPANDED;
3147 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3148 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3150 bSetSelection = (infoPtr->selectedItem != NULL
3151 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3153 bSetFirstVisible = (infoPtr->firstVisible != NULL
3154 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3156 tmpItem = wineItem;
3157 while (tmpItem)
3159 if (tmpItem->nextSibling)
3161 nextItem = tmpItem->nextSibling;
3162 break;
3164 tmpItem = tmpItem->parent;
3167 if (nextItem)
3168 scrollDist = nextItem->rect.top;
3170 if (bRemoveChildren)
3172 INT old_cChildren = wineItem->cChildren;
3173 TRACE("TVE_COLLAPSERESET\n");
3174 wineItem->state &= ~TVIS_EXPANDEDONCE;
3175 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3176 wineItem->cChildren = old_cChildren;
3179 if (wineItem->firstChild)
3181 TREEVIEW_ITEM *item, *sibling;
3183 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3185 for (item = wineItem->firstChild; item != sibling;
3186 item = TREEVIEW_GetNextListItem(infoPtr, item))
3188 item->visibleOrder = -1;
3192 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3194 if (nextItem)
3195 scrollDist = -(scrollDist - nextItem->rect.top);
3197 if (bSetSelection)
3199 /* Don't call DoSelectItem, it sends notifications. */
3200 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3201 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3202 wineItem->state |= TVIS_SELECTED;
3203 infoPtr->selectedItem = wineItem;
3206 TREEVIEW_UpdateScrollBars(infoPtr);
3208 scrollRect.left = 0;
3209 scrollRect.right = infoPtr->clientWidth;
3210 scrollRect.bottom = infoPtr->clientHeight;
3212 if (nextItem)
3214 scrollRect.top = nextItem->rect.top;
3216 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3217 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3218 TREEVIEW_Invalidate(infoPtr, wineItem);
3219 } else {
3220 scrollRect.top = wineItem->rect.top;
3221 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3224 TREEVIEW_SetFirstVisible(infoPtr,
3225 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3226 TRUE);
3228 return TRUE;
3231 static BOOL
3232 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3233 BOOL bExpandPartial, BOOL bUser)
3235 LONG scrollDist;
3236 LONG orgNextTop = 0;
3237 RECT scrollRect;
3238 TREEVIEW_ITEM *nextItem, *tmpItem;
3240 TRACE("\n");
3242 if (wineItem->state & TVIS_EXPANDED)
3243 return TRUE;
3245 tmpItem = wineItem; nextItem = NULL;
3246 while (tmpItem)
3248 if (tmpItem->nextSibling)
3250 nextItem = tmpItem->nextSibling;
3251 break;
3253 tmpItem = tmpItem->parent;
3256 if (nextItem)
3257 orgNextTop = nextItem->rect.top;
3259 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3261 if (bUser || ((wineItem->cChildren != 0) &&
3262 !(wineItem->state & TVIS_EXPANDEDONCE)))
3264 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3266 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3267 return FALSE;
3270 if (!wineItem->firstChild)
3271 return FALSE;
3273 wineItem->state |= TVIS_EXPANDED;
3274 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3275 wineItem->state |= TVIS_EXPANDEDONCE;
3277 else
3279 if (!wineItem->firstChild)
3280 return FALSE;
3282 /* this item has already been expanded */
3283 wineItem->state |= TVIS_EXPANDED;
3286 if (bExpandPartial)
3287 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3289 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3290 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3291 TREEVIEW_UpdateScrollBars(infoPtr);
3293 scrollRect.left = 0;
3294 scrollRect.bottom = infoPtr->treeHeight;
3295 scrollRect.right = infoPtr->clientWidth;
3296 if (nextItem)
3298 scrollDist = nextItem->rect.top - orgNextTop;
3299 scrollRect.top = orgNextTop;
3301 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3302 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3303 TREEVIEW_Invalidate (infoPtr, wineItem);
3304 } else {
3305 scrollRect.top = wineItem->rect.top;
3306 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3309 /* Scroll up so that as many children as possible are visible.
3310 * This fails when expanding causes an HScroll bar to appear, but we
3311 * don't know that yet, so the last item is obscured. */
3312 if (wineItem->firstChild != NULL)
3314 int nChildren = wineItem->lastChild->visibleOrder
3315 - wineItem->firstChild->visibleOrder + 1;
3317 int visible_pos = wineItem->visibleOrder
3318 - infoPtr->firstVisible->visibleOrder;
3320 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3322 if (visible_pos > 0 && nChildren > rows_below)
3324 int scroll = nChildren - rows_below;
3326 if (scroll > visible_pos)
3327 scroll = visible_pos;
3329 if (scroll > 0)
3331 TREEVIEW_ITEM *newFirstVisible
3332 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3333 scroll);
3336 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3341 return TRUE;
3344 static BOOL
3345 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3347 TRACE("\n");
3349 if (wineItem->state & TVIS_EXPANDED)
3350 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3351 else
3352 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3355 static VOID
3356 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3358 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3360 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3362 if (TREEVIEW_HasChildren(infoPtr, item))
3363 TREEVIEW_ExpandAll(infoPtr, item);
3367 /* Note:If the specified item is the child of a collapsed parent item,
3368 the parent's list of child items is (recursively) expanded to reveal the
3369 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3370 know if it also applies here.
3373 static LRESULT
3374 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3376 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3377 return 0;
3379 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3380 TREEVIEW_ItemName(wineItem), flag,
3381 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3383 switch (flag & TVE_TOGGLE)
3385 case TVE_COLLAPSE:
3386 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3387 FALSE);
3389 case TVE_EXPAND:
3390 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3391 FALSE);
3393 case TVE_TOGGLE:
3394 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3396 default:
3397 return 0;
3400 #if 0
3401 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3402 #endif
3405 /* Hit-Testing **********************************************************/
3407 static TREEVIEW_ITEM *
3408 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3410 TREEVIEW_ITEM *wineItem;
3411 LONG row;
3413 if (!infoPtr->firstVisible)
3414 return NULL;
3416 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3418 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3419 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3421 if (row >= wineItem->visibleOrder
3422 && row < wineItem->visibleOrder + wineItem->iIntegral)
3423 break;
3426 return wineItem;
3429 static LRESULT
3430 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3432 TREEVIEW_ITEM *wineItem;
3433 RECT rect;
3434 UINT status;
3435 LONG x, y;
3437 lpht->hItem = 0;
3438 GetClientRect(infoPtr->hwnd, &rect);
3439 status = 0;
3440 x = lpht->pt.x;
3441 y = lpht->pt.y;
3443 if (x < rect.left)
3445 status |= TVHT_TOLEFT;
3447 else if (x > rect.right)
3449 status |= TVHT_TORIGHT;
3452 if (y < rect.top)
3454 status |= TVHT_ABOVE;
3456 else if (y > rect.bottom)
3458 status |= TVHT_BELOW;
3461 if (status)
3463 lpht->flags = status;
3464 return (LRESULT)(HTREEITEM)NULL;
3467 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3468 if (!wineItem)
3470 lpht->flags = TVHT_NOWHERE;
3471 return (LRESULT)(HTREEITEM)NULL;
3474 if (x >= wineItem->textOffset + wineItem->textWidth)
3476 lpht->flags = TVHT_ONITEMRIGHT;
3478 else if (x >= wineItem->textOffset)
3480 lpht->flags = TVHT_ONITEMLABEL;
3482 else if (x >= wineItem->imageOffset)
3484 lpht->flags = TVHT_ONITEMICON;
3486 else if (x >= wineItem->stateOffset)
3488 lpht->flags = TVHT_ONITEMSTATEICON;
3490 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3492 lpht->flags = TVHT_ONITEMBUTTON;
3494 else
3496 lpht->flags = TVHT_ONITEMINDENT;
3499 lpht->hItem = wineItem;
3500 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3502 return (LRESULT)wineItem;
3505 /* Item Label Editing ***************************************************/
3507 static LRESULT
3508 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3510 return (LRESULT)infoPtr->hwndEdit;
3513 static LRESULT CALLBACK
3514 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3516 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3517 BOOL bCancel = FALSE;
3518 LRESULT rc;
3520 switch (uMsg)
3522 case WM_PAINT:
3523 TRACE("WM_PAINT start\n");
3524 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3525 lParam);
3526 TRACE("WM_PAINT done\n");
3527 return rc;
3529 case WM_KILLFOCUS:
3530 if (infoPtr->bIgnoreEditKillFocus)
3531 return TRUE;
3532 break;
3534 case WM_GETDLGCODE:
3535 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3537 case WM_KEYDOWN:
3538 if (wParam == (WPARAM)VK_ESCAPE)
3540 bCancel = TRUE;
3541 break;
3543 else if (wParam == (WPARAM)VK_RETURN)
3545 break;
3548 /* fall through */
3549 default:
3550 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3553 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3554 /* eg. Using a messagebox */
3556 infoPtr->bIgnoreEditKillFocus = TRUE;
3557 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3558 infoPtr->bIgnoreEditKillFocus = FALSE;
3560 return 0;
3564 /* should handle edit control messages here */
3566 static LRESULT
3567 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3569 TRACE("%lx %ld\n", wParam, lParam);
3571 switch (HIWORD(wParam))
3573 case EN_UPDATE:
3576 * Adjust the edit window size
3578 WCHAR buffer[1024];
3579 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3580 HDC hdc = GetDC(infoPtr->hwndEdit);
3581 SIZE sz;
3582 int len;
3583 HFONT hFont, hOldFont = 0;
3585 infoPtr->bLabelChanged = TRUE;
3587 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer));
3589 /* Select font to get the right dimension of the string */
3590 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3592 if (hFont != 0)
3594 hOldFont = SelectObject(hdc, hFont);
3597 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3599 TEXTMETRICW textMetric;
3601 /* Add Extra spacing for the next character */
3602 GetTextMetricsW(hdc, &textMetric);
3603 sz.cx += (textMetric.tmMaxCharWidth * 2);
3605 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3606 sz.cx = min(sz.cx,
3607 infoPtr->clientWidth - editItem->textOffset + 2);
3609 SetWindowPos(infoPtr->hwndEdit,
3610 HWND_TOP,
3613 sz.cx,
3614 editItem->rect.bottom - editItem->rect.top + 3,
3615 SWP_NOMOVE | SWP_DRAWFRAME);
3618 if (hFont != 0)
3620 SelectObject(hdc, hOldFont);
3623 ReleaseDC(infoPtr->hwnd, hdc);
3624 break;
3627 default:
3628 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3631 return 0;
3634 static HWND
3635 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3637 HWND hwnd = infoPtr->hwnd;
3638 HWND hwndEdit;
3639 SIZE sz;
3640 TREEVIEW_ITEM *editItem = hItem;
3641 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3642 HDC hdc;
3643 HFONT hOldFont=0;
3644 TEXTMETRICW textMetric;
3645 static const WCHAR EditW[] = {'E','d','i','t',0};
3647 TRACE("%p %p\n", hwnd, hItem);
3648 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3649 return NULL;
3651 if (infoPtr->hwndEdit)
3652 return infoPtr->hwndEdit;
3654 infoPtr->bLabelChanged = FALSE;
3656 /* Make sure that edit item is selected */
3657 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3658 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3660 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3662 hdc = GetDC(hwnd);
3663 /* Select the font to get appropriate metric dimensions */
3664 if (infoPtr->hFont != 0)
3666 hOldFont = SelectObject(hdc, infoPtr->hFont);
3669 /* Get string length in pixels */
3670 GetTextExtentPoint32W(hdc, editItem->pszText, strlenW(editItem->pszText),
3671 &sz);
3673 /* Add Extra spacing for the next character */
3674 GetTextMetricsW(hdc, &textMetric);
3675 sz.cx += (textMetric.tmMaxCharWidth * 2);
3677 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3678 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3680 if (infoPtr->hFont != 0)
3682 SelectObject(hdc, hOldFont);
3685 ReleaseDC(hwnd, hdc);
3686 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3687 EditW,
3689 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3690 WS_CLIPSIBLINGS | ES_WANTRETURN |
3691 ES_LEFT, editItem->textOffset - 2,
3692 editItem->rect.top - 1, sz.cx + 3,
3693 editItem->rect.bottom -
3694 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3695 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3697 infoPtr->hwndEdit = hwndEdit;
3699 /* Get a 2D border. */
3700 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3701 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3702 SetWindowLongW(hwndEdit, GWL_STYLE,
3703 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3705 SendMessageW(hwndEdit, WM_SETFONT,
3706 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3708 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3709 (DWORD_PTR)
3710 TREEVIEW_Edit_SubclassProc);
3712 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3714 DestroyWindow(hwndEdit);
3715 infoPtr->hwndEdit = 0;
3716 return NULL;
3719 infoPtr->selectedItem = hItem;
3720 SetWindowTextW(hwndEdit, editItem->pszText);
3721 SetFocus(hwndEdit);
3722 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3723 ShowWindow(hwndEdit, SW_SHOW);
3725 return hwndEdit;
3729 static LRESULT
3730 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3732 HWND hwnd = infoPtr->hwnd;
3733 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3734 NMTVDISPINFOW tvdi;
3735 BOOL bCommit;
3736 WCHAR tmpText[1024] = { '\0' };
3737 WCHAR *newText = tmpText;
3738 int iLength = 0;
3740 if (!infoPtr->hwndEdit)
3741 return FALSE;
3743 tvdi.hdr.hwndFrom = hwnd;
3744 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3745 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3746 tvdi.item.mask = 0;
3747 tvdi.item.hItem = editedItem;
3748 tvdi.item.state = editedItem->state;
3749 tvdi.item.lParam = editedItem->lParam;
3751 if (!bCancel)
3753 if (!infoPtr->bNtfUnicode)
3754 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3755 else
3756 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3758 if (iLength >= 1023)
3760 ERR("Insufficient space to retrieve new item label\n");
3763 tvdi.item.mask = TVIF_TEXT;
3764 tvdi.item.pszText = tmpText;
3765 tvdi.item.cchTextMax = iLength + 1;
3767 else
3769 tvdi.item.pszText = NULL;
3770 tvdi.item.cchTextMax = 0;
3773 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3775 if (!bCancel && bCommit) /* Apply the changes */
3777 if (!infoPtr->bNtfUnicode)
3779 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3780 newText = Alloc(len * sizeof(WCHAR));
3781 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3782 iLength = len - 1;
3785 if (strcmpW(newText, editedItem->pszText) != 0)
3787 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3788 if (ptr == NULL)
3790 ERR("OutOfMemory, cannot allocate space for label\n");
3791 if(newText != tmpText) Free(newText);
3792 DestroyWindow(infoPtr->hwndEdit);
3793 infoPtr->hwndEdit = 0;
3794 return FALSE;
3796 else
3798 editedItem->pszText = ptr;
3799 editedItem->cchTextMax = iLength + 1;
3800 strcpyW(editedItem->pszText, newText);
3801 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3804 if(newText != tmpText) Free(newText);
3807 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3808 DestroyWindow(infoPtr->hwndEdit);
3809 infoPtr->hwndEdit = 0;
3810 return TRUE;
3813 static LRESULT
3814 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3816 if (wParam != TV_EDIT_TIMER)
3818 ERR("got unknown timer\n");
3819 return 1;
3822 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3823 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3825 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3827 return 0;
3831 /* Mouse Tracking/Drag **************************************************/
3833 /***************************************************************************
3834 * This is quite unusual piece of code, but that's how it's implemented in
3835 * Windows.
3837 static LRESULT
3838 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3840 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3841 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3842 RECT r;
3843 MSG msg;
3845 r.top = pt.y - cyDrag;
3846 r.left = pt.x - cxDrag;
3847 r.bottom = pt.y + cyDrag;
3848 r.right = pt.x + cxDrag;
3850 SetCapture(infoPtr->hwnd);
3852 while (1)
3854 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3856 if (msg.message == WM_MOUSEMOVE)
3858 pt.x = (short)LOWORD(msg.lParam);
3859 pt.y = (short)HIWORD(msg.lParam);
3860 if (PtInRect(&r, pt))
3861 continue;
3862 else
3864 ReleaseCapture();
3865 return 1;
3868 else if (msg.message >= WM_LBUTTONDOWN &&
3869 msg.message <= WM_RBUTTONDBLCLK)
3871 if (msg.message == WM_RBUTTONUP)
3872 TREEVIEW_RButtonUp(infoPtr, &pt);
3873 break;
3876 DispatchMessageW(&msg);
3879 if (GetCapture() != infoPtr->hwnd)
3880 return 0;
3883 ReleaseCapture();
3884 return 0;
3888 static LRESULT
3889 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3891 TREEVIEW_ITEM *wineItem;
3892 TVHITTESTINFO hit;
3894 TRACE("\n");
3895 SetFocus(infoPtr->hwnd);
3897 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3899 /* If there is pending 'edit label' event - kill it now */
3900 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3903 hit.pt.x = (short)LOWORD(lParam);
3904 hit.pt.y = (short)HIWORD(lParam);
3906 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3907 if (!wineItem)
3908 return 0;
3909 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3911 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3912 { /* FIXME! */
3913 switch (hit.flags)
3915 case TVHT_ONITEMRIGHT:
3916 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3917 break;
3919 case TVHT_ONITEMINDENT:
3920 if (!(infoPtr->dwStyle & TVS_HASLINES))
3922 break;
3924 else
3926 int level = hit.pt.x / infoPtr->uIndent;
3927 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3929 while (wineItem->iLevel > level)
3931 wineItem = wineItem->parent;
3934 /* fall through */
3937 case TVHT_ONITEMLABEL:
3938 case TVHT_ONITEMICON:
3939 case TVHT_ONITEMBUTTON:
3940 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3941 break;
3943 case TVHT_ONITEMSTATEICON:
3944 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3945 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3946 else
3947 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3948 break;
3951 return TRUE;
3955 static LRESULT
3956 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3958 HWND hwnd = infoPtr->hwnd;
3959 TVHITTESTINFO ht;
3960 BOOL bTrack, bDoLabelEdit;
3961 HTREEITEM tempItem;
3963 /* If Edit control is active - kill it and return.
3964 * The best way to do it is to set focus to itself.
3965 * Edit control subclassed procedure will automatically call
3966 * EndEditLabelNow.
3968 if (infoPtr->hwndEdit)
3970 SetFocus(hwnd);
3971 return 0;
3974 ht.pt.x = (short)LOWORD(lParam);
3975 ht.pt.y = (short)HIWORD(lParam);
3977 TREEVIEW_HitTest(infoPtr, &ht);
3978 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3980 /* update focusedItem and redraw both items */
3981 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3983 infoPtr->focusedItem = ht.hItem;
3984 InvalidateRect(hwnd, &ht.hItem->rect, TRUE);
3986 if(infoPtr->selectedItem)
3987 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3990 bTrack = (ht.flags & TVHT_ONITEM)
3991 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3994 * If the style allows editing and the node is already selected
3995 * and the click occurred on the item label...
3997 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
3998 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4000 /* Send NM_CLICK right away */
4001 if (!bTrack)
4002 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4003 goto setfocus;
4005 if (ht.flags & TVHT_ONITEMBUTTON)
4007 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4008 goto setfocus;
4010 else if (bTrack)
4011 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4012 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4014 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4015 infoPtr->dropItem = ht.hItem;
4017 /* clean up focusedItem as we dragged and won't select this item */
4018 if(infoPtr->focusedItem)
4020 /* refresh the item that was focused */
4021 tempItem = infoPtr->focusedItem;
4022 infoPtr->focusedItem = 0;
4023 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
4025 /* refresh the selected item to return the filled background */
4026 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
4029 return 0;
4033 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4034 goto setfocus;
4036 if (bDoLabelEdit)
4038 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4039 KillTimer(hwnd, TV_EDIT_TIMER);
4041 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4042 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4044 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4047 * if we are TVS_SINGLEEXPAND then we want this single click to
4048 * do a bunch of things.
4050 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4051 (infoPtr->hwndEdit == 0))
4053 TREEVIEW_ITEM *SelItem;
4056 * Send the notification
4058 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4061 * Close the previous selection all the way to the root
4062 * as long as the new selection is not a child
4064 if((infoPtr->selectedItem)
4065 && (infoPtr->selectedItem != ht.hItem))
4067 BOOL closeit = TRUE;
4068 SelItem = ht.hItem;
4070 /* determine if the hitItem is a child of the currently selected item */
4071 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4073 closeit = (SelItem != infoPtr->selectedItem);
4074 SelItem = SelItem->parent;
4077 if(closeit)
4079 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4080 SelItem = infoPtr->selectedItem;
4082 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4084 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4085 SelItem = SelItem->parent;
4091 * Expand the current item
4093 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4096 /* Select the current item */
4097 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4099 else if (ht.flags & TVHT_ONITEMSTATEICON)
4101 /* TVS_CHECKBOXES requires us to toggle the current state */
4102 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4103 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4106 setfocus:
4107 SetFocus(hwnd);
4108 return 0;
4112 static LRESULT
4113 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4115 TVHITTESTINFO ht;
4117 if (infoPtr->hwndEdit)
4119 SetFocus(infoPtr->hwnd);
4120 return 0;
4123 ht.pt.x = (short)LOWORD(lParam);
4124 ht.pt.y = (short)HIWORD(lParam);
4126 TREEVIEW_HitTest(infoPtr, &ht);
4128 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4130 if (ht.hItem)
4132 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4133 infoPtr->dropItem = ht.hItem;
4136 else
4138 SetFocus(infoPtr->hwnd);
4139 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4142 return 0;
4145 static LRESULT
4146 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4148 TVHITTESTINFO ht;
4150 ht.pt = *pPt;
4152 TREEVIEW_HitTest(infoPtr, &ht);
4154 if (ht.hItem)
4156 /* Change to screen coordinate for WM_CONTEXTMENU */
4157 ClientToScreen(infoPtr->hwnd, &ht.pt);
4159 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4160 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4161 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4163 return 0;
4167 static LRESULT
4168 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4170 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4171 INT cx, cy;
4172 HDC hdc, htopdc;
4173 HWND hwtop;
4174 HBITMAP hbmp, hOldbmp;
4175 SIZE size;
4176 RECT rc;
4177 HFONT hOldFont;
4179 TRACE("\n");
4181 if (!(infoPtr->himlNormal))
4182 return 0;
4184 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4185 return 0;
4187 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4189 hwtop = GetDesktopWindow();
4190 htopdc = GetDC(hwtop);
4191 hdc = CreateCompatibleDC(htopdc);
4193 hOldFont = SelectObject(hdc, infoPtr->hFont);
4194 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4195 &size);
4196 TRACE("%d %d %s %d\n", size.cx, size.cy, debugstr_w(dragItem->pszText),
4197 strlenW(dragItem->pszText));
4198 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4199 hOldbmp = SelectObject(hdc, hbmp);
4201 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4202 size.cx += cx;
4203 if (cy > size.cy)
4204 size.cy = cy;
4206 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4207 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4208 ILD_NORMAL);
4211 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4212 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4215 /* draw item text */
4217 SetRect(&rc, cx, 0, size.cx, size.cy);
4218 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4219 DT_LEFT);
4220 SelectObject(hdc, hOldFont);
4221 SelectObject(hdc, hOldbmp);
4223 ImageList_Add(infoPtr->dragList, hbmp, 0);
4225 DeleteDC(hdc);
4226 DeleteObject(hbmp);
4227 ReleaseDC(hwtop, htopdc);
4229 return (LRESULT)infoPtr->dragList;
4232 /* Selection ************************************************************/
4234 static LRESULT
4235 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4236 INT cause)
4238 TREEVIEW_ITEM *prevSelect;
4239 RECT rcFocused;
4241 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4243 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4244 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4245 newSelect ? newSelect->state : 0);
4247 /* reset and redraw focusedItem if focusedItem was set so we don't */
4248 /* have to worry about the previously focused item when we set a new one */
4249 if(infoPtr->focusedItem)
4251 rcFocused = (infoPtr->focusedItem)->rect;
4252 infoPtr->focusedItem = 0;
4253 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4256 switch (action)
4258 case TVGN_CARET:
4259 prevSelect = infoPtr->selectedItem;
4261 if (prevSelect == newSelect) {
4262 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4263 break;
4266 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4267 TVN_SELCHANGINGW,
4268 cause,
4269 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4270 prevSelect,
4271 newSelect))
4272 return FALSE;
4274 if (prevSelect)
4275 prevSelect->state &= ~TVIS_SELECTED;
4276 if (newSelect)
4277 newSelect->state |= TVIS_SELECTED;
4279 infoPtr->selectedItem = newSelect;
4281 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4283 if (prevSelect)
4284 TREEVIEW_Invalidate(infoPtr, prevSelect);
4285 if (newSelect)
4286 TREEVIEW_Invalidate(infoPtr, newSelect);
4288 TREEVIEW_SendTreeviewNotify(infoPtr,
4289 TVN_SELCHANGEDW,
4290 cause,
4291 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4292 prevSelect,
4293 newSelect);
4294 break;
4296 case TVGN_DROPHILITE:
4297 prevSelect = infoPtr->dropItem;
4299 if (prevSelect)
4300 prevSelect->state &= ~TVIS_DROPHILITED;
4302 infoPtr->dropItem = newSelect;
4304 if (newSelect)
4305 newSelect->state |= TVIS_DROPHILITED;
4307 TREEVIEW_Invalidate(infoPtr, prevSelect);
4308 TREEVIEW_Invalidate(infoPtr, newSelect);
4309 break;
4311 case TVGN_FIRSTVISIBLE:
4312 if (newSelect != NULL)
4314 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4315 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4316 TREEVIEW_Invalidate(infoPtr, NULL);
4318 break;
4321 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4322 return TRUE;
4325 /* FIXME: handle NM_KILLFOCUS etc */
4326 static LRESULT
4327 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4329 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4330 return FALSE;
4332 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4334 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4335 return FALSE;
4337 return TRUE;
4340 /*************************************************************************
4341 * TREEVIEW_ProcessLetterKeys
4343 * Processes keyboard messages generated by pressing the letter keys
4344 * on the keyboard.
4345 * What this does is perform a case insensitive search from the
4346 * current position with the following quirks:
4347 * - If two chars or more are pressed in quick succession we search
4348 * for the corresponding string (e.g. 'abc').
4349 * - If there is a delay we wipe away the current search string and
4350 * restart with just that char.
4351 * - If the user keeps pressing the same character, whether slowly or
4352 * fast, so that the search string is entirely composed of this
4353 * character ('aaaaa' for instance), then we search for first item
4354 * that starting with that character.
4355 * - If the user types the above character in quick succession, then
4356 * we must also search for the corresponding string ('aaaaa'), and
4357 * go to that string if there is a match.
4359 * RETURNS
4361 * Zero.
4363 * BUGS
4365 * - The current implementation has a list of characters it will
4366 * accept and it ignores everything else. In particular it will
4367 * ignore accentuated characters which seems to match what
4368 * Windows does. But I'm not sure it makes sense to follow
4369 * Windows there.
4370 * - We don't sound a beep when the search fails.
4371 * - The search should start from the focused item, not from the selected
4372 * item. One reason for this is to allow for multiple selections in trees.
4373 * But currently infoPtr->focusedItem does not seem very usable.
4375 * SEE ALSO
4377 * TREEVIEW_ProcessLetterKeys
4379 static INT TREEVIEW_ProcessLetterKeys(
4380 HWND hwnd, /* handle to the window */
4381 WPARAM charCode, /* the character code, the actual character */
4382 LPARAM keyData /* key data */
4385 TREEVIEW_INFO *infoPtr;
4386 HTREEITEM nItem;
4387 HTREEITEM endidx,idx;
4388 TVITEMEXW item;
4389 WCHAR buffer[MAX_PATH];
4390 DWORD timestamp,elapsed;
4392 /* simple parameter checking */
4393 if (!hwnd || !charCode || !keyData)
4394 return 0;
4396 infoPtr=(TREEVIEW_INFO*)GetWindowLongPtrW(hwnd, 0);
4397 if (!infoPtr)
4398 return 0;
4400 /* only allow the valid WM_CHARs through */
4401 if (!isalnum(charCode) &&
4402 charCode != '.' && charCode != '`' && charCode != '!' &&
4403 charCode != '@' && charCode != '#' && charCode != '$' &&
4404 charCode != '%' && charCode != '^' && charCode != '&' &&
4405 charCode != '*' && charCode != '(' && charCode != ')' &&
4406 charCode != '-' && charCode != '_' && charCode != '+' &&
4407 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4408 charCode != '}' && charCode != '[' && charCode != '{' &&
4409 charCode != '/' && charCode != '?' && charCode != '>' &&
4410 charCode != '<' && charCode != ',' && charCode != '~')
4411 return 0;
4413 /* compute how much time elapsed since last keypress */
4414 timestamp = GetTickCount();
4415 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4416 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4417 } else {
4418 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4421 /* update the search parameters */
4422 infoPtr->lastKeyPressTimestamp=timestamp;
4423 if (elapsed < KEY_DELAY) {
4424 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4425 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4427 if (infoPtr->charCode != charCode) {
4428 infoPtr->charCode=charCode=0;
4430 } else {
4431 infoPtr->charCode=charCode;
4432 infoPtr->szSearchParam[0]=charCode;
4433 infoPtr->nSearchParamLength=1;
4434 /* Redundant with the 1 char string */
4435 charCode=0;
4438 /* and search from the current position */
4439 nItem=NULL;
4440 if (infoPtr->selectedItem != NULL) {
4441 endidx=infoPtr->selectedItem;
4442 /* if looking for single character match,
4443 * then we must always move forward
4445 if (infoPtr->nSearchParamLength == 1)
4446 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4447 else
4448 idx=endidx;
4449 } else {
4450 endidx=NULL;
4451 idx=infoPtr->root->firstChild;
4453 do {
4454 /* At the end point, sort out wrapping */
4455 if (idx == NULL) {
4457 /* If endidx is null, stop at the last item (ie top to bottom) */
4458 if (endidx == NULL)
4459 break;
4461 /* Otherwise, start again at the very beginning */
4462 idx=infoPtr->root->firstChild;
4464 /* But if we are stopping on the first child, end now! */
4465 if (idx == endidx) break;
4468 /* get item */
4469 ZeroMemory(&item, sizeof(item));
4470 item.mask = TVIF_TEXT;
4471 item.hItem = idx;
4472 item.pszText = buffer;
4473 item.cchTextMax = sizeof(buffer);
4474 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4476 /* check for a match */
4477 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4478 nItem=idx;
4479 break;
4480 } else if ( (charCode != 0) && (nItem == NULL) &&
4481 (nItem != infoPtr->selectedItem) &&
4482 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4483 /* This would work but we must keep looking for a longer match */
4484 nItem=idx;
4486 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4487 } while (idx != endidx);
4489 if (nItem != NULL) {
4490 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4491 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4495 return 0;
4498 /* Scrolling ************************************************************/
4500 static LRESULT
4501 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4503 int viscount;
4504 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4505 HTREEITEM newFirstVisible = NULL;
4506 int visible_pos = -1;
4508 if (!TREEVIEW_ValidItem(infoPtr, item))
4509 return FALSE;
4511 if (!ISVISIBLE(item))
4513 /* Expand parents as necessary. */
4514 HTREEITEM parent;
4516 /* see if we are trying to ensure that root is visible */
4517 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4518 parent = item->parent;
4519 else
4520 parent = item; /* this item is the topmost item */
4522 while (parent != infoPtr->root)
4524 if (!(parent->state & TVIS_EXPANDED))
4525 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4527 parent = parent->parent;
4531 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4533 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4534 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4536 if (hasFirstVisible)
4537 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4539 if (visible_pos < 0)
4541 /* item is before the start of the list: put it at the top. */
4542 newFirstVisible = item;
4544 else if (visible_pos >= viscount
4545 /* Sometimes, before we are displayed, GVC is 0, causing us to
4546 * spuriously scroll up. */
4547 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4549 /* item is past the end of the list. */
4550 int scroll = visible_pos - viscount;
4552 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4553 scroll + 1);
4556 if (bHScroll)
4558 /* Scroll window so item's text is visible as much as possible */
4559 /* Calculation of amount of extra space is taken from EditLabel code */
4560 INT pos, x;
4561 TEXTMETRICW textMetric;
4562 HDC hdc = GetWindowDC(infoPtr->hwnd);
4564 x = item->textWidth;
4566 GetTextMetricsW(hdc, &textMetric);
4567 ReleaseDC(infoPtr->hwnd, hdc);
4569 x += (textMetric.tmMaxCharWidth * 2);
4570 x = max(x, textMetric.tmMaxCharWidth * 3);
4572 if (item->textOffset < 0)
4573 pos = item->textOffset;
4574 else if (item->textOffset + x > infoPtr->clientWidth)
4576 if (x > infoPtr->clientWidth)
4577 pos = item->textOffset;
4578 else
4579 pos = item->textOffset + x - infoPtr->clientWidth;
4581 else
4582 pos = 0;
4584 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4587 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4589 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4591 return TRUE;
4594 return FALSE;
4597 static VOID
4598 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4599 TREEVIEW_ITEM *newFirstVisible,
4600 BOOL bUpdateScrollPos)
4602 int gap_size;
4604 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4606 if (newFirstVisible != NULL)
4608 /* Prevent an empty gap from appearing at the bottom... */
4609 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4610 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4612 if (gap_size > 0)
4614 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4615 -gap_size);
4617 /* ... unless we just don't have enough items. */
4618 if (newFirstVisible == NULL)
4619 newFirstVisible = infoPtr->root->firstChild;
4623 if (infoPtr->firstVisible != newFirstVisible)
4625 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4627 infoPtr->firstVisible = newFirstVisible;
4628 TREEVIEW_Invalidate(infoPtr, NULL);
4630 else
4632 TREEVIEW_ITEM *item;
4633 int scroll = infoPtr->uItemHeight *
4634 (infoPtr->firstVisible->visibleOrder
4635 - newFirstVisible->visibleOrder);
4637 infoPtr->firstVisible = newFirstVisible;
4639 for (item = infoPtr->root->firstChild; item != NULL;
4640 item = TREEVIEW_GetNextListItem(infoPtr, item))
4642 item->rect.top += scroll;
4643 item->rect.bottom += scroll;
4646 if (bUpdateScrollPos)
4647 SetScrollPos(infoPtr->hwnd, SB_VERT,
4648 newFirstVisible->visibleOrder, TRUE);
4650 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4655 /************************************************************************
4656 * VScroll is always in units of visible items. i.e. we always have a
4657 * visible item aligned to the top of the control. (Unless we have no
4658 * items at all.)
4660 static LRESULT
4661 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4663 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4664 TREEVIEW_ITEM *newFirstVisible = NULL;
4666 int nScrollCode = LOWORD(wParam);
4668 TRACE("wp %lx\n", wParam);
4670 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4671 return 0;
4673 if (!oldFirstVisible)
4675 assert(infoPtr->root->firstChild == NULL);
4676 return 0;
4679 switch (nScrollCode)
4681 case SB_TOP:
4682 newFirstVisible = infoPtr->root->firstChild;
4683 break;
4685 case SB_BOTTOM:
4686 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4687 break;
4689 case SB_LINEUP:
4690 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4691 break;
4693 case SB_LINEDOWN:
4694 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4695 break;
4697 case SB_PAGEUP:
4698 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4699 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4700 break;
4702 case SB_PAGEDOWN:
4703 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4704 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4705 break;
4707 case SB_THUMBTRACK:
4708 case SB_THUMBPOSITION:
4709 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4710 infoPtr->root->firstChild,
4711 (LONG)(SHORT)HIWORD(wParam));
4712 break;
4714 case SB_ENDSCROLL:
4715 return 0;
4718 if (newFirstVisible != NULL)
4720 if (newFirstVisible != oldFirstVisible)
4721 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4722 nScrollCode != SB_THUMBTRACK);
4723 else if (nScrollCode == SB_THUMBPOSITION)
4724 SetScrollPos(infoPtr->hwnd, SB_VERT,
4725 newFirstVisible->visibleOrder, TRUE);
4728 return 0;
4731 static LRESULT
4732 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4734 int maxWidth;
4735 int scrollX = infoPtr->scrollX;
4736 int nScrollCode = LOWORD(wParam);
4738 TRACE("wp %lx\n", wParam);
4740 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4741 return FALSE;
4743 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4744 /* shall never occur */
4745 if (maxWidth <= 0)
4747 scrollX = 0;
4748 goto scroll;
4751 switch (nScrollCode)
4753 case SB_LINELEFT:
4754 scrollX -= infoPtr->uItemHeight;
4755 break;
4756 case SB_LINERIGHT:
4757 scrollX += infoPtr->uItemHeight;
4758 break;
4759 case SB_PAGELEFT:
4760 scrollX -= infoPtr->clientWidth;
4761 break;
4762 case SB_PAGERIGHT:
4763 scrollX += infoPtr->clientWidth;
4764 break;
4766 case SB_THUMBTRACK:
4767 case SB_THUMBPOSITION:
4768 scrollX = (int)(SHORT)HIWORD(wParam);
4769 break;
4771 case SB_ENDSCROLL:
4772 return 0;
4775 if (scrollX > maxWidth)
4776 scrollX = maxWidth;
4777 else if (scrollX < 0)
4778 scrollX = 0;
4780 scroll:
4781 if (scrollX != infoPtr->scrollX)
4783 TREEVIEW_ITEM *item;
4784 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4786 for (item = infoPtr->root->firstChild; item != NULL;
4787 item = TREEVIEW_GetNextListItem(infoPtr, item))
4789 item->linesOffset += scroll_pixels;
4790 item->stateOffset += scroll_pixels;
4791 item->imageOffset += scroll_pixels;
4792 item->textOffset += scroll_pixels;
4795 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4796 infoPtr->scrollX = scrollX;
4797 UpdateWindow(infoPtr->hwnd);
4800 if (nScrollCode != SB_THUMBTRACK)
4801 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4803 return 0;
4806 static LRESULT
4807 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4809 short gcWheelDelta;
4810 UINT pulScrollLines = 3;
4812 if (infoPtr->firstVisible == NULL)
4813 return TRUE;
4815 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4817 gcWheelDelta = -(short)HIWORD(wParam);
4818 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4820 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4822 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4823 int maxDy = infoPtr->maxVisibleOrder;
4825 if (newDy > maxDy)
4826 newDy = maxDy;
4828 if (newDy < 0)
4829 newDy = 0;
4831 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4833 return TRUE;
4836 /* Create/Destroy *******************************************************/
4838 static void
4839 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4841 RECT rc;
4842 HBITMAP hbm, hbmOld;
4843 HDC hdc, hdcScreen;
4844 int nIndex;
4846 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4848 hdcScreen = GetDC(0);
4850 hdc = CreateCompatibleDC(hdcScreen);
4851 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4852 hbmOld = SelectObject(hdc, hbm);
4854 SetRect(&rc, 0, 0, 48, 16);
4855 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4857 SetRect(&rc, 18, 2, 30, 14);
4858 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4859 DFCS_BUTTONCHECK|DFCS_FLAT);
4861 SetRect(&rc, 34, 2, 46, 14);
4862 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4863 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4865 SelectObject(hdc, hbmOld);
4866 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4867 GetSysColor(COLOR_WINDOW));
4868 TRACE("checkbox index %d\n", nIndex);
4870 DeleteObject(hbm);
4871 DeleteDC(hdc);
4872 ReleaseDC(0, hdcScreen);
4874 infoPtr->stateImageWidth = 16;
4875 infoPtr->stateImageHeight = 16;
4878 static LRESULT
4879 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4881 RECT rcClient;
4882 TREEVIEW_INFO *infoPtr;
4883 LOGFONTW lf;
4885 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4887 infoPtr = (TREEVIEW_INFO *)Alloc(sizeof(TREEVIEW_INFO));
4889 if (infoPtr == NULL)
4891 ERR("could not allocate info memory!\n");
4892 return 0;
4895 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4897 infoPtr->hwnd = hwnd;
4898 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4899 infoPtr->Timer = 0;
4900 infoPtr->uNumItems = 0;
4901 infoPtr->cdmode = 0;
4902 infoPtr->uScrollTime = 300; /* milliseconds */
4903 infoPtr->bRedraw = TRUE;
4905 GetClientRect(hwnd, &rcClient);
4907 /* No scroll bars yet. */
4908 infoPtr->clientWidth = rcClient.right;
4909 infoPtr->clientHeight = rcClient.bottom;
4910 infoPtr->uInternalStatus = 0;
4912 infoPtr->treeWidth = 0;
4913 infoPtr->treeHeight = 0;
4915 infoPtr->uIndent = MINIMUM_INDENT;
4916 infoPtr->selectedItem = 0;
4917 infoPtr->focusedItem = 0;
4918 infoPtr->hotItem = 0;
4919 infoPtr->firstVisible = 0;
4920 infoPtr->maxVisibleOrder = 0;
4921 infoPtr->dropItem = 0;
4922 infoPtr->insertMarkItem = 0;
4923 infoPtr->insertBeforeorAfter = 0;
4924 /* dragList */
4926 infoPtr->scrollX = 0;
4928 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4929 infoPtr->clrText = -1; /* use system color */
4930 infoPtr->clrLine = RGB(128, 128, 128);
4931 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4933 /* hwndToolTip */
4935 infoPtr->hwndEdit = 0;
4936 infoPtr->wpEditOrig = NULL;
4937 infoPtr->bIgnoreEditKillFocus = FALSE;
4938 infoPtr->bLabelChanged = FALSE;
4940 infoPtr->himlNormal = NULL;
4941 infoPtr->himlState = NULL;
4942 infoPtr->normalImageWidth = 0;
4943 infoPtr->normalImageHeight = 0;
4944 infoPtr->stateImageWidth = 0;
4945 infoPtr->stateImageHeight = 0;
4947 infoPtr->items = DPA_Create(16);
4949 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
4950 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
4951 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4952 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
4953 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
4955 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4957 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4958 infoPtr->root->state = TVIS_EXPANDED;
4959 infoPtr->root->iLevel = -1;
4960 infoPtr->root->visibleOrder = -1;
4962 infoPtr->hwndNotify = lpcs->hwndParent;
4963 #if 0
4964 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4965 #endif
4967 infoPtr->hwndToolTip = 0;
4969 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
4971 /* Determine what type of notify should be issued */
4972 /* sets infoPtr->bNtfUnicode */
4973 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
4975 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4976 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
4977 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4978 hwnd, 0, 0, 0);
4980 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4981 initialize_checkboxes(infoPtr);
4983 /* Make sure actual scrollbar state is consistent with uInternalStatus */
4984 ShowScrollBar(hwnd, SB_VERT, FALSE);
4985 ShowScrollBar(hwnd, SB_HORZ, FALSE);
4987 OpenThemeData (hwnd, themeClass);
4989 return 0;
4993 static LRESULT
4994 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4996 TRACE("\n");
4998 TREEVIEW_RemoveTree(infoPtr);
5000 /* tool tip is automatically destroyed: we are its owner */
5002 /* Restore original wndproc */
5003 if (infoPtr->hwndEdit)
5004 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5005 (DWORD_PTR)infoPtr->wpEditOrig);
5007 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5009 /* Deassociate treeview from the window before doing anything drastic. */
5010 SetWindowLongPtrW(infoPtr->hwnd, 0, (DWORD_PTR)NULL);
5013 DeleteObject(infoPtr->hDefaultFont);
5014 DeleteObject(infoPtr->hBoldFont);
5015 DeleteObject(infoPtr->hUnderlineFont);
5016 Free(infoPtr);
5018 return 0;
5021 /* Miscellaneous Messages ***********************************************/
5023 static LRESULT
5024 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5026 static const struct
5028 unsigned char code;
5030 scroll[] =
5032 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5033 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5034 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5035 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5036 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5037 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5038 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5039 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5040 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5041 #undef SCROLL_ENTRY
5044 if (key >= VK_PRIOR && key <= VK_DOWN)
5046 unsigned char code = scroll[key - VK_PRIOR].code;
5048 (((code & (1 << 7)) == (SB_HORZ << 7))
5049 ? TREEVIEW_HScroll
5050 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5053 return 0;
5056 /************************************************************************
5057 * TREEVIEW_KeyDown
5059 * VK_UP Move selection to the previous non-hidden item.
5060 * VK_DOWN Move selection to the next non-hidden item.
5061 * VK_HOME Move selection to the first item.
5062 * VK_END Move selection to the last item.
5063 * VK_LEFT If expanded then collapse, otherwise move to parent.
5064 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5065 * VK_ADD Expand.
5066 * VK_SUBTRACT Collapse.
5067 * VK_MULTIPLY Expand all.
5068 * VK_PRIOR Move up GetVisibleCount items.
5069 * VK_NEXT Move down GetVisibleCount items.
5070 * VK_BACK Move to parent.
5071 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5073 static LRESULT
5074 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5076 /* If it is non-NULL and different, it will be selected and visible. */
5077 TREEVIEW_ITEM *newSelection = NULL;
5079 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5081 TRACE("%lx\n", wParam);
5083 if (prevItem == NULL)
5084 return FALSE;
5086 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5087 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5089 switch (wParam)
5091 case VK_UP:
5092 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5093 if (!newSelection)
5094 newSelection = infoPtr->root->firstChild;
5095 break;
5097 case VK_DOWN:
5098 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5099 break;
5101 case VK_HOME:
5102 newSelection = infoPtr->root->firstChild;
5103 break;
5105 case VK_END:
5106 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5107 break;
5109 case VK_LEFT:
5110 if (prevItem->state & TVIS_EXPANDED)
5112 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5114 else if (prevItem->parent != infoPtr->root)
5116 newSelection = prevItem->parent;
5118 break;
5120 case VK_RIGHT:
5121 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5123 if (!(prevItem->state & TVIS_EXPANDED))
5124 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5125 else
5127 newSelection = prevItem->firstChild;
5131 break;
5133 case VK_MULTIPLY:
5134 TREEVIEW_ExpandAll(infoPtr, prevItem);
5135 break;
5137 case VK_ADD:
5138 if (!(prevItem->state & TVIS_EXPANDED))
5139 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5140 break;
5142 case VK_SUBTRACT:
5143 if (prevItem->state & TVIS_EXPANDED)
5144 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5145 break;
5147 case VK_PRIOR:
5148 newSelection
5149 = TREEVIEW_GetListItem(infoPtr, prevItem,
5150 -TREEVIEW_GetVisibleCount(infoPtr));
5151 break;
5153 case VK_NEXT:
5154 newSelection
5155 = TREEVIEW_GetListItem(infoPtr, prevItem,
5156 TREEVIEW_GetVisibleCount(infoPtr));
5157 break;
5159 case VK_BACK:
5160 newSelection = prevItem->parent;
5161 if (newSelection == infoPtr->root)
5162 newSelection = NULL;
5163 break;
5165 case VK_SPACE:
5166 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5167 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5168 break;
5171 if (newSelection && newSelection != prevItem)
5173 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5174 TVC_BYKEYBOARD))
5176 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5180 return FALSE;
5183 static LRESULT
5184 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5186 if (infoPtr->hotItem)
5188 /* remove hot effect from item */
5189 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5190 infoPtr->hotItem = NULL;
5192 return 0;
5195 static LRESULT
5196 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, WPARAM wParam, LPARAM lParam)
5198 POINT pt;
5199 TRACKMOUSEEVENT trackinfo;
5200 TREEVIEW_ITEM * item;
5202 /* fill in the TRACKMOUSEEVENT struct */
5203 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5204 trackinfo.dwFlags = TME_QUERY;
5205 trackinfo.hwndTrack = infoPtr->hwnd;
5206 trackinfo.dwHoverTime = HOVER_DEFAULT;
5208 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5209 _TrackMouseEvent(&trackinfo);
5211 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5212 if(!(trackinfo.dwFlags & TME_LEAVE))
5214 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5216 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5217 /* and can properly deactivate the hot item */
5218 _TrackMouseEvent(&trackinfo);
5221 pt.x = (short)LOWORD(lParam);
5222 pt.y = (short)HIWORD(lParam);
5224 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5226 if (item != infoPtr->hotItem)
5228 /* redraw old hot item */
5229 if (infoPtr->hotItem)
5230 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5231 infoPtr->hotItem = item;
5232 /* redraw new hot item */
5233 if (infoPtr->hotItem)
5234 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5237 return 0;
5240 /* Draw themed border */
5241 static BOOL nc_paint (const TREEVIEW_INFO *infoPtr, HRGN region)
5243 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5244 HDC dc;
5245 RECT r;
5246 HRGN cliprgn;
5247 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5248 cyEdge = GetSystemMetrics (SM_CYEDGE);
5250 if (!theme) return FALSE;
5252 GetWindowRect(infoPtr->hwnd, &r);
5254 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5255 r.right - cxEdge, r.bottom - cyEdge);
5256 if (region != (HRGN)1)
5257 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5258 OffsetRect(&r, -r.left, -r.top);
5260 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5261 OffsetRect(&r, -r.left, -r.top);
5263 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5264 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5265 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5266 ReleaseDC(infoPtr->hwnd, dc);
5268 /* Call default proc to get the scrollbars etc. painted */
5269 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5271 return TRUE;
5274 static LRESULT
5275 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5277 LPNMHDR lpnmh = (LPNMHDR)lParam;
5279 if (lpnmh->code == PGN_CALCSIZE) {
5280 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5282 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5283 lppgc->iWidth = infoPtr->treeWidth;
5284 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5285 infoPtr->treeWidth, infoPtr->clientWidth);
5287 else {
5288 lppgc->iHeight = infoPtr->treeHeight;
5289 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5290 infoPtr->treeHeight, infoPtr->clientHeight);
5292 return 0;
5294 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5297 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5299 INT format;
5301 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5303 if (nCommand != NF_REQUERY) return 0;
5305 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5306 TRACE("format=%d\n", format);
5308 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5310 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5312 return format;
5315 static LRESULT
5316 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5318 if (wParam == SIZE_RESTORED)
5320 infoPtr->clientWidth = (short)LOWORD(lParam);
5321 infoPtr->clientHeight = (short)HIWORD(lParam);
5323 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5324 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5325 TREEVIEW_UpdateScrollBars(infoPtr);
5327 else
5329 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5332 TREEVIEW_Invalidate(infoPtr, NULL);
5333 return 0;
5336 static LRESULT
5337 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5339 TRACE("(%lx %lx)\n", wParam, lParam);
5341 if (wParam == GWL_STYLE)
5343 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5345 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5347 if (dwNewStyle & TVS_CHECKBOXES)
5349 initialize_checkboxes(infoPtr);
5350 TRACE("checkboxes enabled\n");
5352 else
5354 FIXME("tried to disable checkboxes\n");
5358 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5360 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5362 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5363 TRACE("tooltips enabled\n");
5365 else
5367 DestroyWindow(infoPtr->hwndToolTip);
5368 infoPtr->hwndToolTip = 0;
5369 TRACE("tooltips disabled\n");
5373 infoPtr->dwStyle = dwNewStyle;
5376 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5377 TREEVIEW_UpdateScrollBars(infoPtr);
5378 TREEVIEW_Invalidate(infoPtr, NULL);
5380 return 0;
5383 static LRESULT
5384 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5386 POINT pt;
5387 TREEVIEW_ITEM * item;
5389 GetCursorPos(&pt);
5390 ScreenToClient(infoPtr->hwnd, &pt);
5392 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5394 /* FIXME: send NM_SETCURSOR */
5396 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5398 SetCursor(infoPtr->hcurHand);
5399 return 0;
5401 else
5402 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5405 static LRESULT
5406 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5408 TRACE("\n");
5410 if (!infoPtr->selectedItem)
5412 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5413 TVC_UNKNOWN);
5416 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5417 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5418 return 0;
5421 static LRESULT
5422 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5424 TRACE("\n");
5426 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5427 UpdateWindow(infoPtr->hwnd);
5428 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5429 return 0;
5432 /* update theme after a WM_THEMECHANGED message */
5433 static LRESULT theme_changed(const TREEVIEW_INFO *infoPtr)
5435 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5436 CloseThemeData (theme);
5437 OpenThemeData (infoPtr->hwnd, themeClass);
5438 return 0;
5442 static LRESULT WINAPI
5443 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5445 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5447 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5449 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5450 else
5452 if (uMsg == WM_CREATE)
5453 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5454 else
5455 goto def;
5458 switch (uMsg)
5460 case TVM_CREATEDRAGIMAGE:
5461 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5463 case TVM_DELETEITEM:
5464 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5466 case TVM_EDITLABELA:
5467 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5469 case TVM_EDITLABELW:
5470 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5472 case TVM_ENDEDITLABELNOW:
5473 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5475 case TVM_ENSUREVISIBLE:
5476 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5478 case TVM_EXPAND:
5479 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5481 case TVM_GETBKCOLOR:
5482 return TREEVIEW_GetBkColor(infoPtr);
5484 case TVM_GETCOUNT:
5485 return TREEVIEW_GetCount(infoPtr);
5487 case TVM_GETEDITCONTROL:
5488 return TREEVIEW_GetEditControl(infoPtr);
5490 case TVM_GETIMAGELIST:
5491 return TREEVIEW_GetImageList(infoPtr, wParam);
5493 case TVM_GETINDENT:
5494 return TREEVIEW_GetIndent(infoPtr);
5496 case TVM_GETINSERTMARKCOLOR:
5497 return TREEVIEW_GetInsertMarkColor(infoPtr);
5499 case TVM_GETISEARCHSTRINGA:
5500 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5501 return 0;
5503 case TVM_GETISEARCHSTRINGW:
5504 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5505 return 0;
5507 case TVM_GETITEMA:
5508 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5510 case TVM_GETITEMW:
5511 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5513 case TVM_GETITEMHEIGHT:
5514 return TREEVIEW_GetItemHeight(infoPtr);
5516 case TVM_GETITEMRECT:
5517 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5519 case TVM_GETITEMSTATE:
5520 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5522 case TVM_GETLINECOLOR:
5523 return TREEVIEW_GetLineColor(infoPtr);
5525 case TVM_GETNEXTITEM:
5526 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5528 case TVM_GETSCROLLTIME:
5529 return TREEVIEW_GetScrollTime(infoPtr);
5531 case TVM_GETTEXTCOLOR:
5532 return TREEVIEW_GetTextColor(infoPtr);
5534 case TVM_GETTOOLTIPS:
5535 return TREEVIEW_GetToolTips(infoPtr);
5537 case TVM_GETUNICODEFORMAT:
5538 return TREEVIEW_GetUnicodeFormat(infoPtr);
5540 case TVM_GETVISIBLECOUNT:
5541 return TREEVIEW_GetVisibleCount(infoPtr);
5543 case TVM_HITTEST:
5544 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5546 case TVM_INSERTITEMA:
5547 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, FALSE);
5549 case TVM_INSERTITEMW:
5550 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, TRUE);
5552 case TVM_SELECTITEM:
5553 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5555 case TVM_SETBKCOLOR:
5556 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5558 case TVM_SETIMAGELIST:
5559 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5561 case TVM_SETINDENT:
5562 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5564 case TVM_SETINSERTMARK:
5565 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5567 case TVM_SETINSERTMARKCOLOR:
5568 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5570 case TVM_SETITEMA:
5571 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5573 case TVM_SETITEMW:
5574 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5576 case TVM_SETLINECOLOR:
5577 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5579 case TVM_SETITEMHEIGHT:
5580 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5582 case TVM_SETSCROLLTIME:
5583 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5585 case TVM_SETTEXTCOLOR:
5586 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5588 case TVM_SETTOOLTIPS:
5589 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5591 case TVM_SETUNICODEFORMAT:
5592 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5594 case TVM_SORTCHILDREN:
5595 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5597 case TVM_SORTCHILDRENCB:
5598 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5600 case WM_CHAR:
5601 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5603 case WM_COMMAND:
5604 return TREEVIEW_Command(infoPtr, wParam, lParam);
5606 case WM_DESTROY:
5607 return TREEVIEW_Destroy(infoPtr);
5609 /* WM_ENABLE */
5611 case WM_ERASEBKGND:
5612 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5614 case WM_GETDLGCODE:
5615 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5617 case WM_GETFONT:
5618 return TREEVIEW_GetFont(infoPtr);
5620 case WM_HSCROLL:
5621 return TREEVIEW_HScroll(infoPtr, wParam);
5623 case WM_KEYDOWN:
5624 return TREEVIEW_KeyDown(infoPtr, wParam);
5626 case WM_KILLFOCUS:
5627 return TREEVIEW_KillFocus(infoPtr);
5629 case WM_LBUTTONDBLCLK:
5630 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5632 case WM_LBUTTONDOWN:
5633 return TREEVIEW_LButtonDown(infoPtr, lParam);
5635 /* WM_MBUTTONDOWN */
5637 case WM_MOUSELEAVE:
5638 return TREEVIEW_MouseLeave(infoPtr);
5640 case WM_MOUSEMOVE:
5641 if (infoPtr->dwStyle & TVS_TRACKSELECT)
5642 return TREEVIEW_MouseMove(infoPtr, wParam, lParam);
5643 else
5644 return 0;
5646 case WM_NCLBUTTONDOWN:
5647 if (infoPtr->hwndEdit)
5648 SetFocus(infoPtr->hwnd);
5649 goto def;
5651 case WM_NCPAINT:
5652 if (nc_paint (infoPtr, (HRGN)wParam))
5653 return 0;
5654 goto def;
5656 case WM_NOTIFY:
5657 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5659 case WM_NOTIFYFORMAT:
5660 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5662 case WM_PRINTCLIENT:
5663 case WM_PAINT:
5664 return TREEVIEW_Paint(infoPtr, wParam);
5666 case WM_RBUTTONDOWN:
5667 return TREEVIEW_RButtonDown(infoPtr, lParam);
5669 case WM_SETCURSOR:
5670 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5672 case WM_SETFOCUS:
5673 return TREEVIEW_SetFocus(infoPtr);
5675 case WM_SETFONT:
5676 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5678 case WM_SETREDRAW:
5679 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5681 case WM_SIZE:
5682 return TREEVIEW_Size(infoPtr, wParam, lParam);
5684 case WM_STYLECHANGED:
5685 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5687 /* WM_SYSCOLORCHANGE */
5689 /* WM_SYSKEYDOWN */
5691 case WM_TIMER:
5692 return TREEVIEW_HandleTimer(infoPtr, wParam);
5694 case WM_THEMECHANGED:
5695 return theme_changed (infoPtr);
5697 case WM_VSCROLL:
5698 return TREEVIEW_VScroll(infoPtr, wParam);
5700 /* WM_WININICHANGE */
5702 case WM_MOUSEWHEEL:
5703 if (wParam & (MK_SHIFT | MK_CONTROL))
5704 goto def;
5705 return TREEVIEW_MouseWheel(infoPtr, wParam);
5707 case WM_DRAWITEM:
5708 TRACE("drawItem\n");
5709 goto def;
5711 default:
5712 /* This mostly catches MFC and Delphi messages. :( */
5713 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5714 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5715 def:
5716 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5721 /* Class Registration ***************************************************/
5723 VOID
5724 TREEVIEW_Register(void)
5726 WNDCLASSW wndClass;
5728 TRACE("\n");
5730 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5731 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5732 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5733 wndClass.cbClsExtra = 0;
5734 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5736 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5737 wndClass.hbrBackground = 0;
5738 wndClass.lpszClassName = WC_TREEVIEWW;
5740 RegisterClassW(&wndClass);
5744 VOID
5745 TREEVIEW_Unregister(void)
5747 UnregisterClassW(WC_TREEVIEWW, NULL);
5751 /* Tree Verification ****************************************************/
5753 #ifdef NDEBUG
5754 static inline void
5755 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5757 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5758 TREEVIEW_ITEM *item)
5760 assert(infoPtr != NULL);
5761 assert(item != NULL);
5763 /* both NULL, or both non-null */
5764 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5766 assert(item->firstChild != item);
5767 assert(item->lastChild != item);
5769 if (item->firstChild)
5771 assert(item->firstChild->parent == item);
5772 assert(item->firstChild->prevSibling == NULL);
5775 if (item->lastChild)
5777 assert(item->lastChild->parent == item);
5778 assert(item->lastChild->nextSibling == NULL);
5781 assert(item->nextSibling != item);
5782 if (item->nextSibling)
5784 assert(item->nextSibling->parent == item->parent);
5785 assert(item->nextSibling->prevSibling == item);
5788 assert(item->prevSibling != item);
5789 if (item->prevSibling)
5791 assert(item->prevSibling->parent == item->parent);
5792 assert(item->prevSibling->nextSibling == item);
5796 static inline void
5797 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5799 assert(item != NULL);
5801 assert(item->parent != NULL);
5802 assert(item->parent != item);
5803 assert(item->iLevel == item->parent->iLevel + 1);
5805 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5807 TREEVIEW_VerifyItemCommon(infoPtr, item);
5809 TREEVIEW_VerifyChildren(infoPtr, item);
5812 static inline void
5813 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5815 TREEVIEW_ITEM *child;
5816 assert(item != NULL);
5818 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5819 TREEVIEW_VerifyItem(infoPtr, child);
5822 static inline void
5823 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5825 TREEVIEW_ITEM *root = infoPtr->root;
5827 assert(root != NULL);
5828 assert(root->iLevel == -1);
5829 assert(root->parent == NULL);
5830 assert(root->prevSibling == NULL);
5832 TREEVIEW_VerifyItemCommon(infoPtr, root);
5834 TREEVIEW_VerifyChildren(infoPtr, root);
5837 static void
5838 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5840 assert(infoPtr != NULL);
5842 TREEVIEW_VerifyRoot(infoPtr);
5844 #endif