widl: Renamed --rt option to --winrt for midl compatibility.
[wine.git] / dlls / comctl32 / treeview.c
blob812bd35da6cb7af66892edcf8406af212da84991
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: If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
27 * of size TEXT_CALLBACK_SIZE in DoSetItem.
28 * We use callbackMask to keep track of fields to be updated.
30 * TODO:
31 * missing notifications: TVN_GETINFOTIP, TVN_KEYDOWN,
32 * TVN_SETDISPINFO
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_EXPANDPARTIAL, TVIS_EX_FLAT,
37 * TVIS_EX_DISABLED
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
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 "vssym32.h"
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
69 /* internal structures */
70 typedef struct tagTREEVIEW_INFO
72 HWND hwnd;
73 HWND hwndNotify; /* Owner window to send notifications to */
74 DWORD dwStyle;
75 HTREEITEM root;
76 UINT uInternalStatus;
77 INT Timer;
78 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
79 INT cdmode; /* last custom draw setting */
80 UINT uScrollTime; /* max. time for scrolling in milliseconds */
81 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
83 UINT uItemHeight; /* item height */
84 BOOL bHeightSet;
86 LONG clientWidth; /* width of control window */
87 LONG clientHeight; /* height of control window */
89 LONG treeWidth; /* width of visible tree items */
90 LONG treeHeight; /* height of visible tree items */
92 UINT uIndent; /* indentation in pixels */
93 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
94 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
95 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
96 HTREEITEM editItem; /* item being edited with builtin edit box */
98 HTREEITEM firstVisible; /* handle to first visible item */
99 LONG maxVisibleOrder;
100 HTREEITEM dropItem; /* handle to item selected by drag cursor */
101 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
102 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
103 HIMAGELIST dragList; /* Bitmap of dragged item */
104 LONG scrollX;
105 INT wheelRemainder;
106 COLORREF clrBk;
107 COLORREF clrText;
108 COLORREF clrLine;
109 COLORREF clrInsertMark;
110 HFONT hFont;
111 HFONT hDefaultFont;
112 HFONT hBoldFont;
113 HFONT hUnderlineFont;
114 HFONT hBoldUnderlineFont;
115 HCURSOR hcurHand;
116 HWND hwndToolTip;
118 HWND hwndEdit;
119 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
120 BOOL bIgnoreEditKillFocus;
121 BOOL bLabelChanged;
123 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
124 HIMAGELIST himlNormal;
125 int normalImageHeight;
126 int normalImageWidth;
127 HIMAGELIST himlState;
128 int stateImageHeight;
129 int stateImageWidth;
130 HDPA items;
132 DWORD lastKeyPressTimestamp;
133 WPARAM charCode;
134 INT nSearchParamLength;
135 WCHAR szSearchParam[ MAX_PATH ];
136 } TREEVIEW_INFO;
138 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
140 HTREEITEM parent; /* handle to parent or 0 if at root */
141 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
142 HTREEITEM firstChild; /* handle to first child or 0 if no child */
144 UINT callbackMask;
145 UINT state;
146 UINT stateMask;
147 LPWSTR pszText;
148 int cchTextMax;
149 int iImage;
150 int iSelectedImage;
151 int iExpandedImage;
152 int cChildren;
153 LPARAM lParam;
154 int iIntegral; /* item height multiplier (1 is normal) */
155 int iLevel; /* indentation level:0=root level */
156 HTREEITEM lastChild;
157 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
158 RECT rect;
159 LONG linesOffset;
160 LONG stateOffset;
161 LONG imageOffset;
162 LONG textOffset;
163 LONG textWidth; /* horizontal text extent for pszText */
164 LONG visibleOrder; /* visible ordering, 0 is first visible item */
165 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
166 } TREEVIEW_ITEM;
168 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
169 #define KEY_DELAY 450
171 /* bitflags for infoPtr->uInternalStatus */
173 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
174 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
175 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
176 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
177 #define TV_RDRAG 0x10 /* ditto Rbutton */
178 #define TV_RDRAGGING 0x20
180 /* bitflags for infoPtr->timer */
182 #define TV_EDIT_TIMER 2
183 #define TV_EDIT_TIMER_SET 2
185 #define TEXT_CALLBACK_SIZE 260
187 #define TREEVIEW_LEFT_MARGIN 8
189 #define MINIMUM_INDENT 19
191 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
193 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
194 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
195 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
197 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
198 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
199 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
200 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
202 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
205 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
208 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
210 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
211 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
212 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
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);
217 /* Random Utilities *****************************************************/
218 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
220 /* Returns the treeview private data if hwnd is a treeview.
221 * Otherwise returns an undefined value. */
222 static inline TREEVIEW_INFO *
223 TREEVIEW_GetInfoPtr(HWND hwnd)
225 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
228 /* Don't call this. Nothing wants an item index. */
229 static inline int
230 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
232 return DPA_GetPtrIndex(infoPtr->items, handle);
235 /* Checks if item has changed and needs to be redrawn */
236 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
237 const TVITEMEXW *tvChange)
239 /* Number of children has changed */
240 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
241 return TRUE;
243 /* Image has changed and it's not a callback */
244 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
245 tiNew->iImage != I_IMAGECALLBACK)
246 return TRUE;
248 /* Selected image has changed and it's not a callback */
249 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
250 tiNew->iSelectedImage != I_IMAGECALLBACK)
251 return TRUE;
253 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
254 tiNew->iExpandedImage != I_IMAGECALLBACK)
255 return TRUE;
257 /* Text has changed and it's not a callback */
258 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
259 tiNew->pszText != LPSTR_TEXTCALLBACKW)
260 return TRUE;
262 /* Indent has changed */
263 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
264 return TRUE;
266 /* Item state has changed */
267 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
268 return TRUE;
270 return FALSE;
273 /***************************************************************************
274 * This method checks that handle is an item for this tree.
276 static BOOL
277 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
279 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
281 TRACE("invalid item %p\n", handle);
282 return FALSE;
284 else
285 return TRUE;
288 static HFONT
289 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
291 LOGFONTW font;
293 GetObjectW(hOrigFont, sizeof(font), &font);
294 font.lfWeight = FW_BOLD;
295 return CreateFontIndirectW(&font);
298 static HFONT
299 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
301 LOGFONTW font;
303 GetObjectW(hOrigFont, sizeof(font), &font);
304 font.lfUnderline = TRUE;
305 return CreateFontIndirectW(&font);
308 static HFONT
309 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont)
311 LOGFONTW font;
313 GetObjectW(hfont, sizeof(font), &font);
314 font.lfWeight = FW_BOLD;
315 font.lfUnderline = TRUE;
316 return CreateFontIndirectW(&font);
319 static inline HFONT
320 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
322 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
323 return item->state & TVIS_BOLD ? infoPtr->hBoldUnderlineFont : infoPtr->hUnderlineFont;
324 if (item->state & TVIS_BOLD)
325 return infoPtr->hBoldFont;
326 return infoPtr->hFont;
329 /* for trace/debugging purposes only */
330 static const char *
331 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
333 if (item == NULL) return "<null item>";
334 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
335 if (item->pszText == NULL) return "<null>";
336 return debugstr_w(item->pszText);
339 /* An item is not a child of itself. */
340 static BOOL
341 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
345 child = child->parent;
346 if (child == parent) return TRUE;
347 } while (child != NULL);
349 return FALSE;
353 /* Tree Traversal *******************************************************/
355 /***************************************************************************
356 * This method returns the last expanded sibling or child child item
357 * of a tree node
359 static TREEVIEW_ITEM *
360 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
362 if (!item) return NULL;
364 while (item->lastChild)
366 if (item->state & TVIS_EXPANDED)
367 item = item->lastChild;
368 else
369 break;
372 if (item == infoPtr->root)
373 return NULL;
375 return item;
378 /***************************************************************************
379 * This method returns the previous non-hidden item in the list not
380 * considering the tree hierarchy.
382 static TREEVIEW_ITEM *
383 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
385 if (tvItem->prevSibling)
387 /* This item has a prevSibling, get the last item in the sibling's tree. */
388 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
390 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
391 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
392 else
393 return upItem;
395 else
397 /* this item does not have a prevSibling, get the parent */
398 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
403 /***************************************************************************
404 * This method returns the next physical item in the treeview not
405 * considering the tree hierarchy.
407 static TREEVIEW_ITEM *
408 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
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 *item,
448 LONG count)
450 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
451 TREEVIEW_ITEM *previousItem;
453 assert(item != 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 item;
469 previousItem = item;
470 item = next_item(infoPtr, item);
472 } while (--count && item != NULL);
475 return item ? item : 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 inline BOOL
502 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, UINT code, NMHDR *hdr)
504 TRACE("code=%d, hdr=%p\n", code, hdr);
506 hdr->hwndFrom = infoPtr->hwnd;
507 hdr->idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
508 hdr->code = get_notifycode(infoPtr, code);
510 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, hdr->idFrom, (LPARAM)hdr);
513 static BOOL
514 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
516 NMHDR hdr;
517 return TREEVIEW_SendRealNotify(infoPtr, code, &hdr);
520 static VOID
521 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
523 tvItem->mask = mask;
524 tvItem->hItem = item;
525 tvItem->state = item->state;
526 tvItem->stateMask = 0;
527 tvItem->iImage = item->iImage;
528 tvItem->iSelectedImage = item->iSelectedImage;
529 tvItem->cChildren = item->cChildren;
530 tvItem->lParam = item->lParam;
532 if(mask & TVIF_TEXT)
534 if (!infoPtr->bNtfUnicode)
536 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
537 tvItem->pszText = Alloc (tvItem->cchTextMax);
538 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
540 else
542 tvItem->cchTextMax = item->cchTextMax;
543 tvItem->pszText = item->pszText;
546 else
548 tvItem->cchTextMax = 0;
549 tvItem->pszText = NULL;
553 static BOOL
554 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
555 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
557 NMTREEVIEWW nmhdr;
558 BOOL ret;
560 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
561 code, action, oldItem, newItem);
563 memset(&nmhdr, 0, sizeof(NMTREEVIEWW));
564 nmhdr.action = action;
566 if (oldItem)
567 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
569 if (newItem)
570 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
572 nmhdr.ptDrag.x = 0;
573 nmhdr.ptDrag.y = 0;
575 ret = TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
576 if (!infoPtr->bNtfUnicode)
578 Free(nmhdr.itemOld.pszText);
579 Free(nmhdr.itemNew.pszText);
581 return ret;
584 static BOOL
585 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
586 HTREEITEM dragItem, POINT pt)
588 NMTREEVIEWW nmhdr;
590 TRACE("code:%d dragitem:%p\n", code, dragItem);
592 nmhdr.action = 0;
593 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
594 nmhdr.itemNew.hItem = dragItem;
595 nmhdr.itemNew.state = dragItem->state;
596 nmhdr.itemNew.lParam = dragItem->lParam;
598 nmhdr.ptDrag.x = pt.x;
599 nmhdr.ptDrag.y = pt.y;
601 return TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
605 static BOOL
606 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
607 HDC hdc, RECT rc)
609 NMTVCUSTOMDRAW nmcdhdr;
610 NMCUSTOMDRAW *nmcd;
612 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
614 nmcd = &nmcdhdr.nmcd;
615 nmcd->dwDrawStage = dwDrawStage;
616 nmcd->hdc = hdc;
617 nmcd->rc = rc;
618 nmcd->dwItemSpec = 0;
619 nmcd->uItemState = 0;
620 nmcd->lItemlParam = 0;
621 nmcdhdr.clrText = infoPtr->clrText;
622 nmcdhdr.clrTextBk = infoPtr->clrBk;
623 nmcdhdr.iLevel = 0;
625 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr.nmcd.hdr);
628 /* FIXME: need to find out when the flags in uItemState need to be set */
630 static BOOL
631 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
632 TREEVIEW_ITEM *item, UINT uItemDrawState,
633 NMTVCUSTOMDRAW *nmcdhdr)
635 NMCUSTOMDRAW *nmcd;
636 DWORD dwDrawStage;
637 DWORD_PTR dwItemSpec;
638 UINT uItemState;
640 dwDrawStage = CDDS_ITEM | uItemDrawState;
641 dwItemSpec = (DWORD_PTR)item;
642 uItemState = 0;
643 if (item->state & TVIS_SELECTED)
644 uItemState |= CDIS_SELECTED;
645 if (item == infoPtr->selectedItem)
646 uItemState |= CDIS_FOCUS;
647 if (item == infoPtr->hotItem)
648 uItemState |= CDIS_HOT;
650 nmcd = &nmcdhdr->nmcd;
651 nmcd->dwDrawStage = dwDrawStage;
652 nmcd->hdc = hdc;
653 nmcd->rc = item->rect;
654 nmcd->dwItemSpec = dwItemSpec;
655 nmcd->uItemState = uItemState;
656 nmcd->lItemlParam = item->lParam;
657 nmcdhdr->iLevel = item->iLevel;
659 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
660 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
661 nmcd->uItemState, nmcd->lItemlParam);
663 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr->nmcd.hdr);
666 static BOOL
667 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
669 NMTVDISPINFOW tvdi;
670 BOOL ret;
672 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
673 &tvdi.item, editItem);
675 ret = TREEVIEW_SendRealNotify(infoPtr, TVN_BEGINLABELEDITW, &tvdi.hdr);
677 if (!infoPtr->bNtfUnicode)
678 Free(tvdi.item.pszText);
680 return ret;
683 static void
684 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
685 UINT mask)
687 NMTVDISPINFOEXW callback;
689 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
690 mask &= item->callbackMask;
692 if (mask == 0) return;
694 /* 'state' always contains valid value, as well as 'lParam'.
695 * All other parameters are uninitialized.
697 callback.item.pszText = item->pszText;
698 callback.item.cchTextMax = item->cchTextMax;
699 callback.item.mask = mask;
700 callback.item.hItem = item;
701 callback.item.state = item->state;
702 callback.item.lParam = item->lParam;
704 /* If text is changed we need to recalculate textWidth */
705 if (mask & TVIF_TEXT)
706 item->textWidth = 0;
708 TREEVIEW_SendRealNotify(infoPtr, TVN_GETDISPINFOW, &callback.hdr);
709 TRACE("resulting code 0x%08x\n", callback.hdr.code);
711 /* It may have changed due to a call to SetItem. */
712 mask &= item->callbackMask;
714 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
716 /* Instead of copying text into our buffer user specified his own */
717 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
718 LPWSTR newText;
719 int buflen;
720 int len = MultiByteToWideChar( CP_ACP, 0,
721 (LPSTR)callback.item.pszText, -1,
722 NULL, 0);
723 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
724 newText = ReAlloc(item->pszText, buflen);
726 TRACE("returned str %s, len=%d, buflen=%d\n",
727 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
729 if (newText)
731 item->pszText = newText;
732 MultiByteToWideChar( CP_ACP, 0,
733 (LPSTR)callback.item.pszText, -1,
734 item->pszText, buflen/sizeof(WCHAR));
735 item->cchTextMax = buflen/sizeof(WCHAR);
737 /* If ReAlloc fails we have nothing to do, but keep original text */
739 else {
740 int len = max(lstrlenW(callback.item.pszText) + 1,
741 TEXT_CALLBACK_SIZE);
742 LPWSTR newText = ReAlloc(item->pszText, len);
744 TRACE("returned wstr %s, len=%d\n",
745 debugstr_w(callback.item.pszText), len);
747 if (newText)
749 item->pszText = newText;
750 strcpyW(item->pszText, callback.item.pszText);
751 item->cchTextMax = len;
753 /* If ReAlloc fails we have nothing to do, but keep original text */
756 else if (mask & TVIF_TEXT) {
757 /* User put text into our buffer, that is ok unless A string */
758 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
759 LPWSTR newText;
760 int buflen;
761 int len = MultiByteToWideChar( CP_ACP, 0,
762 (LPSTR)callback.item.pszText, -1,
763 NULL, 0);
764 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
765 newText = Alloc(buflen);
767 TRACE("same buffer str %s, len=%d, buflen=%d\n",
768 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
770 if (newText)
772 LPWSTR oldText = item->pszText;
773 item->pszText = newText;
774 MultiByteToWideChar( CP_ACP, 0,
775 (LPSTR)callback.item.pszText, -1,
776 item->pszText, buflen/sizeof(WCHAR));
777 item->cchTextMax = buflen/sizeof(WCHAR);
778 Free(oldText);
783 if (mask & TVIF_IMAGE)
784 item->iImage = callback.item.iImage;
786 if (mask & TVIF_SELECTEDIMAGE)
787 item->iSelectedImage = callback.item.iSelectedImage;
789 if (mask & TVIF_EXPANDEDIMAGE)
790 item->iExpandedImage = callback.item.iExpandedImage;
792 if (mask & TVIF_CHILDREN)
793 item->cChildren = callback.item.cChildren;
795 if (callback.item.mask & TVIF_STATE)
797 item->state &= ~callback.item.stateMask;
798 item->state |= (callback.item.state & callback.item.stateMask);
801 /* These members are now permanently set. */
802 if (callback.item.mask & TVIF_DI_SETITEM)
803 item->callbackMask &= ~callback.item.mask;
806 /***************************************************************************
807 * This function uses cChildren field to decide whether the item has
808 * children or not.
809 * Note: if this returns TRUE, the child items may not actually exist,
810 * they could be virtual.
812 * Just use item->firstChild to check for physical children.
814 static BOOL
815 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
817 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
819 return item->cChildren > 0;
822 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
824 INT format;
826 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
828 if (nCommand != NF_REQUERY) return 0;
830 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
831 TRACE("format=%d\n", format);
833 /* Invalid format returned by NF_QUERY defaults to ANSI*/
834 if (format != NFR_ANSI && format != NFR_UNICODE)
835 format = NFR_ANSI;
837 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
839 return format;
842 /* Item Position ********************************************************/
844 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
845 static VOID
846 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
848 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
849 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
850 > TVS_LINESATROOT);
852 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
853 - infoPtr->scrollX;
854 item->stateOffset = item->linesOffset + infoPtr->uIndent;
855 item->imageOffset = item->stateOffset
856 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
857 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
860 static VOID
861 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
863 HDC hdc;
864 HFONT hOldFont=0;
865 SIZE sz;
867 /* DRAW's OM docker creates items like this */
868 if (item->pszText == NULL)
870 item->textWidth = 0;
871 return;
874 if (hDC != 0)
876 hdc = hDC;
878 else
880 hdc = GetDC(infoPtr->hwnd);
881 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
884 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
885 item->textWidth = sz.cx;
887 if (hDC == 0)
889 SelectObject(hdc, hOldFont);
890 ReleaseDC(0, hdc);
894 static VOID
895 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
897 item->rect.top = infoPtr->uItemHeight *
898 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
900 item->rect.bottom = item->rect.top
901 + infoPtr->uItemHeight * item->iIntegral - 1;
903 item->rect.left = 0;
904 item->rect.right = infoPtr->clientWidth;
907 /* We know that only items after start need their order updated. */
908 static void
909 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
911 TREEVIEW_ITEM *item;
912 int order;
914 if (!start)
916 start = infoPtr->root->firstChild;
917 order = 0;
919 else
920 order = start->visibleOrder;
922 for (item = start; item != NULL;
923 item = TREEVIEW_GetNextListItem(infoPtr, item))
925 if (!ISVISIBLE(item) && order > 0)
926 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
927 item->visibleOrder = order;
928 order += item->iIntegral;
931 infoPtr->maxVisibleOrder = order;
933 for (item = start; item != NULL;
934 item = TREEVIEW_GetNextListItem(infoPtr, item))
936 TREEVIEW_ComputeItemRect(infoPtr, item);
941 /* Update metrics of all items in selected subtree.
942 * root must be expanded
944 static VOID
945 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
947 TREEVIEW_ITEM *sibling;
948 HDC hdc;
949 HFONT hOldFont;
951 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
952 return;
954 root->state &= ~TVIS_EXPANDED;
955 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
956 root->state |= TVIS_EXPANDED;
958 hdc = GetDC(infoPtr->hwnd);
959 hOldFont = SelectObject(hdc, infoPtr->hFont);
961 for (; root != sibling;
962 root = TREEVIEW_GetNextListItem(infoPtr, root))
964 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
966 if (root->callbackMask & TVIF_TEXT)
967 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
969 if (root->textWidth == 0)
971 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
972 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
976 SelectObject(hdc, hOldFont);
977 ReleaseDC(infoPtr->hwnd, hdc);
980 /* Item Allocation **********************************************************/
982 static TREEVIEW_ITEM *
983 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
985 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
987 if (!newItem)
988 return NULL;
990 /* I_IMAGENONE would make more sense but this is neither what is
991 * documented (MSDN doesn't specify) nor what Windows actually does
992 * (it sets it to zero)... and I can so imagine an application using
993 * inc/dec to toggle the images. */
994 newItem->iImage = 0;
995 newItem->iSelectedImage = 0;
996 newItem->iExpandedImage = (WORD)I_IMAGENONE;
997 newItem->infoPtr = infoPtr;
999 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1001 Free(newItem);
1002 return NULL;
1005 return newItem;
1008 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1009 * free item->pszText. */
1010 static void
1011 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1013 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1014 if (infoPtr->selectedItem == item)
1015 infoPtr->selectedItem = NULL;
1016 if (infoPtr->hotItem == item)
1017 infoPtr->hotItem = NULL;
1018 if (infoPtr->focusedItem == item)
1019 infoPtr->focusedItem = NULL;
1020 if (infoPtr->firstVisible == item)
1021 infoPtr->firstVisible = NULL;
1022 if (infoPtr->dropItem == item)
1023 infoPtr->dropItem = NULL;
1024 if (infoPtr->insertMarkItem == item)
1025 infoPtr->insertMarkItem = NULL;
1026 Free(item);
1030 /* Item Insertion *******************************************************/
1032 /***************************************************************************
1033 * This method inserts newItem before sibling as a child of parent.
1034 * sibling can be NULL, but only if parent has no children.
1036 static void
1037 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1038 TREEVIEW_ITEM *parent)
1040 assert(parent != NULL);
1042 if (sibling != NULL)
1044 assert(sibling->parent == parent);
1046 if (sibling->prevSibling != NULL)
1047 sibling->prevSibling->nextSibling = newItem;
1049 newItem->prevSibling = sibling->prevSibling;
1050 sibling->prevSibling = newItem;
1052 else
1053 newItem->prevSibling = NULL;
1055 newItem->nextSibling = sibling;
1057 if (parent->firstChild == sibling)
1058 parent->firstChild = newItem;
1060 if (parent->lastChild == NULL)
1061 parent->lastChild = newItem;
1064 /***************************************************************************
1065 * This method inserts newItem after sibling as a child of parent.
1066 * sibling can be NULL, but only if parent has no children.
1068 static void
1069 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1070 TREEVIEW_ITEM *parent)
1072 assert(parent != NULL);
1074 if (sibling != NULL)
1076 assert(sibling->parent == parent);
1078 if (sibling->nextSibling != NULL)
1079 sibling->nextSibling->prevSibling = newItem;
1081 newItem->nextSibling = sibling->nextSibling;
1082 sibling->nextSibling = newItem;
1084 else
1085 newItem->nextSibling = NULL;
1087 newItem->prevSibling = sibling;
1089 if (parent->lastChild == sibling)
1090 parent->lastChild = newItem;
1092 if (parent->firstChild == NULL)
1093 parent->firstChild = newItem;
1096 static BOOL
1097 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1098 const TVITEMEXW *tvItem, BOOL isW)
1100 UINT callbackClear = 0;
1101 UINT callbackSet = 0;
1103 TRACE("item %p\n", item);
1104 /* Do this first in case it fails. */
1105 if (tvItem->mask & TVIF_TEXT)
1107 item->textWidth = 0; /* force width recalculation */
1108 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1110 int len;
1111 LPWSTR newText;
1112 if (isW)
1113 len = lstrlenW(tvItem->pszText) + 1;
1114 else
1115 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1117 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1119 if (newText == NULL) return FALSE;
1121 callbackClear |= TVIF_TEXT;
1123 item->pszText = newText;
1124 item->cchTextMax = len;
1125 if (isW)
1126 lstrcpynW(item->pszText, tvItem->pszText, len);
1127 else
1128 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1129 item->pszText, len);
1131 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1133 else
1135 callbackSet |= TVIF_TEXT;
1137 item->pszText = ReAlloc(item->pszText,
1138 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1139 item->cchTextMax = TEXT_CALLBACK_SIZE;
1140 TRACE("setting callback, item %p\n", item);
1144 if (tvItem->mask & TVIF_CHILDREN)
1146 item->cChildren = tvItem->cChildren;
1148 if (item->cChildren == I_CHILDRENCALLBACK)
1149 callbackSet |= TVIF_CHILDREN;
1150 else
1151 callbackClear |= TVIF_CHILDREN;
1154 if (tvItem->mask & TVIF_IMAGE)
1156 item->iImage = tvItem->iImage;
1158 if (item->iImage == I_IMAGECALLBACK)
1159 callbackSet |= TVIF_IMAGE;
1160 else
1161 callbackClear |= TVIF_IMAGE;
1164 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1166 item->iSelectedImage = tvItem->iSelectedImage;
1168 if (item->iSelectedImage == I_IMAGECALLBACK)
1169 callbackSet |= TVIF_SELECTEDIMAGE;
1170 else
1171 callbackClear |= TVIF_SELECTEDIMAGE;
1174 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1176 item->iExpandedImage = tvItem->iExpandedImage;
1178 if (item->iExpandedImage == I_IMAGECALLBACK)
1179 callbackSet |= TVIF_EXPANDEDIMAGE;
1180 else
1181 callbackClear |= TVIF_EXPANDEDIMAGE;
1184 if (tvItem->mask & TVIF_PARAM)
1185 item->lParam = tvItem->lParam;
1187 /* If the application sets TVIF_INTEGRAL without
1188 * supplying a TVITEMEX structure, it's toast. */
1189 if (tvItem->mask & TVIF_INTEGRAL)
1190 item->iIntegral = tvItem->iIntegral;
1192 if (tvItem->mask & TVIF_STATE)
1194 TRACE("prevstate,state,mask:%x,%x,%x\n", item->state, tvItem->state,
1195 tvItem->stateMask);
1196 item->state &= ~tvItem->stateMask;
1197 item->state |= (tvItem->state & tvItem->stateMask);
1200 if (tvItem->mask & TVIF_STATEEX)
1202 FIXME("New extended state: %x\n", tvItem->uStateEx);
1205 item->callbackMask |= callbackSet;
1206 item->callbackMask &= ~callbackClear;
1208 return TRUE;
1211 /* Note that the new item is pre-zeroed. */
1212 static LRESULT
1213 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1215 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1216 HTREEITEM insertAfter;
1217 TREEVIEW_ITEM *newItem, *parentItem;
1218 BOOL bTextUpdated = FALSE;
1220 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1222 parentItem = infoPtr->root;
1224 else
1226 parentItem = ptdi->hParent;
1228 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1230 WARN("invalid parent %p\n", parentItem);
1231 return 0;
1235 insertAfter = ptdi->hInsertAfter;
1237 /* Validate this now for convenience. */
1238 switch ((DWORD_PTR)insertAfter)
1240 case (DWORD_PTR)TVI_FIRST:
1241 case (DWORD_PTR)TVI_LAST:
1242 case (DWORD_PTR)TVI_SORT:
1243 break;
1245 default:
1246 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1247 insertAfter->parent != parentItem)
1249 WARN("invalid insert after %p\n", insertAfter);
1250 insertAfter = TVI_LAST;
1254 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1255 (tvItem->mask & TVIF_TEXT)
1256 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1257 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1258 : "<no label>");
1260 newItem = TREEVIEW_AllocateItem(infoPtr);
1261 if (newItem == NULL)
1262 return 0;
1264 newItem->parent = parentItem;
1265 newItem->iIntegral = 1;
1266 newItem->visibleOrder = -1;
1268 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1269 return 0;
1271 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1273 infoPtr->uNumItems++;
1275 switch ((DWORD_PTR)insertAfter)
1277 case (DWORD_PTR)TVI_FIRST:
1279 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1280 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1281 if (infoPtr->firstVisible == originalFirst)
1282 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1284 break;
1286 case (DWORD_PTR)TVI_LAST:
1287 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1288 break;
1290 /* hInsertAfter names a specific item we want to insert after */
1291 default:
1292 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1293 break;
1295 case (DWORD_PTR)TVI_SORT:
1297 TREEVIEW_ITEM *aChild;
1298 TREEVIEW_ITEM *previousChild = NULL;
1299 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1300 BOOL bItemInserted = FALSE;
1302 aChild = parentItem->firstChild;
1304 bTextUpdated = TRUE;
1305 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1307 /* Iterate the parent children to see where we fit in */
1308 while (aChild != NULL)
1310 INT comp;
1312 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1313 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1315 if (comp < 0) /* we are smaller than the current one */
1317 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1318 if (infoPtr->firstVisible == originalFirst &&
1319 aChild == originalFirst)
1320 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1321 bItemInserted = TRUE;
1322 break;
1324 else if (comp > 0) /* we are bigger than the current one */
1326 previousChild = aChild;
1328 /* This will help us to exit if there is no more sibling */
1329 aChild = (aChild->nextSibling == 0)
1330 ? NULL
1331 : aChild->nextSibling;
1333 /* Look at the next item */
1334 continue;
1336 else if (comp == 0)
1339 * An item with this name is already existing, therefore,
1340 * we add after the one we found
1342 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1343 bItemInserted = TRUE;
1344 break;
1349 * we reach the end of the child list and the item has not
1350 * yet been inserted, therefore, insert it after the last child.
1352 if ((!bItemInserted) && (aChild == NULL))
1353 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1355 break;
1360 TRACE("new item %p; parent %p, mask %x\n", newItem,
1361 newItem->parent, tvItem->mask);
1363 newItem->iLevel = newItem->parent->iLevel + 1;
1365 if (newItem->parent->cChildren == 0)
1366 newItem->parent->cChildren = 1;
1368 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1370 if (STATEIMAGEINDEX(newItem->state) == 0)
1371 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1374 if (infoPtr->firstVisible == NULL)
1375 infoPtr->firstVisible = newItem;
1377 TREEVIEW_VerifyTree(infoPtr);
1379 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1381 if (parentItem == infoPtr->root ||
1382 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1384 TREEVIEW_ITEM *item;
1385 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1387 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1388 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1390 if (!bTextUpdated)
1391 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1393 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1394 TREEVIEW_UpdateScrollBars(infoPtr);
1396 * if the item was inserted in a visible part of the tree,
1397 * invalidate it, as well as those after it
1399 for (item = newItem;
1400 item != NULL;
1401 item = TREEVIEW_GetNextListItem(infoPtr, item))
1402 TREEVIEW_Invalidate(infoPtr, item);
1404 else
1406 /* refresh treeview if newItem is the first item inserted under parentItem */
1407 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1409 /* parent got '+' - update it */
1410 TREEVIEW_Invalidate(infoPtr, parentItem);
1414 return (LRESULT)newItem;
1417 /* Item Deletion ************************************************************/
1418 static void
1419 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1421 static void
1422 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1424 TREEVIEW_ITEM *kill = parentItem->firstChild;
1426 while (kill != NULL)
1428 TREEVIEW_ITEM *next = kill->nextSibling;
1430 TREEVIEW_RemoveItem(infoPtr, kill);
1432 kill = next;
1435 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1436 assert(parentItem->firstChild == NULL);
1437 assert(parentItem->lastChild == NULL);
1440 static void
1441 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1443 TREEVIEW_ITEM *parentItem = item->parent;
1445 assert(item != NULL);
1446 assert(item->parent != NULL); /* i.e. it must not be the root */
1448 if (parentItem->firstChild == item)
1449 parentItem->firstChild = item->nextSibling;
1451 if (parentItem->lastChild == item)
1452 parentItem->lastChild = item->prevSibling;
1454 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1455 && parentItem->cChildren > 0)
1456 parentItem->cChildren = 0;
1458 if (item->prevSibling)
1459 item->prevSibling->nextSibling = item->nextSibling;
1461 if (item->nextSibling)
1462 item->nextSibling->prevSibling = item->prevSibling;
1465 static void
1466 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1468 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1470 if (item->firstChild)
1471 TREEVIEW_RemoveAllChildren(infoPtr, item);
1473 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1474 TVIF_HANDLE | TVIF_PARAM, item, 0);
1476 TREEVIEW_UnlinkItem(item);
1478 infoPtr->uNumItems--;
1480 if (item->pszText != LPSTR_TEXTCALLBACKW)
1481 Free(item->pszText);
1483 TREEVIEW_FreeItem(infoPtr, item);
1487 /* Empty out the tree. */
1488 static void
1489 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1491 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1493 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1496 static LRESULT
1497 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1499 TREEVIEW_ITEM *newSelection = NULL;
1500 TREEVIEW_ITEM *newFirstVisible = NULL;
1501 TREEVIEW_ITEM *parent, *prev = NULL;
1502 BOOL visible = FALSE;
1504 if (item == TVI_ROOT || !item)
1506 TRACE("TVI_ROOT\n");
1507 parent = infoPtr->root;
1508 newSelection = NULL;
1509 visible = TRUE;
1510 TREEVIEW_RemoveTree(infoPtr);
1512 else
1514 if (!TREEVIEW_ValidItem(infoPtr, item))
1515 return FALSE;
1517 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1518 parent = item->parent;
1520 if (ISVISIBLE(item))
1522 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1523 visible = TRUE;
1526 if (infoPtr->selectedItem != NULL
1527 && (item == infoPtr->selectedItem
1528 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1530 if (item->nextSibling)
1531 newSelection = item->nextSibling;
1532 else if (item->parent != infoPtr->root)
1533 newSelection = item->parent;
1534 else
1535 newSelection = item->prevSibling;
1536 TRACE("newSelection = %p\n", newSelection);
1539 if (infoPtr->firstVisible == item)
1541 if (item->nextSibling)
1542 newFirstVisible = item->nextSibling;
1543 else if (item->prevSibling)
1544 newFirstVisible = item->prevSibling;
1545 else if (item->parent != infoPtr->root)
1546 newFirstVisible = item->parent;
1547 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1549 else
1550 newFirstVisible = infoPtr->firstVisible;
1552 TREEVIEW_RemoveItem(infoPtr, item);
1555 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1556 if (!infoPtr->selectedItem && newSelection)
1558 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1559 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1562 /* Validate insertMark dropItem.
1563 * hotItem ??? - used for comparison only.
1565 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1566 infoPtr->insertMarkItem = 0;
1568 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1569 infoPtr->dropItem = 0;
1571 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1572 newFirstVisible = infoPtr->root->firstChild;
1574 TREEVIEW_VerifyTree(infoPtr);
1576 if (!infoPtr->bRedraw) return TRUE;
1578 if (visible)
1580 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1581 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1582 TREEVIEW_UpdateScrollBars(infoPtr);
1583 TREEVIEW_Invalidate(infoPtr, NULL);
1585 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1587 /* parent lost '+/-' - update it */
1588 TREEVIEW_Invalidate(infoPtr, parent);
1591 return TRUE;
1595 /* Get/Set Messages *********************************************************/
1596 static LRESULT
1597 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1599 infoPtr->bRedraw = wParam != 0;
1601 if (infoPtr->bRedraw)
1603 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1604 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1605 TREEVIEW_UpdateScrollBars(infoPtr);
1606 TREEVIEW_Invalidate(infoPtr, NULL);
1608 return 0;
1611 static LRESULT
1612 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1614 TRACE("\n");
1615 return infoPtr->uIndent;
1618 static LRESULT
1619 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1621 TRACE("\n");
1623 if (newIndent < MINIMUM_INDENT)
1624 newIndent = MINIMUM_INDENT;
1626 if (infoPtr->uIndent != newIndent)
1628 infoPtr->uIndent = newIndent;
1629 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1630 TREEVIEW_UpdateScrollBars(infoPtr);
1631 TREEVIEW_Invalidate(infoPtr, NULL);
1634 return 0;
1638 static LRESULT
1639 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1641 TRACE("\n");
1642 return (LRESULT)infoPtr->hwndToolTip;
1645 static LRESULT
1646 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1648 HWND prevToolTip;
1650 TRACE("\n");
1651 prevToolTip = infoPtr->hwndToolTip;
1652 infoPtr->hwndToolTip = hwndTT;
1654 return (LRESULT)prevToolTip;
1657 static LRESULT
1658 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1660 BOOL rc = infoPtr->bNtfUnicode;
1661 infoPtr->bNtfUnicode = fUnicode;
1662 return rc;
1665 static LRESULT
1666 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1668 return infoPtr->bNtfUnicode;
1671 static LRESULT
1672 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1674 return infoPtr->uScrollTime;
1677 static LRESULT
1678 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1680 UINT uOldScrollTime = infoPtr->uScrollTime;
1682 infoPtr->uScrollTime = min(uScrollTime, 100);
1684 return uOldScrollTime;
1688 static LRESULT
1689 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1691 TRACE("\n");
1693 switch (wParam)
1695 case TVSIL_NORMAL:
1696 return (LRESULT)infoPtr->himlNormal;
1698 case TVSIL_STATE:
1699 return (LRESULT)infoPtr->himlState;
1701 default:
1702 return 0;
1706 #define TVHEIGHT_MIN 16
1707 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1709 /* Compute the natural height for items. */
1710 static UINT
1711 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1713 TEXTMETRICW tm;
1714 HDC hdc = GetDC(0);
1715 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1716 UINT height;
1718 /* Height is the maximum of:
1719 * 16 (a hack because our fonts are tiny), and
1720 * The text height + border & margin, and
1721 * The size of the normal image list
1723 GetTextMetricsW(hdc, &tm);
1724 SelectObject(hdc, hOldFont);
1725 ReleaseDC(0, hdc);
1727 height = TVHEIGHT_MIN;
1728 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1729 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1730 if (height < infoPtr->normalImageHeight)
1731 height = infoPtr->normalImageHeight;
1733 /* Round down, unless we support odd ("non even") heights. */
1734 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1735 height &= ~1;
1737 return height;
1740 static LRESULT
1741 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1743 HIMAGELIST himlOld = 0;
1744 int oldWidth = infoPtr->normalImageWidth;
1745 int oldHeight = infoPtr->normalImageHeight;
1747 TRACE("%u,%p\n", type, himlNew);
1749 switch (type)
1751 case TVSIL_NORMAL:
1752 himlOld = infoPtr->himlNormal;
1753 infoPtr->himlNormal = himlNew;
1755 if (himlNew)
1756 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1757 &infoPtr->normalImageHeight);
1758 else
1760 infoPtr->normalImageWidth = 0;
1761 infoPtr->normalImageHeight = 0;
1764 break;
1766 case TVSIL_STATE:
1767 himlOld = infoPtr->himlState;
1768 infoPtr->himlState = himlNew;
1770 if (himlNew)
1771 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1772 &infoPtr->stateImageHeight);
1773 else
1775 infoPtr->stateImageWidth = 0;
1776 infoPtr->stateImageHeight = 0;
1779 break;
1781 default:
1782 ERR("unknown imagelist type %u\n", type);
1785 if (oldWidth != infoPtr->normalImageWidth ||
1786 oldHeight != infoPtr->normalImageHeight)
1788 BOOL bRecalcVisible = FALSE;
1790 if (oldHeight != infoPtr->normalImageHeight &&
1791 !infoPtr->bHeightSet)
1793 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1794 bRecalcVisible = TRUE;
1797 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1798 infoPtr->normalImageWidth != infoPtr->uIndent)
1800 infoPtr->uIndent = infoPtr->normalImageWidth;
1801 bRecalcVisible = TRUE;
1804 if (bRecalcVisible)
1805 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1807 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1808 TREEVIEW_UpdateScrollBars(infoPtr);
1811 TREEVIEW_Invalidate(infoPtr, NULL);
1813 return (LRESULT)himlOld;
1816 static LRESULT
1817 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1819 INT prevHeight = infoPtr->uItemHeight;
1821 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1822 if (newHeight == -1)
1824 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1825 infoPtr->bHeightSet = FALSE;
1827 else
1829 if (newHeight == 0) newHeight = 1;
1830 infoPtr->uItemHeight = newHeight;
1831 infoPtr->bHeightSet = TRUE;
1834 /* Round down, unless we support odd ("non even") heights. */
1835 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1837 infoPtr->uItemHeight &= ~1;
1838 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1841 if (infoPtr->uItemHeight != prevHeight)
1843 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1844 TREEVIEW_UpdateScrollBars(infoPtr);
1845 TREEVIEW_Invalidate(infoPtr, NULL);
1848 return prevHeight;
1851 static LRESULT
1852 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1854 TRACE("\n");
1855 return infoPtr->uItemHeight;
1859 static LRESULT
1860 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1862 TRACE("%p\n", infoPtr->hFont);
1863 return (LRESULT)infoPtr->hFont;
1867 static INT CALLBACK
1868 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1870 (void)unused;
1872 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1874 return 1;
1877 static LRESULT
1878 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1880 UINT uHeight = infoPtr->uItemHeight;
1882 TRACE("%p %i\n", hFont, bRedraw);
1884 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1886 DeleteObject(infoPtr->hBoldFont);
1887 DeleteObject(infoPtr->hUnderlineFont);
1888 DeleteObject(infoPtr->hBoldUnderlineFont);
1889 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1890 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1891 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
1893 if (!infoPtr->bHeightSet)
1894 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1896 if (uHeight != infoPtr->uItemHeight)
1897 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1899 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1901 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1902 TREEVIEW_UpdateScrollBars(infoPtr);
1904 if (bRedraw)
1905 TREEVIEW_Invalidate(infoPtr, NULL);
1907 return 0;
1911 static LRESULT
1912 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1914 TRACE("\n");
1915 return (LRESULT)infoPtr->clrLine;
1918 static LRESULT
1919 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1921 COLORREF prevColor = infoPtr->clrLine;
1923 TRACE("\n");
1924 infoPtr->clrLine = color;
1925 return (LRESULT)prevColor;
1929 static LRESULT
1930 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1932 TRACE("\n");
1933 return (LRESULT)infoPtr->clrText;
1936 static LRESULT
1937 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1939 COLORREF prevColor = infoPtr->clrText;
1941 TRACE("\n");
1942 infoPtr->clrText = color;
1944 if (infoPtr->clrText != prevColor)
1945 TREEVIEW_Invalidate(infoPtr, NULL);
1947 return (LRESULT)prevColor;
1951 static LRESULT
1952 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1954 TRACE("\n");
1955 return (LRESULT)infoPtr->clrBk;
1958 static LRESULT
1959 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1961 COLORREF prevColor = infoPtr->clrBk;
1963 TRACE("\n");
1964 infoPtr->clrBk = newColor;
1966 if (newColor != prevColor)
1967 TREEVIEW_Invalidate(infoPtr, NULL);
1969 return (LRESULT)prevColor;
1973 static LRESULT
1974 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1976 TRACE("\n");
1977 return (LRESULT)infoPtr->clrInsertMark;
1980 static LRESULT
1981 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1983 COLORREF prevColor = infoPtr->clrInsertMark;
1985 TRACE("%x\n", color);
1986 infoPtr->clrInsertMark = color;
1988 return (LRESULT)prevColor;
1992 static LRESULT
1993 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1995 TRACE("%d %p\n", wParam, item);
1997 if (!TREEVIEW_ValidItem(infoPtr, item))
1998 return 0;
2000 infoPtr->insertBeforeorAfter = wParam;
2001 infoPtr->insertMarkItem = item;
2003 TREEVIEW_Invalidate(infoPtr, NULL);
2005 return 1;
2009 /************************************************************************
2010 * Some serious braindamage here. lParam is a pointer to both the
2011 * input HTREEITEM and the output RECT.
2013 static LRESULT
2014 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2016 TREEVIEW_ITEM *item;
2017 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2019 TRACE("\n");
2021 if (pItem == NULL)
2022 return FALSE;
2024 item = *pItem;
2025 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2026 return FALSE;
2029 * If wParam is TRUE return the text size otherwise return
2030 * the whole item size
2032 if (fTextRect)
2034 /* Windows does not send TVN_GETDISPINFO here. */
2036 lpRect->top = item->rect.top;
2037 lpRect->bottom = item->rect.bottom;
2039 lpRect->left = item->textOffset;
2040 if (!item->textWidth)
2041 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2043 lpRect->right = item->textOffset + item->textWidth + 4;
2045 else
2047 *lpRect = item->rect;
2050 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2052 return TRUE;
2055 static inline LRESULT
2056 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2058 /* Surprise! This does not take integral height into account. */
2059 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2060 return infoPtr->clientHeight / infoPtr->uItemHeight;
2064 static LRESULT
2065 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2067 TREEVIEW_ITEM *item = tvItem->hItem;
2069 if (!TREEVIEW_ValidItem(infoPtr, item))
2071 if (!item) return FALSE;
2073 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2074 infoPtr = item->infoPtr;
2075 if (!TREEVIEW_ValidItem(infoPtr, item)) return FALSE;
2078 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2080 if (tvItem->mask & TVIF_CHILDREN)
2082 if (item->cChildren==I_CHILDRENCALLBACK)
2083 FIXME("I_CHILDRENCALLBACK not supported\n");
2084 tvItem->cChildren = item->cChildren;
2087 if (tvItem->mask & TVIF_HANDLE)
2088 tvItem->hItem = item;
2090 if (tvItem->mask & TVIF_IMAGE)
2091 tvItem->iImage = item->iImage;
2093 if (tvItem->mask & TVIF_INTEGRAL)
2094 tvItem->iIntegral = item->iIntegral;
2096 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2097 tvItem->lParam = item->lParam;
2099 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2100 tvItem->iSelectedImage = item->iSelectedImage;
2102 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2103 tvItem->iExpandedImage = item->iExpandedImage;
2105 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2106 tvItem->state = item->state;
2108 if (tvItem->mask & TVIF_TEXT)
2110 if (item->pszText == NULL)
2112 if (tvItem->cchTextMax > 0)
2113 tvItem->pszText[0] = '\0';
2115 else if (isW)
2117 if (item->pszText == LPSTR_TEXTCALLBACKW)
2119 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2120 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2122 else
2124 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2127 else
2129 if (item->pszText == LPSTR_TEXTCALLBACKW)
2131 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2132 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2134 else
2136 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2137 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2142 if (tvItem->mask & TVIF_STATEEX)
2144 FIXME("Extended item state not supported, returning 0.\n");
2145 tvItem->uStateEx = 0;
2148 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2149 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2151 return TRUE;
2154 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2155 * which is wrong. */
2156 static LRESULT
2157 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2159 TREEVIEW_ITEM *item;
2160 TREEVIEW_ITEM originalItem;
2162 item = tvItem->hItem;
2164 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2165 tvItem->mask);
2167 if (!TREEVIEW_ValidItem(infoPtr, item))
2168 return FALSE;
2170 /* store the original item values */
2171 originalItem = *item;
2173 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2174 return FALSE;
2176 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2177 if ((tvItem->mask & TVIF_TEXT
2178 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2179 && ISVISIBLE(item))
2181 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2182 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2185 if (tvItem->mask != 0 && ISVISIBLE(item))
2187 /* The refresh updates everything, but we can't wait until then. */
2188 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2190 /* if any of the item's values changed and it's not a callback, redraw the item */
2191 if (item_changed(&originalItem, item, tvItem))
2193 if (tvItem->mask & TVIF_INTEGRAL)
2195 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2196 TREEVIEW_UpdateScrollBars(infoPtr);
2198 TREEVIEW_Invalidate(infoPtr, NULL);
2200 else
2202 TREEVIEW_UpdateScrollBars(infoPtr);
2203 TREEVIEW_Invalidate(infoPtr, item);
2208 return TRUE;
2211 static LRESULT
2212 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2214 TRACE("\n");
2216 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2217 return 0;
2219 return (item->state & mask);
2222 static LRESULT
2223 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2225 TREEVIEW_ITEM *retval;
2227 retval = 0;
2229 /* handle all the global data here */
2230 switch (which)
2232 case TVGN_CHILD: /* Special case: child of 0 is root */
2233 if (item)
2234 break;
2235 /* fall through */
2236 case TVGN_ROOT:
2237 retval = infoPtr->root->firstChild;
2238 break;
2240 case TVGN_CARET:
2241 retval = infoPtr->selectedItem;
2242 break;
2244 case TVGN_FIRSTVISIBLE:
2245 retval = infoPtr->firstVisible;
2246 break;
2248 case TVGN_DROPHILITE:
2249 retval = infoPtr->dropItem;
2250 break;
2252 case TVGN_LASTVISIBLE:
2253 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2254 break;
2257 if (retval)
2259 TRACE("flags:%x, returns %p\n", which, retval);
2260 return (LRESULT)retval;
2263 if (item == TVI_ROOT) item = infoPtr->root;
2265 if (!TREEVIEW_ValidItem(infoPtr, item))
2266 return FALSE;
2268 switch (which)
2270 case TVGN_NEXT:
2271 retval = item->nextSibling;
2272 break;
2273 case TVGN_PREVIOUS:
2274 retval = item->prevSibling;
2275 break;
2276 case TVGN_PARENT:
2277 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2278 break;
2279 case TVGN_CHILD:
2280 retval = item->firstChild;
2281 break;
2282 case TVGN_NEXTVISIBLE:
2283 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2284 break;
2285 case TVGN_PREVIOUSVISIBLE:
2286 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2287 break;
2288 default:
2289 TRACE("Unknown msg %x,item %p\n", which, item);
2290 break;
2293 TRACE("flags:%x, item %p;returns %p\n", which, item, retval);
2294 return (LRESULT)retval;
2298 static LRESULT
2299 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2301 TRACE(" %d\n", infoPtr->uNumItems);
2302 return (LRESULT)infoPtr->uNumItems;
2305 static VOID
2306 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2308 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2310 static const unsigned int state_table[] = { 0, 2, 1 };
2312 unsigned int state;
2314 state = STATEIMAGEINDEX(item->state);
2315 TRACE("state:%x\n", state);
2316 item->state &= ~TVIS_STATEIMAGEMASK;
2318 if (state < 3)
2319 state = state_table[state];
2321 item->state |= INDEXTOSTATEIMAGEMASK(state);
2323 TRACE("state:%x\n", state);
2324 TREEVIEW_Invalidate(infoPtr, item);
2329 /* Painting *************************************************************/
2331 /* Draw the lines and expand button for an item. Also draws one section
2332 * of the line from item's parent to item's parent's next sibling. */
2333 static void
2334 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2336 LONG centerx, centery;
2337 BOOL lar = ((infoPtr->dwStyle
2338 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2339 > TVS_LINESATROOT);
2340 HBRUSH hbr, hbrOld;
2341 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2343 if (!lar && item->iLevel == 0)
2344 return;
2346 hbr = CreateSolidBrush(clrBk);
2347 hbrOld = SelectObject(hdc, hbr);
2349 centerx = (item->linesOffset + item->stateOffset) / 2;
2350 centery = (item->rect.top + item->rect.bottom) / 2;
2352 if (infoPtr->dwStyle & TVS_HASLINES)
2354 HPEN hOldPen, hNewPen;
2355 HTREEITEM parent;
2356 LOGBRUSH lb;
2358 /* Get a dotted grey pen */
2359 lb.lbStyle = BS_SOLID;
2360 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2361 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2362 hOldPen = SelectObject(hdc, hNewPen);
2364 /* Make sure the center is on a dot (using +2 instead
2365 * of +1 gives us pixel-by-pixel compat with native) */
2366 centery = (centery + 2) & ~1;
2368 MoveToEx(hdc, item->stateOffset, centery, NULL);
2369 LineTo(hdc, centerx - 1, centery);
2371 if (item->prevSibling || item->parent != infoPtr->root)
2373 MoveToEx(hdc, centerx, item->rect.top, NULL);
2374 LineTo(hdc, centerx, centery);
2377 if (item->nextSibling)
2379 MoveToEx(hdc, centerx, centery, NULL);
2380 LineTo(hdc, centerx, item->rect.bottom + 1);
2383 /* Draw the line from our parent to its next sibling. */
2384 parent = item->parent;
2385 while (parent != infoPtr->root)
2387 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2389 if (parent->nextSibling
2390 /* skip top-levels unless TVS_LINESATROOT */
2391 && parent->stateOffset > parent->linesOffset)
2393 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2394 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2397 parent = parent->parent;
2400 SelectObject(hdc, hOldPen);
2401 DeleteObject(hNewPen);
2405 * Display the (+/-) signs
2408 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2410 if (item->cChildren)
2412 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2413 if (theme)
2415 RECT glyphRect = item->rect;
2416 glyphRect.left = item->linesOffset;
2417 glyphRect.right = item->stateOffset;
2418 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2419 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2420 &glyphRect, NULL);
2422 else
2424 LONG height = item->rect.bottom - item->rect.top;
2425 LONG width = item->stateOffset - item->linesOffset;
2426 LONG rectsize = min(height, width) / 4;
2427 /* plussize = ceil(rectsize * 3/4) */
2428 LONG plussize = (rectsize + 1) * 3 / 4;
2430 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2431 HPEN old_pen = SelectObject(hdc, new_pen);
2433 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2434 centerx + rectsize + 2, centery + rectsize + 2);
2436 SelectObject(hdc, old_pen);
2437 DeleteObject(new_pen);
2439 /* draw +/- signs with current text color */
2440 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2441 old_pen = SelectObject(hdc, new_pen);
2443 if (height < 18 || width < 18)
2445 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2446 LineTo(hdc, centerx + plussize, centery);
2448 if (!(item->state & TVIS_EXPANDED) ||
2449 (item->state & TVIS_EXPANDPARTIAL))
2451 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2452 LineTo(hdc, centerx, centery + plussize);
2455 else
2457 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2458 centerx + plussize, centery + 2);
2460 if (!(item->state & TVIS_EXPANDED) ||
2461 (item->state & TVIS_EXPANDPARTIAL))
2463 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2464 centerx + 2, centery + plussize);
2465 SetPixel(hdc, centerx - 1, centery, clrBk);
2466 SetPixel(hdc, centerx + 1, centery, clrBk);
2470 SelectObject(hdc, old_pen);
2471 DeleteObject(new_pen);
2475 SelectObject(hdc, hbrOld);
2476 DeleteObject(hbr);
2479 static void
2480 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2482 INT cditem;
2483 HFONT hOldFont;
2484 COLORREF oldTextColor, oldTextBkColor;
2485 int centery;
2486 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2487 NMTVCUSTOMDRAW nmcdhdr;
2489 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2491 /* - If item is drop target or it is selected and window is in focus -
2492 * use blue background (COLOR_HIGHLIGHT).
2493 * - If item is selected, window is not in focus, but it has style
2494 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2495 * - Otherwise - use background color
2497 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2498 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem || item == infoPtr->focusedItem) &&
2499 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2501 if ((item->state & TVIS_DROPHILITED) || inFocus)
2503 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2504 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2506 else
2508 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2509 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2512 else
2514 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2515 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2516 nmcdhdr.clrText = comctl32_color.clrHighlight;
2517 else
2518 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2521 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2523 /* The custom draw handler can query the text rectangle,
2524 * so get ready. */
2525 /* should already be known, set to 0 when changed */
2526 if (!item->textWidth)
2527 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2529 cditem = 0;
2531 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2533 cditem = TREEVIEW_SendCustomDrawItemNotify
2534 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2535 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2537 if (cditem & CDRF_SKIPDEFAULT)
2539 SelectObject(hdc, hOldFont);
2540 return;
2544 if (cditem & CDRF_NEWFONT)
2545 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2547 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2549 /* Set colors. Custom draw handler can change these so we do this after it. */
2550 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2551 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2553 centery = (item->rect.top + item->rect.bottom) / 2;
2556 * Display the images associated with this item
2559 INT imageIndex;
2561 /* State images are displayed to the left of the Normal image
2562 * image number is in state; zero should be `display no image'.
2564 imageIndex = STATEIMAGEINDEX(item->state);
2566 if (infoPtr->himlState && imageIndex)
2568 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2569 item->stateOffset,
2570 centery - infoPtr->stateImageHeight / 2,
2571 ILD_NORMAL);
2574 /* Now, draw the normal image; can be either selected,
2575 * non-selected or expanded image.
2578 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2580 /* The item is currently selected */
2581 imageIndex = item->iSelectedImage;
2583 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2585 /* The item is currently not selected but expanded */
2586 imageIndex = item->iExpandedImage;
2588 else
2590 /* The item is not selected and not expanded */
2591 imageIndex = item->iImage;
2594 if (infoPtr->himlNormal)
2596 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2598 style |= item->state & TVIS_OVERLAYMASK;
2600 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2601 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2602 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2603 style);
2609 * Display the text associated with this item
2612 /* Don't paint item's text if it's being edited */
2613 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2615 if (item->pszText)
2617 RECT rcText;
2618 UINT align;
2619 SIZE sz;
2621 rcText.top = item->rect.top;
2622 rcText.bottom = item->rect.bottom;
2623 rcText.left = item->textOffset;
2624 rcText.right = rcText.left + item->textWidth + 4;
2626 TRACE("drawing text %s at (%s)\n",
2627 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2629 /* Draw it */
2630 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2632 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2633 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2634 ETO_CLIPPED | ETO_OPAQUE,
2635 &rcText,
2636 item->pszText,
2637 lstrlenW(item->pszText),
2638 NULL);
2639 SetTextAlign(hdc, align);
2641 /* Draw focus box around the selected item */
2642 if ((item == infoPtr->selectedItem) && inFocus)
2644 DrawFocusRect(hdc,&rcText);
2649 /* Draw insertion mark if necessary */
2651 if (infoPtr->insertMarkItem)
2652 TRACE("item:%d,mark:%p\n",
2653 TREEVIEW_GetItemIndex(infoPtr, item),
2654 infoPtr->insertMarkItem);
2656 if (item == infoPtr->insertMarkItem)
2658 HPEN hNewPen, hOldPen;
2659 int offset;
2660 int left, right;
2662 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2663 hOldPen = SelectObject(hdc, hNewPen);
2665 if (infoPtr->insertBeforeorAfter)
2666 offset = item->rect.bottom - 1;
2667 else
2668 offset = item->rect.top + 1;
2670 left = item->textOffset - 2;
2671 right = item->textOffset + item->textWidth + 2;
2673 MoveToEx(hdc, left, offset - 3, NULL);
2674 LineTo(hdc, left, offset + 4);
2676 MoveToEx(hdc, left, offset, NULL);
2677 LineTo(hdc, right + 1, offset);
2679 MoveToEx(hdc, right, offset + 3, NULL);
2680 LineTo(hdc, right, offset - 4);
2682 SelectObject(hdc, hOldPen);
2683 DeleteObject(hNewPen);
2686 /* Restore the hdc state */
2687 SetTextColor(hdc, oldTextColor);
2688 SetBkColor(hdc, oldTextBkColor);
2689 SelectObject(hdc, hOldFont);
2691 if (cditem & CDRF_NOTIFYPOSTPAINT)
2693 cditem = TREEVIEW_SendCustomDrawItemNotify
2694 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2695 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2699 /* Computes treeHeight and treeWidth and updates the scroll bars.
2701 static void
2702 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2704 TREEVIEW_ITEM *item;
2705 HWND hwnd = infoPtr->hwnd;
2706 BOOL vert = FALSE;
2707 BOOL horz = FALSE;
2708 SCROLLINFO si;
2709 LONG scrollX = infoPtr->scrollX;
2711 infoPtr->treeWidth = 0;
2712 infoPtr->treeHeight = 0;
2714 /* We iterate through all visible items in order to get the tree height
2715 * and width */
2716 item = infoPtr->root->firstChild;
2718 while (item != NULL)
2720 if (ISVISIBLE(item))
2722 /* actually we draw text at textOffset + 2 */
2723 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2724 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2726 /* This is scroll-adjusted, but we fix this below. */
2727 infoPtr->treeHeight = item->rect.bottom;
2730 item = TREEVIEW_GetNextListItem(infoPtr, item);
2733 /* Fix the scroll adjusted treeHeight and treeWidth. */
2734 if (infoPtr->root->firstChild)
2735 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2737 infoPtr->treeWidth += infoPtr->scrollX;
2739 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2741 /* Adding one scroll bar may take up enough space that it forces us
2742 * to add the other as well. */
2743 if (infoPtr->treeHeight > infoPtr->clientHeight)
2745 vert = TRUE;
2747 if (infoPtr->treeWidth
2748 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2749 horz = TRUE;
2751 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2752 horz = TRUE;
2754 if (!vert && horz && infoPtr->treeHeight
2755 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2756 vert = TRUE;
2758 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2760 si.cbSize = sizeof(SCROLLINFO);
2761 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2762 si.nMin = 0;
2764 if (vert)
2766 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2767 if ( si.nPage && NULL != infoPtr->firstVisible)
2769 si.nPos = infoPtr->firstVisible->visibleOrder;
2770 si.nMax = infoPtr->maxVisibleOrder - 1;
2772 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2774 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2775 ShowScrollBar(hwnd, SB_VERT, TRUE);
2776 infoPtr->uInternalStatus |= TV_VSCROLL;
2778 else
2780 if (infoPtr->uInternalStatus & TV_VSCROLL)
2781 ShowScrollBar(hwnd, SB_VERT, FALSE);
2782 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2785 else
2787 if (infoPtr->uInternalStatus & TV_VSCROLL)
2788 ShowScrollBar(hwnd, SB_VERT, FALSE);
2789 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2792 if (horz)
2794 si.nPage = infoPtr->clientWidth;
2795 si.nPos = infoPtr->scrollX;
2796 si.nMax = infoPtr->treeWidth - 1;
2798 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2800 si.nPos = si.nMax - max( si.nPage-1, 0 );
2801 scrollX = si.nPos;
2804 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2805 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2806 infoPtr->uInternalStatus |= TV_HSCROLL;
2808 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2809 TREEVIEW_HScroll(infoPtr,
2810 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2812 else
2814 if (infoPtr->uInternalStatus & TV_HSCROLL)
2815 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2816 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2818 scrollX = 0;
2819 if (infoPtr->scrollX != 0)
2821 TREEVIEW_HScroll(infoPtr,
2822 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2826 if (!horz)
2827 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2830 static void
2831 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2833 HBRUSH hBrush;
2834 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2836 hBrush = CreateSolidBrush(clrBk);
2837 FillRect(hdc, rc, hBrush);
2838 DeleteObject(hBrush);
2841 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2842 static LRESULT
2843 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2845 RECT rect;
2847 TRACE("%p\n", infoPtr);
2849 GetClientRect(infoPtr->hwnd, &rect);
2850 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2852 return 1;
2855 static void
2856 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2858 HWND hwnd = infoPtr->hwnd;
2859 RECT rect = *rc;
2860 TREEVIEW_ITEM *item;
2862 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2864 TRACE("empty window\n");
2865 return;
2868 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2869 hdc, rect);
2871 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2873 ReleaseDC(hwnd, hdc);
2874 return;
2877 for (item = infoPtr->root->firstChild;
2878 item != NULL;
2879 item = TREEVIEW_GetNextListItem(infoPtr, item))
2881 if (ISVISIBLE(item))
2883 /* Avoid unneeded calculations */
2884 if (item->rect.top > rect.bottom)
2885 break;
2886 if (item->rect.bottom < rect.top)
2887 continue;
2889 TREEVIEW_DrawItem(infoPtr, hdc, item);
2893 TREEVIEW_UpdateScrollBars(infoPtr);
2895 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2896 infoPtr->cdmode =
2897 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2900 static inline void
2901 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2903 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2906 static void
2907 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2909 if (item)
2910 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2911 else
2912 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2915 static void
2916 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
2918 RECT rc;
2919 HBITMAP hbm, hbmOld;
2920 HDC hdc, hdcScreen;
2921 int nIndex;
2923 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2925 hdcScreen = GetDC(0);
2927 hdc = CreateCompatibleDC(hdcScreen);
2928 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2929 hbmOld = SelectObject(hdc, hbm);
2931 SetRect(&rc, 0, 0, 48, 16);
2932 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2934 SetRect(&rc, 18, 2, 30, 14);
2935 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2936 DFCS_BUTTONCHECK|DFCS_FLAT);
2938 SetRect(&rc, 34, 2, 46, 14);
2939 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2940 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
2942 SelectObject(hdc, hbmOld);
2943 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
2944 comctl32_color.clrWindow);
2945 TRACE("checkbox index %d\n", nIndex);
2947 DeleteObject(hbm);
2948 DeleteDC(hdc);
2949 ReleaseDC(0, hdcScreen);
2951 infoPtr->stateImageWidth = 16;
2952 infoPtr->stateImageHeight = 16;
2955 static void
2956 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2958 TREEVIEW_ITEM *child = item->firstChild;
2960 item->state &= ~TVIS_STATEIMAGEMASK;
2961 item->state |= INDEXTOSTATEIMAGEMASK(1);
2963 while (child)
2965 TREEVIEW_ITEM *next = child->nextSibling;
2966 TREEVIEW_ResetImageStateIndex(infoPtr, child);
2967 child = next;
2971 static LRESULT
2972 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2974 HDC hdc;
2975 PAINTSTRUCT ps;
2976 RECT rc;
2978 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2980 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
2982 TREEVIEW_InitCheckboxes(infoPtr);
2983 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
2985 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
2986 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
2987 TREEVIEW_UpdateScrollBars(infoPtr);
2988 TREEVIEW_Invalidate(infoPtr, NULL);
2991 if (hdc_ref)
2993 hdc = hdc_ref;
2994 GetClientRect(infoPtr->hwnd, &rc);
2995 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2997 else
2999 hdc = BeginPaint(infoPtr->hwnd, &ps);
3000 rc = ps.rcPaint;
3001 if(ps.fErase)
3002 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3005 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3006 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3008 if (!hdc_ref)
3009 EndPaint(infoPtr->hwnd, &ps);
3011 return 0;
3014 static LRESULT
3015 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
3017 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3019 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3020 return 0;
3022 if (options & PRF_ERASEBKGND)
3023 TREEVIEW_EraseBackground(infoPtr, hdc);
3025 if (options & PRF_CLIENT)
3027 RECT rc;
3028 GetClientRect(infoPtr->hwnd, &rc);
3029 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3032 return 0;
3035 /* Sorting **************************************************************/
3037 /***************************************************************************
3038 * Forward the DPA local callback to the treeview owner callback
3040 static INT WINAPI
3041 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3042 const TVSORTCB *pCallBackSort)
3044 /* Forward the call to the client-defined callback */
3045 return pCallBackSort->lpfnCompare(first->lParam,
3046 second->lParam,
3047 pCallBackSort->lParam);
3050 /***************************************************************************
3051 * Treeview native sort routine: sort on item text.
3053 static INT WINAPI
3054 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3055 const TREEVIEW_INFO *infoPtr)
3057 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3058 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3060 if(first->pszText && second->pszText)
3061 return lstrcmpiW(first->pszText, second->pszText);
3062 else if(first->pszText)
3063 return -1;
3064 else if(second->pszText)
3065 return 1;
3066 else
3067 return 0;
3070 /* Returns the number of physical children belonging to item. */
3071 static INT
3072 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3074 INT cChildren = 0;
3075 HTREEITEM hti;
3077 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3078 cChildren++;
3080 return cChildren;
3083 /* Returns a DPA containing a pointer to each physical child of item in
3084 * sibling order. If item has no children, an empty DPA is returned. */
3085 static HDPA
3086 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3088 HTREEITEM child;
3090 HDPA list = DPA_Create(8);
3091 if (list == 0) return NULL;
3093 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3095 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3097 DPA_Destroy(list);
3098 return NULL;
3102 return list;
3105 /***************************************************************************
3106 * Setup the treeview structure with regards of the sort method
3107 * and sort the children of the TV item specified in lParam
3108 * fRecurse: currently unused. Should be zero.
3109 * parent: if pSort!=NULL, should equal pSort->hParent.
3110 * otherwise, item which child items are to be sorted.
3111 * pSort: sort method info. if NULL, sort on item text.
3112 * if non-NULL, sort on item's lParam content, and let the
3113 * application decide what that means. See also TVM_SORTCHILDRENCB.
3116 static LRESULT
3117 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3118 LPTVSORTCB pSort)
3120 INT cChildren;
3121 PFNDPACOMPARE pfnCompare;
3122 LPARAM lpCompare;
3124 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3125 if (parent == TVI_ROOT || parent == NULL)
3126 parent = infoPtr->root;
3128 /* Check for a valid handle to the parent item */
3129 if (!TREEVIEW_ValidItem(infoPtr, parent))
3131 ERR("invalid item hParent=%p\n", parent);
3132 return FALSE;
3135 if (pSort)
3137 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3138 lpCompare = (LPARAM)pSort;
3140 else
3142 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3143 lpCompare = (LPARAM)infoPtr;
3146 cChildren = TREEVIEW_CountChildren(parent);
3148 /* Make sure there is something to sort */
3149 if (cChildren > 1)
3151 /* TREEVIEW_ITEM rechaining */
3152 INT count = 0;
3153 HTREEITEM item = 0;
3154 HTREEITEM nextItem = 0;
3155 HTREEITEM prevItem = 0;
3157 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3159 if (sortList == NULL)
3160 return FALSE;
3162 /* let DPA sort the list */
3163 DPA_Sort(sortList, pfnCompare, lpCompare);
3165 /* The order of DPA entries has been changed, so fixup the
3166 * nextSibling and prevSibling pointers. */
3168 item = DPA_GetPtr(sortList, count++);
3169 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3171 /* link the two current item together */
3172 item->nextSibling = nextItem;
3173 nextItem->prevSibling = item;
3175 if (prevItem == NULL)
3177 /* this is the first item, update the parent */
3178 parent->firstChild = item;
3179 item->prevSibling = NULL;
3181 else
3183 /* fix the back chaining */
3184 item->prevSibling = prevItem;
3187 /* get ready for the next one */
3188 prevItem = item;
3189 item = nextItem;
3192 /* the last item is pointed to by item and never has a sibling */
3193 item->nextSibling = NULL;
3194 parent->lastChild = item;
3196 DPA_Destroy(sortList);
3198 TREEVIEW_VerifyTree(infoPtr);
3200 if (parent->state & TVIS_EXPANDED)
3202 int visOrder = infoPtr->firstVisible->visibleOrder;
3204 if (parent == infoPtr->root)
3205 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3206 else
3207 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3209 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3211 TREEVIEW_ITEM *item;
3213 for (item = infoPtr->root->firstChild; item != NULL;
3214 item = TREEVIEW_GetNextListItem(infoPtr, item))
3216 if (item->visibleOrder == visOrder)
3217 break;
3220 if (!item) item = parent->firstChild;
3221 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3224 TREEVIEW_Invalidate(infoPtr, NULL);
3227 return TRUE;
3229 return FALSE;
3233 /***************************************************************************
3234 * Setup the treeview structure with regards of the sort method
3235 * and sort the children of the TV item specified in lParam
3237 static LRESULT
3238 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3240 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3244 /***************************************************************************
3245 * Sort the children of the TV item specified in lParam.
3247 static LRESULT
3248 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3250 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3254 /* Expansion/Collapse ***************************************************/
3256 static BOOL
3257 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3258 UINT action)
3260 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3261 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3262 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3263 0, item);
3266 static VOID
3267 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3268 UINT action)
3270 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3271 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3272 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3273 0, item);
3277 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3278 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3279 static BOOL
3280 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3281 BOOL bRemoveChildren, BOOL bUser)
3283 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3284 BOOL bSetSelection, bSetFirstVisible;
3285 RECT scrollRect;
3286 LONG scrollDist = 0;
3287 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3288 BOOL wasExpanded;
3290 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3292 if (!TREEVIEW_HasChildren(infoPtr, item))
3293 return FALSE;
3295 if (bUser)
3296 TREEVIEW_SendExpanding(infoPtr, item, action);
3298 if (item->firstChild == NULL)
3299 return FALSE;
3301 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3302 item->state &= ~TVIS_EXPANDED;
3304 if (wasExpanded && bUser)
3305 TREEVIEW_SendExpanded(infoPtr, item, action);
3307 bSetSelection = (infoPtr->selectedItem != NULL
3308 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3310 bSetFirstVisible = (infoPtr->firstVisible != NULL
3311 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3313 tmpItem = item;
3314 while (tmpItem)
3316 if (tmpItem->nextSibling)
3318 nextItem = tmpItem->nextSibling;
3319 break;
3321 tmpItem = tmpItem->parent;
3324 if (nextItem)
3325 scrollDist = nextItem->rect.top;
3327 if (bRemoveChildren)
3329 INT old_cChildren = item->cChildren;
3330 TRACE("TVE_COLLAPSERESET\n");
3331 item->state &= ~TVIS_EXPANDEDONCE;
3332 TREEVIEW_RemoveAllChildren(infoPtr, item);
3333 item->cChildren = old_cChildren;
3335 if (!wasExpanded)
3336 return FALSE;
3338 if (item->firstChild)
3340 TREEVIEW_ITEM *i, *sibling;
3342 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3344 for (i = item->firstChild; i != sibling;
3345 i = TREEVIEW_GetNextListItem(infoPtr, i))
3347 i->visibleOrder = -1;
3351 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3353 if (nextItem)
3354 scrollDist = -(scrollDist - nextItem->rect.top);
3356 if (bSetSelection)
3358 /* Don't call DoSelectItem, it sends notifications. */
3359 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3360 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3361 item->state |= TVIS_SELECTED;
3362 infoPtr->selectedItem = item;
3365 TREEVIEW_UpdateScrollBars(infoPtr);
3367 scrollRect.left = 0;
3368 scrollRect.right = infoPtr->clientWidth;
3369 scrollRect.bottom = infoPtr->clientHeight;
3371 if (nextItem)
3373 scrollRect.top = nextItem->rect.top;
3375 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3376 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3377 TREEVIEW_Invalidate(infoPtr, item);
3378 } else {
3379 scrollRect.top = item->rect.top;
3380 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3383 TREEVIEW_SetFirstVisible(infoPtr,
3384 bSetFirstVisible ? item : infoPtr->firstVisible,
3385 TRUE);
3387 return wasExpanded;
3390 static BOOL
3391 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3392 BOOL partial, BOOL user)
3394 LONG scrollDist;
3395 LONG orgNextTop = 0;
3396 RECT scrollRect;
3397 TREEVIEW_ITEM *nextItem, *tmpItem;
3398 BOOL sendsNotifications;
3400 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr, item, partial, user);
3402 if (!TREEVIEW_HasChildren(infoPtr, item))
3403 return FALSE;
3405 tmpItem = item; nextItem = NULL;
3406 while (tmpItem)
3408 if (tmpItem->nextSibling)
3410 nextItem = tmpItem->nextSibling;
3411 break;
3413 tmpItem = tmpItem->parent;
3416 if (nextItem)
3417 orgNextTop = nextItem->rect.top;
3419 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3421 sendsNotifications = user || ((item->cChildren != 0) &&
3422 !(item->state & TVIS_EXPANDEDONCE));
3423 if (sendsNotifications)
3425 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3427 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3428 return FALSE;
3431 if (!item->firstChild)
3432 return FALSE;
3434 item->state |= TVIS_EXPANDED;
3436 if (partial)
3437 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3439 if (ISVISIBLE(item))
3441 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3442 TREEVIEW_UpdateSubTree(infoPtr, item);
3443 TREEVIEW_UpdateScrollBars(infoPtr);
3445 scrollRect.left = 0;
3446 scrollRect.bottom = infoPtr->treeHeight;
3447 scrollRect.right = infoPtr->clientWidth;
3448 if (nextItem)
3450 scrollDist = nextItem->rect.top - orgNextTop;
3451 scrollRect.top = orgNextTop;
3453 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3454 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3455 TREEVIEW_Invalidate (infoPtr, item);
3456 } else {
3457 scrollRect.top = item->rect.top;
3458 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3461 /* Scroll up so that as many children as possible are visible.
3462 * This fails when expanding causes an HScroll bar to appear, but we
3463 * don't know that yet, so the last item is obscured. */
3464 if (item->firstChild != NULL)
3466 int nChildren = item->lastChild->visibleOrder
3467 - item->firstChild->visibleOrder + 1;
3469 int visible_pos = item->visibleOrder
3470 - infoPtr->firstVisible->visibleOrder;
3472 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3474 if (visible_pos > 0 && nChildren > rows_below)
3476 int scroll = nChildren - rows_below;
3478 if (scroll > visible_pos)
3479 scroll = visible_pos;
3481 if (scroll > 0)
3483 TREEVIEW_ITEM *newFirstVisible
3484 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3485 scroll);
3488 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3494 if (sendsNotifications) {
3495 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3496 item->state |= TVIS_EXPANDEDONCE;
3499 return TRUE;
3502 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3503 to mouse messages and TVM_SELECTITEM.
3505 selection - previously selected item, used to collapse a part of a tree
3506 item - new selected item
3508 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3509 HTREEITEM selection, HTREEITEM item)
3511 TREEVIEW_ITEM *prev, *curr;
3513 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3515 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3518 * Close the previous item and its ancestors as long as they are not
3519 * ancestors of the current item
3521 for (prev = selection; prev && TREEVIEW_ValidItem(infoPtr, prev); prev = prev->parent)
3523 for (curr = item; curr && TREEVIEW_ValidItem(infoPtr, curr); curr = curr->parent)
3525 if (curr == prev)
3526 goto finish;
3528 TREEVIEW_Collapse(infoPtr, prev, FALSE, TRUE);
3531 finish:
3533 * Expand the current item
3535 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3538 static BOOL
3539 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3541 TRACE("item=%p, user=%d\n", item, user);
3543 if (item->state & TVIS_EXPANDED)
3544 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3545 else
3546 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3549 static VOID
3550 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3552 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3554 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3556 if (TREEVIEW_HasChildren(infoPtr, item))
3557 TREEVIEW_ExpandAll(infoPtr, item);
3561 /* Note:If the specified item is the child of a collapsed parent item,
3562 the parent's list of child items is (recursively) expanded to reveal the
3563 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3564 know if it also applies here.
3567 static LRESULT
3568 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3570 if (!TREEVIEW_ValidItem(infoPtr, item))
3571 return 0;
3573 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3574 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3575 flag, item->state);
3577 switch (flag & TVE_TOGGLE)
3579 case TVE_COLLAPSE:
3580 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3581 FALSE);
3583 case TVE_EXPAND:
3584 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3585 FALSE);
3587 case TVE_TOGGLE:
3588 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3590 default:
3591 return 0;
3595 /* Hit-Testing **********************************************************/
3597 static TREEVIEW_ITEM *
3598 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3600 TREEVIEW_ITEM *item;
3601 LONG row;
3603 if (!infoPtr->firstVisible)
3604 return NULL;
3606 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3608 for (item = infoPtr->firstVisible; item != NULL;
3609 item = TREEVIEW_GetNextListItem(infoPtr, item))
3611 if (row >= item->visibleOrder
3612 && row < item->visibleOrder + item->iIntegral)
3613 break;
3616 return item;
3619 static LRESULT
3620 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3622 TREEVIEW_ITEM *item;
3623 RECT rect;
3624 UINT status;
3625 LONG x, y;
3627 lpht->hItem = 0;
3628 GetClientRect(infoPtr->hwnd, &rect);
3629 status = 0;
3630 x = lpht->pt.x;
3631 y = lpht->pt.y;
3633 if (x < rect.left)
3635 status |= TVHT_TOLEFT;
3637 else if (x > rect.right)
3639 status |= TVHT_TORIGHT;
3642 if (y < rect.top)
3644 status |= TVHT_ABOVE;
3646 else if (y > rect.bottom)
3648 status |= TVHT_BELOW;
3651 if (status)
3653 lpht->flags = status;
3654 return 0;
3657 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3658 if (!item)
3660 lpht->flags = TVHT_NOWHERE;
3661 return 0;
3664 if (x >= item->textOffset + item->textWidth)
3666 lpht->flags = TVHT_ONITEMRIGHT;
3668 else if (x >= item->textOffset)
3670 lpht->flags = TVHT_ONITEMLABEL;
3672 else if (x >= item->imageOffset)
3674 lpht->flags = TVHT_ONITEMICON;
3676 else if (x >= item->stateOffset)
3678 lpht->flags = TVHT_ONITEMSTATEICON;
3680 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3682 lpht->flags = TVHT_ONITEMBUTTON;
3684 else
3686 lpht->flags = TVHT_ONITEMINDENT;
3689 lpht->hItem = item;
3690 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3692 return (LRESULT)item;
3695 /* Item Label Editing ***************************************************/
3697 static LRESULT
3698 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3700 return (LRESULT)infoPtr->hwndEdit;
3703 static LRESULT CALLBACK
3704 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3706 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3707 BOOL bCancel = FALSE;
3708 LRESULT rc;
3710 switch (uMsg)
3712 case WM_PAINT:
3713 TRACE("WM_PAINT start\n");
3714 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3715 lParam);
3716 TRACE("WM_PAINT done\n");
3717 return rc;
3719 case WM_KILLFOCUS:
3720 if (infoPtr->bIgnoreEditKillFocus)
3721 return TRUE;
3722 break;
3724 case WM_DESTROY:
3726 WNDPROC editProc = infoPtr->wpEditOrig;
3727 infoPtr->wpEditOrig = 0;
3728 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3729 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3732 case WM_GETDLGCODE:
3733 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3735 case WM_KEYDOWN:
3736 if (wParam == VK_ESCAPE)
3738 bCancel = TRUE;
3739 break;
3741 else if (wParam == VK_RETURN)
3743 break;
3746 /* fall through */
3747 default:
3748 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3751 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3752 /* eg. Using a messagebox */
3754 infoPtr->bIgnoreEditKillFocus = TRUE;
3755 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3756 infoPtr->bIgnoreEditKillFocus = FALSE;
3758 return 0;
3762 /* should handle edit control messages here */
3764 static LRESULT
3765 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3767 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3769 switch (HIWORD(wParam))
3771 case EN_UPDATE:
3774 * Adjust the edit window size
3776 WCHAR buffer[1024];
3777 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3778 HDC hdc = GetDC(infoPtr->hwndEdit);
3779 SIZE sz;
3780 HFONT hFont, hOldFont = 0;
3782 TRACE("edit=%p\n", infoPtr->hwndEdit);
3784 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3786 infoPtr->bLabelChanged = TRUE;
3788 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3790 /* Select font to get the right dimension of the string */
3791 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3793 if (hFont != 0)
3795 hOldFont = SelectObject(hdc, hFont);
3798 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3800 TEXTMETRICW textMetric;
3802 /* Add Extra spacing for the next character */
3803 GetTextMetricsW(hdc, &textMetric);
3804 sz.cx += (textMetric.tmMaxCharWidth * 2);
3806 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3807 sz.cx = min(sz.cx,
3808 infoPtr->clientWidth - editItem->textOffset + 2);
3810 SetWindowPos(infoPtr->hwndEdit,
3811 HWND_TOP,
3814 sz.cx,
3815 editItem->rect.bottom - editItem->rect.top + 3,
3816 SWP_NOMOVE | SWP_DRAWFRAME);
3819 if (hFont != 0)
3821 SelectObject(hdc, hOldFont);
3824 ReleaseDC(infoPtr->hwnd, hdc);
3825 break;
3827 case EN_KILLFOCUS:
3828 /* apparently we should respect passed handle value */
3829 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3831 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3832 break;
3834 default:
3835 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3838 return 0;
3841 static HWND
3842 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3844 HWND hwnd = infoPtr->hwnd;
3845 HWND hwndEdit;
3846 SIZE sz;
3847 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3848 HDC hdc;
3849 HFONT hOldFont=0;
3850 TEXTMETRICW textMetric;
3852 TRACE("%p %p\n", hwnd, hItem);
3853 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3854 return NULL;
3856 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3857 return NULL;
3859 if (infoPtr->hwndEdit)
3860 return infoPtr->hwndEdit;
3862 infoPtr->bLabelChanged = FALSE;
3864 /* make edit item visible */
3865 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3867 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3869 hdc = GetDC(hwnd);
3870 /* Select the font to get appropriate metric dimensions */
3871 if (infoPtr->hFont != 0)
3873 hOldFont = SelectObject(hdc, infoPtr->hFont);
3876 /* Get string length in pixels */
3877 if (hItem->pszText)
3878 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3879 &sz);
3880 else
3881 GetTextExtentPoint32A(hdc, "", 0, &sz);
3883 /* Add Extra spacing for the next character */
3884 GetTextMetricsW(hdc, &textMetric);
3885 sz.cx += (textMetric.tmMaxCharWidth * 2);
3887 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3888 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3890 if (infoPtr->hFont != 0)
3892 SelectObject(hdc, hOldFont);
3895 ReleaseDC(hwnd, hdc);
3897 infoPtr->editItem = hItem;
3899 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3900 WC_EDITW,
3902 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3903 WS_CLIPSIBLINGS | ES_WANTRETURN |
3904 ES_LEFT, hItem->textOffset - 2,
3905 hItem->rect.top - 1, sz.cx + 3,
3906 hItem->rect.bottom -
3907 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3908 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3910 infoPtr->hwndEdit = hwndEdit;
3912 /* Get a 2D border. */
3913 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3914 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3915 SetWindowLongW(hwndEdit, GWL_STYLE,
3916 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3918 SendMessageW(hwndEdit, WM_SETFONT,
3919 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3921 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3922 (DWORD_PTR)
3923 TREEVIEW_Edit_SubclassProc);
3924 if (hItem->pszText)
3925 SetWindowTextW(hwndEdit, hItem->pszText);
3927 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3929 DestroyWindow(hwndEdit);
3930 infoPtr->hwndEdit = 0;
3931 infoPtr->editItem = NULL;
3932 return NULL;
3935 SetFocus(hwndEdit);
3936 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3937 ShowWindow(hwndEdit, SW_SHOW);
3939 return hwndEdit;
3943 static LRESULT
3944 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3946 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3947 NMTVDISPINFOW tvdi;
3948 BOOL bCommit;
3949 WCHAR tmpText[1024] = { '\0' };
3950 WCHAR *newText = tmpText;
3951 int iLength = 0;
3953 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3955 tvdi.item.mask = 0;
3956 tvdi.item.hItem = editedItem;
3957 tvdi.item.state = editedItem->state;
3958 tvdi.item.lParam = editedItem->lParam;
3960 if (!bCancel)
3962 if (!infoPtr->bNtfUnicode)
3963 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3964 else
3965 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3967 if (iLength >= 1023)
3969 ERR("Insufficient space to retrieve new item label\n");
3972 tvdi.item.mask = TVIF_TEXT;
3973 tvdi.item.pszText = tmpText;
3974 tvdi.item.cchTextMax = iLength + 1;
3976 else
3978 tvdi.item.pszText = NULL;
3979 tvdi.item.cchTextMax = 0;
3982 bCommit = TREEVIEW_SendRealNotify(infoPtr, TVN_ENDLABELEDITW, &tvdi.hdr);
3984 if (!bCancel && bCommit) /* Apply the changes */
3986 if (!infoPtr->bNtfUnicode)
3988 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3989 newText = Alloc(len * sizeof(WCHAR));
3990 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3991 iLength = len - 1;
3994 if (strcmpW(newText, editedItem->pszText) != 0)
3996 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3997 if (ptr == NULL)
3999 ERR("OutOfMemory, cannot allocate space for label\n");
4000 if(newText != tmpText) Free(newText);
4001 DestroyWindow(infoPtr->hwndEdit);
4002 infoPtr->hwndEdit = 0;
4003 infoPtr->editItem = NULL;
4004 return FALSE;
4006 else
4008 editedItem->pszText = ptr;
4009 editedItem->cchTextMax = iLength + 1;
4010 strcpyW(editedItem->pszText, newText);
4011 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
4014 if(newText != tmpText) Free(newText);
4017 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4018 DestroyWindow(infoPtr->hwndEdit);
4019 infoPtr->hwndEdit = 0;
4020 infoPtr->editItem = NULL;
4021 return TRUE;
4024 static LRESULT
4025 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4027 if (wParam != TV_EDIT_TIMER)
4029 ERR("got unknown timer\n");
4030 return 1;
4033 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4034 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4036 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4038 return 0;
4042 /* Mouse Tracking/Drag **************************************************/
4044 /***************************************************************************
4045 * This is quite unusual piece of code, but that's how it's implemented in
4046 * Windows.
4048 static LRESULT
4049 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4051 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4052 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4053 RECT r;
4054 MSG msg;
4056 r.top = pt.y - cyDrag;
4057 r.left = pt.x - cxDrag;
4058 r.bottom = pt.y + cyDrag;
4059 r.right = pt.x + cxDrag;
4061 SetCapture(infoPtr->hwnd);
4063 while (1)
4065 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4067 if (msg.message == WM_MOUSEMOVE)
4069 pt.x = (short)LOWORD(msg.lParam);
4070 pt.y = (short)HIWORD(msg.lParam);
4071 if (PtInRect(&r, pt))
4072 continue;
4073 else
4075 ReleaseCapture();
4076 return 1;
4079 else if (msg.message >= WM_LBUTTONDOWN &&
4080 msg.message <= WM_RBUTTONDBLCLK)
4082 break;
4085 DispatchMessageW(&msg);
4088 if (GetCapture() != infoPtr->hwnd)
4089 return 0;
4092 ReleaseCapture();
4093 return 0;
4097 static LRESULT
4098 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4100 TREEVIEW_ITEM *item;
4101 TVHITTESTINFO hit;
4103 TRACE("\n");
4104 SetFocus(infoPtr->hwnd);
4106 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4108 /* If there is pending 'edit label' event - kill it now */
4109 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4112 hit.pt.x = (short)LOWORD(lParam);
4113 hit.pt.y = (short)HIWORD(lParam);
4115 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4116 if (!item)
4117 return 0;
4118 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4120 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4121 { /* FIXME! */
4122 switch (hit.flags)
4124 case TVHT_ONITEMRIGHT:
4125 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4126 break;
4128 case TVHT_ONITEMINDENT:
4129 if (!(infoPtr->dwStyle & TVS_HASLINES))
4131 break;
4133 else
4135 int level = hit.pt.x / infoPtr->uIndent;
4136 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4138 while (item->iLevel > level)
4140 item = item->parent;
4143 /* fall through */
4146 case TVHT_ONITEMLABEL:
4147 case TVHT_ONITEMICON:
4148 case TVHT_ONITEMBUTTON:
4149 TREEVIEW_Toggle(infoPtr, item, TRUE);
4150 break;
4152 case TVHT_ONITEMSTATEICON:
4153 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4154 TREEVIEW_ToggleItemState(infoPtr, item);
4155 else
4156 TREEVIEW_Toggle(infoPtr, item, TRUE);
4157 break;
4160 return TRUE;
4164 static LRESULT
4165 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4167 HWND hwnd = infoPtr->hwnd;
4168 TVHITTESTINFO ht;
4169 BOOL bTrack, bDoLabelEdit;
4171 /* If Edit control is active - kill it and return.
4172 * The best way to do it is to set focus to itself.
4173 * Edit control subclassed procedure will automatically call
4174 * EndEditLabelNow.
4176 if (infoPtr->hwndEdit)
4178 SetFocus(hwnd);
4179 return 0;
4182 ht.pt.x = (short)LOWORD(lParam);
4183 ht.pt.y = (short)HIWORD(lParam);
4185 TREEVIEW_HitTest(infoPtr, &ht);
4186 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4188 /* update focusedItem and redraw both items */
4189 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4191 infoPtr->focusedItem = ht.hItem;
4192 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4193 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4196 bTrack = (ht.flags & TVHT_ONITEM)
4197 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4200 * If the style allows editing and the node is already selected
4201 * and the click occurred on the item label...
4203 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4204 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4206 /* Send NM_CLICK right away */
4207 if (!bTrack)
4208 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4209 goto setfocus;
4211 if (ht.flags & TVHT_ONITEMBUTTON)
4213 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4214 goto setfocus;
4216 else if (bTrack)
4217 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4218 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4220 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4221 infoPtr->dropItem = ht.hItem;
4223 /* clean up focusedItem as we dragged and won't select this item */
4224 if(infoPtr->focusedItem)
4226 /* refresh the item that was focused */
4227 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4228 infoPtr->focusedItem = NULL;
4230 /* refresh the selected item to return the filled background */
4231 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4234 return 0;
4238 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4239 goto setfocus;
4241 if (bDoLabelEdit)
4243 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4244 KillTimer(hwnd, TV_EDIT_TIMER);
4246 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4247 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4249 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4251 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4253 /* Select the current item */
4254 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4255 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4257 else if (ht.flags & TVHT_ONITEMSTATEICON)
4259 /* TVS_CHECKBOXES requires us to toggle the current state */
4260 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4261 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4264 setfocus:
4265 SetFocus(hwnd);
4266 return 0;
4270 static LRESULT
4271 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4273 TVHITTESTINFO ht;
4275 if (infoPtr->hwndEdit)
4277 SetFocus(infoPtr->hwnd);
4278 return 0;
4281 ht.pt.x = (short)LOWORD(lParam);
4282 ht.pt.y = (short)HIWORD(lParam);
4284 TREEVIEW_HitTest(infoPtr, &ht);
4286 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4288 if (ht.hItem)
4290 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4291 infoPtr->dropItem = ht.hItem;
4294 else
4296 SetFocus(infoPtr->hwnd);
4297 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4299 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4300 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4301 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4305 return 0;
4308 static LRESULT
4309 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4311 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4312 INT cx, cy;
4313 HDC hdc, htopdc;
4314 HWND hwtop;
4315 HBITMAP hbmp, hOldbmp;
4316 SIZE size;
4317 RECT rc;
4318 HFONT hOldFont;
4320 TRACE("\n");
4322 if (!(infoPtr->himlNormal))
4323 return 0;
4325 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4326 return 0;
4328 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4330 hwtop = GetDesktopWindow();
4331 htopdc = GetDC(hwtop);
4332 hdc = CreateCompatibleDC(htopdc);
4334 hOldFont = SelectObject(hdc, infoPtr->hFont);
4336 if (dragItem->pszText)
4337 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4338 &size);
4339 else
4340 GetTextExtentPoint32A(hdc, "", 0, &size);
4342 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4343 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4344 hOldbmp = SelectObject(hdc, hbmp);
4346 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4347 size.cx += cx;
4348 if (cy > size.cy)
4349 size.cy = cy;
4351 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4352 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4353 ILD_NORMAL);
4356 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4357 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4360 /* draw item text */
4362 SetRect(&rc, cx, 0, size.cx, size.cy);
4364 if (dragItem->pszText)
4365 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4366 DT_LEFT);
4368 SelectObject(hdc, hOldFont);
4369 SelectObject(hdc, hOldbmp);
4371 ImageList_Add(infoPtr->dragList, hbmp, 0);
4373 DeleteDC(hdc);
4374 DeleteObject(hbmp);
4375 ReleaseDC(hwtop, htopdc);
4377 return (LRESULT)infoPtr->dragList;
4380 /* Selection ************************************************************/
4382 static LRESULT
4383 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4384 INT cause)
4386 TREEVIEW_ITEM *prevSelect;
4388 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4390 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4391 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4392 newSelect ? newSelect->state : 0);
4394 /* reset and redraw focusedItem if focusedItem was set so we don't */
4395 /* have to worry about the previously focused item when we set a new one */
4396 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4397 infoPtr->focusedItem = NULL;
4399 switch (action)
4401 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4402 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4403 /* Fall through */
4404 case TVGN_CARET:
4405 prevSelect = infoPtr->selectedItem;
4407 if (prevSelect == newSelect) {
4408 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4409 break;
4412 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4413 TVN_SELCHANGINGW,
4414 cause,
4415 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4416 prevSelect,
4417 newSelect))
4418 return FALSE;
4420 if (prevSelect)
4421 prevSelect->state &= ~TVIS_SELECTED;
4422 if (newSelect)
4423 newSelect->state |= TVIS_SELECTED;
4425 infoPtr->selectedItem = newSelect;
4427 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4429 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4430 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4432 TREEVIEW_SendTreeviewNotify(infoPtr,
4433 TVN_SELCHANGEDW,
4434 cause,
4435 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4436 prevSelect,
4437 newSelect);
4438 break;
4440 case TVGN_DROPHILITE:
4441 prevSelect = infoPtr->dropItem;
4443 if (prevSelect)
4444 prevSelect->state &= ~TVIS_DROPHILITED;
4446 infoPtr->dropItem = newSelect;
4448 if (newSelect)
4449 newSelect->state |= TVIS_DROPHILITED;
4451 TREEVIEW_Invalidate(infoPtr, prevSelect);
4452 TREEVIEW_Invalidate(infoPtr, newSelect);
4453 break;
4455 case TVGN_FIRSTVISIBLE:
4456 if (newSelect != NULL)
4458 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4459 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4460 TREEVIEW_Invalidate(infoPtr, NULL);
4462 break;
4465 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4466 return TRUE;
4469 /* FIXME: handle NM_KILLFOCUS etc */
4470 static LRESULT
4471 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4473 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4475 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4476 return FALSE;
4478 if (item == infoPtr->selectedItem)
4479 return TRUE;
4481 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4483 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4484 return FALSE;
4486 TREEVIEW_SingleExpand(infoPtr, selection, item);
4488 return TRUE;
4491 /*************************************************************************
4492 * TREEVIEW_ProcessLetterKeys
4494 * Processes keyboard messages generated by pressing the letter keys
4495 * on the keyboard.
4496 * What this does is perform a case insensitive search from the
4497 * current position with the following quirks:
4498 * - If two chars or more are pressed in quick succession we search
4499 * for the corresponding string (e.g. 'abc').
4500 * - If there is a delay we wipe away the current search string and
4501 * restart with just that char.
4502 * - If the user keeps pressing the same character, whether slowly or
4503 * fast, so that the search string is entirely composed of this
4504 * character ('aaaaa' for instance), then we search for first item
4505 * that starting with that character.
4506 * - If the user types the above character in quick succession, then
4507 * we must also search for the corresponding string ('aaaaa'), and
4508 * go to that string if there is a match.
4510 * RETURNS
4512 * Zero.
4514 * BUGS
4516 * - The current implementation has a list of characters it will
4517 * accept and it ignores everything else. In particular it will
4518 * ignore accentuated characters which seems to match what
4519 * Windows does. But I'm not sure it makes sense to follow
4520 * Windows there.
4521 * - We don't sound a beep when the search fails.
4522 * - The search should start from the focused item, not from the selected
4523 * item. One reason for this is to allow for multiple selections in trees.
4524 * But currently infoPtr->focusedItem does not seem very usable.
4526 * SEE ALSO
4528 * TREEVIEW_ProcessLetterKeys
4530 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4532 HTREEITEM nItem;
4533 HTREEITEM endidx,idx;
4534 TVITEMEXW item;
4535 WCHAR buffer[MAX_PATH];
4536 DWORD timestamp,elapsed;
4538 /* simple parameter checking */
4539 if (!charCode || !keyData) return 0;
4541 /* only allow the valid WM_CHARs through */
4542 if (!isalnum(charCode) &&
4543 charCode != '.' && charCode != '`' && charCode != '!' &&
4544 charCode != '@' && charCode != '#' && charCode != '$' &&
4545 charCode != '%' && charCode != '^' && charCode != '&' &&
4546 charCode != '*' && charCode != '(' && charCode != ')' &&
4547 charCode != '-' && charCode != '_' && charCode != '+' &&
4548 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4549 charCode != '}' && charCode != '[' && charCode != '{' &&
4550 charCode != '/' && charCode != '?' && charCode != '>' &&
4551 charCode != '<' && charCode != ',' && charCode != '~')
4552 return 0;
4554 /* compute how much time elapsed since last keypress */
4555 timestamp = GetTickCount();
4556 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4557 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4558 } else {
4559 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4562 /* update the search parameters */
4563 infoPtr->lastKeyPressTimestamp=timestamp;
4564 if (elapsed < KEY_DELAY) {
4565 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4566 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4568 if (infoPtr->charCode != charCode) {
4569 infoPtr->charCode=charCode=0;
4571 } else {
4572 infoPtr->charCode=charCode;
4573 infoPtr->szSearchParam[0]=charCode;
4574 infoPtr->nSearchParamLength=1;
4575 /* Redundant with the 1 char string */
4576 charCode=0;
4579 /* and search from the current position */
4580 nItem=NULL;
4581 if (infoPtr->selectedItem != NULL) {
4582 endidx=infoPtr->selectedItem;
4583 /* if looking for single character match,
4584 * then we must always move forward
4586 if (infoPtr->nSearchParamLength == 1)
4587 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4588 else
4589 idx=endidx;
4590 } else {
4591 endidx=NULL;
4592 idx=infoPtr->root->firstChild;
4594 do {
4595 /* At the end point, sort out wrapping */
4596 if (idx == NULL) {
4598 /* If endidx is null, stop at the last item (ie top to bottom) */
4599 if (endidx == NULL)
4600 break;
4602 /* Otherwise, start again at the very beginning */
4603 idx=infoPtr->root->firstChild;
4605 /* But if we are stopping on the first child, end now! */
4606 if (idx == endidx) break;
4609 /* get item */
4610 ZeroMemory(&item, sizeof(item));
4611 item.mask = TVIF_TEXT;
4612 item.hItem = idx;
4613 item.pszText = buffer;
4614 item.cchTextMax = sizeof(buffer);
4615 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4617 /* check for a match */
4618 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4619 nItem=idx;
4620 break;
4621 } else if ( (charCode != 0) && (nItem == NULL) &&
4622 (nItem != infoPtr->selectedItem) &&
4623 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4624 /* This would work but we must keep looking for a longer match */
4625 nItem=idx;
4627 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4628 } while (idx != endidx);
4630 if (nItem != NULL) {
4631 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4632 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4636 return 0;
4639 /* Scrolling ************************************************************/
4641 static LRESULT
4642 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4644 int viscount;
4645 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4646 HTREEITEM newFirstVisible = NULL;
4647 int visible_pos = -1;
4649 if (!TREEVIEW_ValidItem(infoPtr, item))
4650 return FALSE;
4652 if (!ISVISIBLE(item))
4654 /* Expand parents as necessary. */
4655 HTREEITEM parent;
4657 /* see if we are trying to ensure that root is visible */
4658 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4659 parent = item->parent;
4660 else
4661 parent = item; /* this item is the topmost item */
4663 while (parent != infoPtr->root)
4665 if (!(parent->state & TVIS_EXPANDED))
4666 TREEVIEW_Expand(infoPtr, parent, FALSE, TRUE);
4668 parent = parent->parent;
4672 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4674 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4675 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4677 if (hasFirstVisible)
4678 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4680 if (visible_pos < 0)
4682 /* item is before the start of the list: put it at the top. */
4683 newFirstVisible = item;
4685 else if (visible_pos >= viscount
4686 /* Sometimes, before we are displayed, GVC is 0, causing us to
4687 * spuriously scroll up. */
4688 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4690 /* item is past the end of the list. */
4691 int scroll = visible_pos - viscount;
4693 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4694 scroll + 1);
4697 if (bHScroll)
4699 /* Scroll window so item's text is visible as much as possible */
4700 /* Calculation of amount of extra space is taken from EditLabel code */
4701 INT pos, x;
4702 TEXTMETRICW textMetric;
4703 HDC hdc = GetWindowDC(infoPtr->hwnd);
4705 x = item->textWidth;
4707 GetTextMetricsW(hdc, &textMetric);
4708 ReleaseDC(infoPtr->hwnd, hdc);
4710 x += (textMetric.tmMaxCharWidth * 2);
4711 x = max(x, textMetric.tmMaxCharWidth * 3);
4713 if (item->textOffset < 0)
4714 pos = item->textOffset;
4715 else if (item->textOffset + x > infoPtr->clientWidth)
4717 if (x > infoPtr->clientWidth)
4718 pos = item->textOffset;
4719 else
4720 pos = item->textOffset + x - infoPtr->clientWidth;
4722 else
4723 pos = 0;
4725 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4728 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4730 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4732 return TRUE;
4735 return FALSE;
4738 static VOID
4739 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4740 TREEVIEW_ITEM *newFirstVisible,
4741 BOOL bUpdateScrollPos)
4743 int gap_size;
4745 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4747 if (newFirstVisible != NULL)
4749 /* Prevent an empty gap from appearing at the bottom... */
4750 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4751 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4753 if (gap_size > 0)
4755 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4756 -gap_size);
4758 /* ... unless we just don't have enough items. */
4759 if (newFirstVisible == NULL)
4760 newFirstVisible = infoPtr->root->firstChild;
4764 if (infoPtr->firstVisible != newFirstVisible)
4766 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4768 infoPtr->firstVisible = newFirstVisible;
4769 TREEVIEW_Invalidate(infoPtr, NULL);
4771 else
4773 TREEVIEW_ITEM *item;
4774 int scroll = infoPtr->uItemHeight *
4775 (infoPtr->firstVisible->visibleOrder
4776 - newFirstVisible->visibleOrder);
4778 infoPtr->firstVisible = newFirstVisible;
4780 for (item = infoPtr->root->firstChild; item != NULL;
4781 item = TREEVIEW_GetNextListItem(infoPtr, item))
4783 item->rect.top += scroll;
4784 item->rect.bottom += scroll;
4787 if (bUpdateScrollPos)
4788 SetScrollPos(infoPtr->hwnd, SB_VERT,
4789 newFirstVisible->visibleOrder, TRUE);
4791 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4796 /************************************************************************
4797 * VScroll is always in units of visible items. i.e. we always have a
4798 * visible item aligned to the top of the control. (Unless we have no
4799 * items at all.)
4801 static LRESULT
4802 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4804 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4805 TREEVIEW_ITEM *newFirstVisible = NULL;
4807 int nScrollCode = LOWORD(wParam);
4809 TRACE("wp %lx\n", wParam);
4811 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4812 return 0;
4814 if (!oldFirstVisible)
4816 assert(infoPtr->root->firstChild == NULL);
4817 return 0;
4820 switch (nScrollCode)
4822 case SB_TOP:
4823 newFirstVisible = infoPtr->root->firstChild;
4824 break;
4826 case SB_BOTTOM:
4827 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4828 break;
4830 case SB_LINEUP:
4831 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4832 break;
4834 case SB_LINEDOWN:
4835 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4836 break;
4838 case SB_PAGEUP:
4839 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4840 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4841 break;
4843 case SB_PAGEDOWN:
4844 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4845 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4846 break;
4848 case SB_THUMBTRACK:
4849 case SB_THUMBPOSITION:
4850 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4851 infoPtr->root->firstChild,
4852 (LONG)(SHORT)HIWORD(wParam));
4853 break;
4855 case SB_ENDSCROLL:
4856 return 0;
4859 if (newFirstVisible != NULL)
4861 if (newFirstVisible != oldFirstVisible)
4862 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4863 nScrollCode != SB_THUMBTRACK);
4864 else if (nScrollCode == SB_THUMBPOSITION)
4865 SetScrollPos(infoPtr->hwnd, SB_VERT,
4866 newFirstVisible->visibleOrder, TRUE);
4869 return 0;
4872 static LRESULT
4873 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4875 int maxWidth;
4876 int scrollX = infoPtr->scrollX;
4877 int nScrollCode = LOWORD(wParam);
4879 TRACE("wp %lx\n", wParam);
4881 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4882 return FALSE;
4884 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4885 /* shall never occur */
4886 if (maxWidth <= 0)
4888 scrollX = 0;
4889 goto scroll;
4892 switch (nScrollCode)
4894 case SB_LINELEFT:
4895 scrollX -= infoPtr->uItemHeight;
4896 break;
4897 case SB_LINERIGHT:
4898 scrollX += infoPtr->uItemHeight;
4899 break;
4900 case SB_PAGELEFT:
4901 scrollX -= infoPtr->clientWidth;
4902 break;
4903 case SB_PAGERIGHT:
4904 scrollX += infoPtr->clientWidth;
4905 break;
4907 case SB_THUMBTRACK:
4908 case SB_THUMBPOSITION:
4909 scrollX = (int)(SHORT)HIWORD(wParam);
4910 break;
4912 case SB_ENDSCROLL:
4913 return 0;
4916 if (scrollX > maxWidth)
4917 scrollX = maxWidth;
4918 else if (scrollX < 0)
4919 scrollX = 0;
4921 scroll:
4922 if (scrollX != infoPtr->scrollX)
4924 TREEVIEW_ITEM *item;
4925 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4927 for (item = infoPtr->root->firstChild; item != NULL;
4928 item = TREEVIEW_GetNextListItem(infoPtr, item))
4930 item->linesOffset += scroll_pixels;
4931 item->stateOffset += scroll_pixels;
4932 item->imageOffset += scroll_pixels;
4933 item->textOffset += scroll_pixels;
4936 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4937 infoPtr->scrollX = scrollX;
4938 UpdateWindow(infoPtr->hwnd);
4941 if (nScrollCode != SB_THUMBTRACK)
4942 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4944 return 0;
4947 static LRESULT
4948 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4950 short wheelDelta;
4951 UINT pulScrollLines = 3;
4953 if (wParam & (MK_SHIFT | MK_CONTROL))
4954 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4956 if (infoPtr->firstVisible == NULL)
4957 return TRUE;
4959 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4961 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4962 /* if scrolling changes direction, ignore left overs */
4963 if ((wheelDelta < 0 && infoPtr->wheelRemainder < 0) ||
4964 (wheelDelta > 0 && infoPtr->wheelRemainder > 0))
4965 infoPtr->wheelRemainder += wheelDelta;
4966 else
4967 infoPtr->wheelRemainder = wheelDelta;
4969 if (infoPtr->wheelRemainder && pulScrollLines)
4971 int newDy;
4972 int maxDy;
4973 int lineScroll;
4975 lineScroll = pulScrollLines * (float)infoPtr->wheelRemainder / WHEEL_DELTA;
4976 infoPtr->wheelRemainder -= WHEEL_DELTA * lineScroll / (int)pulScrollLines;
4978 newDy = infoPtr->firstVisible->visibleOrder - lineScroll;
4979 maxDy = infoPtr->maxVisibleOrder;
4981 if (newDy > maxDy)
4982 newDy = maxDy;
4984 if (newDy < 0)
4985 newDy = 0;
4987 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4989 return TRUE;
4992 /* Create/Destroy *******************************************************/
4994 static LRESULT
4995 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4997 RECT rcClient;
4998 TREEVIEW_INFO *infoPtr;
4999 LOGFONTW lf;
5001 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5003 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
5005 if (infoPtr == NULL)
5007 ERR("could not allocate info memory!\n");
5008 return 0;
5011 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5013 infoPtr->hwnd = hwnd;
5014 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5015 infoPtr->Timer = 0;
5016 infoPtr->uNumItems = 0;
5017 infoPtr->cdmode = 0;
5018 infoPtr->uScrollTime = 300; /* milliseconds */
5019 infoPtr->bRedraw = TRUE;
5021 GetClientRect(hwnd, &rcClient);
5023 /* No scroll bars yet. */
5024 infoPtr->clientWidth = rcClient.right;
5025 infoPtr->clientHeight = rcClient.bottom;
5026 infoPtr->uInternalStatus = 0;
5028 infoPtr->treeWidth = 0;
5029 infoPtr->treeHeight = 0;
5031 infoPtr->uIndent = MINIMUM_INDENT;
5032 infoPtr->selectedItem = NULL;
5033 infoPtr->focusedItem = NULL;
5034 infoPtr->hotItem = NULL;
5035 infoPtr->editItem = NULL;
5036 infoPtr->firstVisible = NULL;
5037 infoPtr->maxVisibleOrder = 0;
5038 infoPtr->dropItem = NULL;
5039 infoPtr->insertMarkItem = NULL;
5040 infoPtr->insertBeforeorAfter = 0;
5041 /* dragList */
5043 infoPtr->scrollX = 0;
5044 infoPtr->wheelRemainder = 0;
5046 infoPtr->clrBk = CLR_NONE; /* use system color */
5047 infoPtr->clrText = CLR_NONE; /* use system color */
5048 infoPtr->clrLine = CLR_DEFAULT;
5049 infoPtr->clrInsertMark = CLR_DEFAULT;
5051 /* hwndToolTip */
5053 infoPtr->hwndEdit = NULL;
5054 infoPtr->wpEditOrig = NULL;
5055 infoPtr->bIgnoreEditKillFocus = FALSE;
5056 infoPtr->bLabelChanged = FALSE;
5058 infoPtr->himlNormal = NULL;
5059 infoPtr->himlState = NULL;
5060 infoPtr->normalImageWidth = 0;
5061 infoPtr->normalImageHeight = 0;
5062 infoPtr->stateImageWidth = 0;
5063 infoPtr->stateImageHeight = 0;
5065 infoPtr->items = DPA_Create(16);
5067 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5068 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5069 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5070 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5071 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
5072 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5074 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5076 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5077 infoPtr->root->state = TVIS_EXPANDED;
5078 infoPtr->root->iLevel = -1;
5079 infoPtr->root->visibleOrder = -1;
5081 infoPtr->hwndNotify = lpcs->hwndParent;
5082 infoPtr->hwndToolTip = 0;
5084 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5085 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5087 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5088 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5089 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5090 hwnd, 0, 0, 0);
5092 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5093 ShowScrollBar(hwnd, SB_VERT, FALSE);
5094 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5096 OpenThemeData (hwnd, themeClass);
5098 return 0;
5102 static LRESULT
5103 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5105 TRACE("\n");
5107 /* free item data */
5108 TREEVIEW_RemoveTree(infoPtr);
5109 /* root isn't freed with other items */
5110 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5111 DPA_Destroy(infoPtr->items);
5113 /* tool tip is automatically destroyed: we are its owner */
5115 /* Restore original wndproc */
5116 if (infoPtr->hwndEdit)
5117 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5118 (DWORD_PTR)infoPtr->wpEditOrig);
5120 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5122 /* Deassociate treeview from the window before doing anything drastic. */
5123 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5125 DeleteObject(infoPtr->hDefaultFont);
5126 DeleteObject(infoPtr->hBoldFont);
5127 DeleteObject(infoPtr->hUnderlineFont);
5128 DeleteObject(infoPtr->hBoldUnderlineFont);
5129 Free(infoPtr);
5131 return 0;
5134 /* Miscellaneous Messages ***********************************************/
5136 static LRESULT
5137 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5139 static const struct
5141 unsigned char code;
5143 scroll[] =
5145 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5146 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5147 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5148 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5149 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5150 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5151 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5152 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5153 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5154 #undef SCROLL_ENTRY
5157 if (key >= VK_PRIOR && key <= VK_DOWN)
5159 unsigned char code = scroll[key - VK_PRIOR].code;
5161 (((code & (1 << 7)) == (SB_HORZ << 7))
5162 ? TREEVIEW_HScroll
5163 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5166 return 0;
5169 /************************************************************************
5170 * TREEVIEW_KeyDown
5172 * VK_UP Move selection to the previous non-hidden item.
5173 * VK_DOWN Move selection to the next non-hidden item.
5174 * VK_HOME Move selection to the first item.
5175 * VK_END Move selection to the last item.
5176 * VK_LEFT If expanded then collapse, otherwise move to parent.
5177 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5178 * VK_ADD Expand.
5179 * VK_SUBTRACT Collapse.
5180 * VK_MULTIPLY Expand all.
5181 * VK_PRIOR Move up GetVisibleCount items.
5182 * VK_NEXT Move down GetVisibleCount items.
5183 * VK_BACK Move to parent.
5184 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5186 static LRESULT
5187 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5189 /* If it is non-NULL and different, it will be selected and visible. */
5190 TREEVIEW_ITEM *newSelection = NULL;
5191 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5192 NMTVKEYDOWN nmkeydown;
5194 TRACE("%lx\n", wParam);
5196 nmkeydown.wVKey = wParam;
5197 nmkeydown.flags = 0;
5198 TREEVIEW_SendRealNotify(infoPtr, TVN_KEYDOWN, &nmkeydown.hdr);
5200 if (prevItem == NULL)
5201 return FALSE;
5203 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5204 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5206 switch (wParam)
5208 case VK_UP:
5209 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5210 if (!newSelection)
5211 newSelection = infoPtr->root->firstChild;
5212 break;
5214 case VK_DOWN:
5215 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5216 break;
5218 case VK_RETURN:
5219 TREEVIEW_SendSimpleNotify(infoPtr, NM_RETURN);
5220 break;
5222 case VK_HOME:
5223 newSelection = infoPtr->root->firstChild;
5224 break;
5226 case VK_END:
5227 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5228 break;
5230 case VK_LEFT:
5231 if (prevItem->state & TVIS_EXPANDED)
5233 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5235 else if (prevItem->parent != infoPtr->root)
5237 newSelection = prevItem->parent;
5239 break;
5241 case VK_RIGHT:
5242 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5244 if (!(prevItem->state & TVIS_EXPANDED))
5245 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5246 else
5248 newSelection = prevItem->firstChild;
5252 break;
5254 case VK_MULTIPLY:
5255 TREEVIEW_ExpandAll(infoPtr, prevItem);
5256 break;
5258 case VK_ADD:
5259 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5260 break;
5262 case VK_SUBTRACT:
5263 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5264 break;
5266 case VK_PRIOR:
5267 newSelection
5268 = TREEVIEW_GetListItem(infoPtr, prevItem,
5269 -TREEVIEW_GetVisibleCount(infoPtr));
5270 break;
5272 case VK_NEXT:
5273 newSelection
5274 = TREEVIEW_GetListItem(infoPtr, prevItem,
5275 TREEVIEW_GetVisibleCount(infoPtr));
5276 break;
5278 case VK_BACK:
5279 newSelection = prevItem->parent;
5280 if (newSelection == infoPtr->root)
5281 newSelection = NULL;
5282 break;
5284 case VK_SPACE:
5285 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5286 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5287 break;
5290 if (newSelection && newSelection != prevItem)
5292 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5293 TVC_BYKEYBOARD))
5295 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5299 return FALSE;
5302 static LRESULT
5303 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5305 /* remove hot effect from item */
5306 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5307 infoPtr->hotItem = NULL;
5309 return 0;
5312 static LRESULT
5313 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5315 POINT pt;
5316 TRACKMOUSEEVENT trackinfo;
5317 TREEVIEW_ITEM * item;
5319 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5321 /* fill in the TRACKMOUSEEVENT struct */
5322 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5323 trackinfo.dwFlags = TME_QUERY;
5324 trackinfo.hwndTrack = infoPtr->hwnd;
5326 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5327 _TrackMouseEvent(&trackinfo);
5329 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5330 if(!(trackinfo.dwFlags & TME_LEAVE))
5332 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5333 trackinfo.hwndTrack = infoPtr->hwnd;
5334 /* do it as fast as possible, minimal systimer latency will be used */
5335 trackinfo.dwHoverTime = 1;
5337 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5338 /* and can properly deactivate the hot item */
5339 _TrackMouseEvent(&trackinfo);
5342 pt.x = (short)LOWORD(lParam);
5343 pt.y = (short)HIWORD(lParam);
5345 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5347 if (item != infoPtr->hotItem)
5349 /* redraw old hot item */
5350 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5351 infoPtr->hotItem = item;
5352 /* redraw new hot item */
5353 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5356 return 0;
5359 /* Draw themed border */
5360 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5362 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5363 HDC dc;
5364 RECT r;
5365 HRGN cliprgn;
5366 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5367 cyEdge = GetSystemMetrics (SM_CYEDGE);
5369 if (!theme)
5370 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5372 GetWindowRect(infoPtr->hwnd, &r);
5374 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5375 r.right - cxEdge, r.bottom - cyEdge);
5376 if (region != (HRGN)1)
5377 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5378 OffsetRect(&r, -r.left, -r.top);
5380 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5381 OffsetRect(&r, -r.left, -r.top);
5383 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5384 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5385 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5386 ReleaseDC(infoPtr->hwnd, dc);
5388 /* Call default proc to get the scrollbars etc. painted */
5389 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5391 return TRUE;
5394 static LRESULT
5395 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5397 LPNMHDR lpnmh = (LPNMHDR)lParam;
5399 if (lpnmh->code == PGN_CALCSIZE) {
5400 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5402 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5403 lppgc->iWidth = infoPtr->treeWidth;
5404 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5405 infoPtr->treeWidth, infoPtr->clientWidth);
5407 else {
5408 lppgc->iHeight = infoPtr->treeHeight;
5409 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5410 infoPtr->treeHeight, infoPtr->clientHeight);
5412 return 0;
5414 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5417 static LRESULT
5418 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5420 if (wParam == SIZE_RESTORED)
5422 infoPtr->clientWidth = (short)LOWORD(lParam);
5423 infoPtr->clientHeight = (short)HIWORD(lParam);
5425 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5426 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5427 TREEVIEW_UpdateScrollBars(infoPtr);
5429 else
5431 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5434 TREEVIEW_Invalidate(infoPtr, NULL);
5435 return 0;
5438 static LRESULT
5439 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5441 TRACE("(%lx %lx)\n", wParam, lParam);
5443 if (wParam == GWL_STYLE)
5445 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5447 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5449 if (dwNewStyle & TVS_CHECKBOXES)
5451 TREEVIEW_InitCheckboxes(infoPtr);
5452 TRACE("checkboxes enabled\n");
5454 /* set all items to state image index 1 */
5455 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5457 else
5459 FIXME("tried to disable checkboxes\n");
5463 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5465 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5467 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5468 TRACE("tooltips enabled\n");
5470 else
5472 DestroyWindow(infoPtr->hwndToolTip);
5473 infoPtr->hwndToolTip = 0;
5474 TRACE("tooltips disabled\n");
5478 infoPtr->dwStyle = dwNewStyle;
5481 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
5482 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5483 TREEVIEW_UpdateScrollBars(infoPtr);
5484 TREEVIEW_Invalidate(infoPtr, NULL);
5486 return 0;
5489 static LRESULT
5490 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5492 POINT pt;
5493 TREEVIEW_ITEM * item;
5494 NMMOUSE nmmouse;
5496 GetCursorPos(&pt);
5497 ScreenToClient(infoPtr->hwnd, &pt);
5499 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5501 memset(&nmmouse, 0, sizeof(nmmouse));
5502 if (item)
5504 nmmouse.dwItemSpec = (DWORD_PTR)item;
5505 nmmouse.dwItemData = item->lParam;
5507 nmmouse.pt.x = 0;
5508 nmmouse.pt.y = 0;
5509 nmmouse.dwHitInfo = lParam;
5510 if (TREEVIEW_SendRealNotify(infoPtr, NM_SETCURSOR, &nmmouse.hdr))
5511 return 0;
5513 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5515 SetCursor(infoPtr->hcurHand);
5516 return 0;
5518 else
5519 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5522 static LRESULT
5523 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5525 TRACE("\n");
5527 if (!infoPtr->selectedItem)
5529 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5530 TVC_UNKNOWN);
5533 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5534 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5535 return 0;
5538 static LRESULT
5539 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5541 TRACE("\n");
5543 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5544 UpdateWindow(infoPtr->hwnd);
5545 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5546 return 0;
5549 /* update theme after a WM_THEMECHANGED message */
5550 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5552 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5553 CloseThemeData (theme);
5554 OpenThemeData (infoPtr->hwnd, themeClass);
5555 return 0;
5559 static LRESULT WINAPI
5560 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5562 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5564 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5566 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5567 else
5569 if (uMsg == WM_CREATE)
5570 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5571 else
5572 goto def;
5575 switch (uMsg)
5577 case TVM_CREATEDRAGIMAGE:
5578 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5580 case TVM_DELETEITEM:
5581 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5583 case TVM_EDITLABELA:
5584 case TVM_EDITLABELW:
5585 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5587 case TVM_ENDEDITLABELNOW:
5588 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5590 case TVM_ENSUREVISIBLE:
5591 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5593 case TVM_EXPAND:
5594 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5596 case TVM_GETBKCOLOR:
5597 return TREEVIEW_GetBkColor(infoPtr);
5599 case TVM_GETCOUNT:
5600 return TREEVIEW_GetCount(infoPtr);
5602 case TVM_GETEDITCONTROL:
5603 return TREEVIEW_GetEditControl(infoPtr);
5605 case TVM_GETIMAGELIST:
5606 return TREEVIEW_GetImageList(infoPtr, wParam);
5608 case TVM_GETINDENT:
5609 return TREEVIEW_GetIndent(infoPtr);
5611 case TVM_GETINSERTMARKCOLOR:
5612 return TREEVIEW_GetInsertMarkColor(infoPtr);
5614 case TVM_GETISEARCHSTRINGA:
5615 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5616 return 0;
5618 case TVM_GETISEARCHSTRINGW:
5619 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5620 return 0;
5622 case TVM_GETITEMA:
5623 case TVM_GETITEMW:
5624 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5625 uMsg == TVM_GETITEMW);
5626 case TVM_GETITEMHEIGHT:
5627 return TREEVIEW_GetItemHeight(infoPtr);
5629 case TVM_GETITEMRECT:
5630 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5632 case TVM_GETITEMSTATE:
5633 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5635 case TVM_GETLINECOLOR:
5636 return TREEVIEW_GetLineColor(infoPtr);
5638 case TVM_GETNEXTITEM:
5639 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5641 case TVM_GETSCROLLTIME:
5642 return TREEVIEW_GetScrollTime(infoPtr);
5644 case TVM_GETTEXTCOLOR:
5645 return TREEVIEW_GetTextColor(infoPtr);
5647 case TVM_GETTOOLTIPS:
5648 return TREEVIEW_GetToolTips(infoPtr);
5650 case TVM_GETUNICODEFORMAT:
5651 return TREEVIEW_GetUnicodeFormat(infoPtr);
5653 case TVM_GETVISIBLECOUNT:
5654 return TREEVIEW_GetVisibleCount(infoPtr);
5656 case TVM_HITTEST:
5657 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5659 case TVM_INSERTITEMA:
5660 case TVM_INSERTITEMW:
5661 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5662 uMsg == TVM_INSERTITEMW);
5663 case TVM_SELECTITEM:
5664 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5666 case TVM_SETBKCOLOR:
5667 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5669 case TVM_SETIMAGELIST:
5670 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5672 case TVM_SETINDENT:
5673 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5675 case TVM_SETINSERTMARK:
5676 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5678 case TVM_SETINSERTMARKCOLOR:
5679 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5681 case TVM_SETITEMA:
5682 case TVM_SETITEMW:
5683 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5684 uMsg == TVM_SETITEMW);
5685 case TVM_SETLINECOLOR:
5686 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5688 case TVM_SETITEMHEIGHT:
5689 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5691 case TVM_SETSCROLLTIME:
5692 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5694 case TVM_SETTEXTCOLOR:
5695 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5697 case TVM_SETTOOLTIPS:
5698 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5700 case TVM_SETUNICODEFORMAT:
5701 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5703 case TVM_SORTCHILDREN:
5704 return TREEVIEW_SortChildren(infoPtr, lParam);
5706 case TVM_SORTCHILDRENCB:
5707 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5709 case WM_CHAR:
5710 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5712 case WM_COMMAND:
5713 return TREEVIEW_Command(infoPtr, wParam, lParam);
5715 case WM_DESTROY:
5716 return TREEVIEW_Destroy(infoPtr);
5718 /* WM_ENABLE */
5720 case WM_ERASEBKGND:
5721 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5723 case WM_GETDLGCODE:
5724 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5726 case WM_GETFONT:
5727 return TREEVIEW_GetFont(infoPtr);
5729 case WM_HSCROLL:
5730 return TREEVIEW_HScroll(infoPtr, wParam);
5732 case WM_KEYDOWN:
5733 return TREEVIEW_KeyDown(infoPtr, wParam);
5735 case WM_KILLFOCUS:
5736 return TREEVIEW_KillFocus(infoPtr);
5738 case WM_LBUTTONDBLCLK:
5739 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5741 case WM_LBUTTONDOWN:
5742 return TREEVIEW_LButtonDown(infoPtr, lParam);
5744 /* WM_MBUTTONDOWN */
5746 case WM_MOUSELEAVE:
5747 return TREEVIEW_MouseLeave(infoPtr);
5749 case WM_MOUSEMOVE:
5750 return TREEVIEW_MouseMove(infoPtr, lParam);
5752 case WM_NCLBUTTONDOWN:
5753 if (infoPtr->hwndEdit)
5754 SetFocus(infoPtr->hwnd);
5755 goto def;
5757 case WM_NCPAINT:
5758 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5760 case WM_NOTIFY:
5761 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5763 case WM_NOTIFYFORMAT:
5764 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5766 case WM_PRINTCLIENT:
5767 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5769 case WM_PAINT:
5770 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5772 case WM_RBUTTONDOWN:
5773 return TREEVIEW_RButtonDown(infoPtr, lParam);
5775 case WM_SETCURSOR:
5776 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5778 case WM_SETFOCUS:
5779 return TREEVIEW_SetFocus(infoPtr);
5781 case WM_SETFONT:
5782 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5784 case WM_SETREDRAW:
5785 return TREEVIEW_SetRedraw(infoPtr, wParam);
5787 case WM_SIZE:
5788 return TREEVIEW_Size(infoPtr, wParam, lParam);
5790 case WM_STYLECHANGED:
5791 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5793 case WM_SYSCOLORCHANGE:
5794 COMCTL32_RefreshSysColors();
5795 return 0;
5797 /* WM_SYSKEYDOWN */
5799 case WM_TIMER:
5800 return TREEVIEW_HandleTimer(infoPtr, wParam);
5802 case WM_THEMECHANGED:
5803 return TREEVIEW_ThemeChanged (infoPtr);
5805 case WM_VSCROLL:
5806 return TREEVIEW_VScroll(infoPtr, wParam);
5808 /* WM_WININICHANGE */
5810 case WM_MOUSEWHEEL:
5811 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5813 case WM_DRAWITEM:
5814 TRACE("drawItem\n");
5815 goto def;
5817 default:
5818 /* This mostly catches MFC and Delphi messages. :( */
5819 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5820 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5821 def:
5822 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5827 /* Class Registration ***************************************************/
5829 VOID
5830 TREEVIEW_Register(void)
5832 WNDCLASSW wndClass;
5834 TRACE("\n");
5836 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5837 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5838 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5839 wndClass.cbClsExtra = 0;
5840 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5842 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5843 wndClass.hbrBackground = 0;
5844 wndClass.lpszClassName = WC_TREEVIEWW;
5846 RegisterClassW(&wndClass);
5850 VOID
5851 TREEVIEW_Unregister(void)
5853 UnregisterClassW(WC_TREEVIEWW, NULL);
5857 /* Tree Verification ****************************************************/
5859 static inline void
5860 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5862 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5863 const TREEVIEW_ITEM *item)
5865 assert(infoPtr != NULL);
5866 assert(item != NULL);
5868 /* both NULL, or both non-null */
5869 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5871 assert(item->firstChild != item);
5872 assert(item->lastChild != item);
5874 if (item->firstChild)
5876 assert(item->firstChild->parent == item);
5877 assert(item->firstChild->prevSibling == NULL);
5880 if (item->lastChild)
5882 assert(item->lastChild->parent == item);
5883 assert(item->lastChild->nextSibling == NULL);
5886 assert(item->nextSibling != item);
5887 if (item->nextSibling)
5889 assert(item->nextSibling->parent == item->parent);
5890 assert(item->nextSibling->prevSibling == item);
5893 assert(item->prevSibling != item);
5894 if (item->prevSibling)
5896 assert(item->prevSibling->parent == item->parent);
5897 assert(item->prevSibling->nextSibling == item);
5901 static inline void
5902 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5904 assert(item != NULL);
5906 assert(item->parent != NULL);
5907 assert(item->parent != item);
5908 assert(item->iLevel == item->parent->iLevel + 1);
5910 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5912 TREEVIEW_VerifyItemCommon(infoPtr, item);
5914 TREEVIEW_VerifyChildren(infoPtr, item);
5917 static inline void
5918 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5920 const TREEVIEW_ITEM *child;
5921 assert(item != NULL);
5923 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5924 TREEVIEW_VerifyItem(infoPtr, child);
5927 static inline void
5928 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5930 TREEVIEW_ITEM *root = infoPtr->root;
5932 assert(root != NULL);
5933 assert(root->iLevel == -1);
5934 assert(root->parent == NULL);
5935 assert(root->prevSibling == NULL);
5937 TREEVIEW_VerifyItemCommon(infoPtr, root);
5939 TREEVIEW_VerifyChildren(infoPtr, root);
5942 static void
5943 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5945 if (!TRACE_ON(treeview)) return;
5947 assert(infoPtr != NULL);
5948 TREEVIEW_VerifyRoot(infoPtr);