wined3d: Fix the VBO check in device_stream_info_from_declaration().
[wine/multimedia.git] / dlls / comctl32 / treeview.c
blob3f4fdc69a63805e31cde6ec33b698248c53995e9
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, TVN_SINGLEEXPAND
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
54 #define NONAMELESSSTRUCT
55 #include "windef.h"
56 #include "winbase.h"
57 #include "wingdi.h"
58 #include "winuser.h"
59 #include "winnls.h"
60 #include "commctrl.h"
61 #include "comctl32.h"
62 #include "uxtheme.h"
63 #include "vssym32.h"
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
69 /* internal structures */
70 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
72 HTREEITEM parent; /* handle to parent or 0 if at root */
73 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
74 HTREEITEM firstChild; /* handle to first child or 0 if no child */
76 UINT callbackMask;
77 UINT state;
78 UINT stateMask;
79 LPWSTR pszText;
80 int cchTextMax;
81 int iImage;
82 int iSelectedImage;
83 int iExpandedImage;
84 int cChildren;
85 LPARAM lParam;
86 int iIntegral; /* item height multiplier (1 is normal) */
87 int iLevel; /* indentation level:0=root level */
88 HTREEITEM lastChild;
89 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
90 RECT rect;
91 LONG linesOffset;
92 LONG stateOffset;
93 LONG imageOffset;
94 LONG textOffset;
95 LONG textWidth; /* horizontal text extent for pszText */
96 LONG visibleOrder; /* visible ordering, 0 is first visible item */
97 } TREEVIEW_ITEM;
100 typedef struct tagTREEVIEW_INFO
102 HWND hwnd;
103 HWND hwndNotify; /* Owner window to send notifications to */
104 DWORD dwStyle;
105 HTREEITEM root;
106 UINT uInternalStatus;
107 INT Timer;
108 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
109 INT cdmode; /* last custom draw setting */
110 UINT uScrollTime; /* max. time for scrolling in milliseconds */
111 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
113 UINT uItemHeight; /* item height */
114 BOOL bHeightSet;
116 LONG clientWidth; /* width of control window */
117 LONG clientHeight; /* height of control window */
119 LONG treeWidth; /* width of visible tree items */
120 LONG treeHeight; /* height of visible tree items */
122 UINT uIndent; /* indentation in pixels */
123 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
124 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
125 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
126 HTREEITEM editItem; /* item being edited with builtin edit box */
128 HTREEITEM firstVisible; /* handle to first visible item */
129 LONG maxVisibleOrder;
130 HTREEITEM dropItem; /* handle to item selected by drag cursor */
131 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
132 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
133 HIMAGELIST dragList; /* Bitmap of dragged item */
134 LONG scrollX;
135 COLORREF clrBk;
136 COLORREF clrText;
137 COLORREF clrLine;
138 COLORREF clrInsertMark;
139 HFONT hFont;
140 HFONT hDefaultFont;
141 HFONT hBoldFont;
142 HFONT hUnderlineFont;
143 HCURSOR hcurHand;
144 HWND hwndToolTip;
146 HWND hwndEdit;
147 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
148 BOOL bIgnoreEditKillFocus;
149 BOOL bLabelChanged;
151 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
152 HIMAGELIST himlNormal;
153 int normalImageHeight;
154 int normalImageWidth;
155 HIMAGELIST himlState;
156 int stateImageHeight;
157 int stateImageWidth;
158 HDPA items;
160 DWORD lastKeyPressTimestamp;
161 WPARAM charCode;
162 INT nSearchParamLength;
163 WCHAR szSearchParam[ MAX_PATH ];
164 } TREEVIEW_INFO;
167 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
168 #define KEY_DELAY 450
170 /* bitflags for infoPtr->uInternalStatus */
172 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
173 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
174 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
175 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
176 #define TV_RDRAG 0x10 /* ditto Rbutton */
177 #define TV_RDRAGGING 0x20
179 /* bitflags for infoPtr->timer */
181 #define TV_EDIT_TIMER 2
182 #define TV_EDIT_TIMER_SET 2
184 #define TEXT_CALLBACK_SIZE 260
186 #define TREEVIEW_LEFT_MARGIN 8
188 #define MINIMUM_INDENT 19
190 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
192 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
193 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
194 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
196 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
197 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
198 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
199 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
201 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
204 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
207 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
209 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
210 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
211 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
212 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
213 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
214 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
215 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
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 inline HFONT
309 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
311 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
312 return infoPtr->hUnderlineFont;
313 if (item->state & TVIS_BOLD)
314 return infoPtr->hBoldFont;
315 return infoPtr->hFont;
318 /* for trace/debugging purposes only */
319 static const char *
320 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
322 if (item == NULL) return "<null item>";
323 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
324 if (item->pszText == NULL) return "<null>";
325 return debugstr_w(item->pszText);
328 /* An item is not a child of itself. */
329 static BOOL
330 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
334 child = child->parent;
335 if (child == parent) return TRUE;
336 } while (child != NULL);
338 return FALSE;
342 /* Tree Traversal *******************************************************/
344 /***************************************************************************
345 * This method returns the last expanded sibling or child child item
346 * of a tree node
348 static TREEVIEW_ITEM *
349 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
351 if (!item) return NULL;
353 while (item->lastChild)
355 if (item->state & TVIS_EXPANDED)
356 item = item->lastChild;
357 else
358 break;
361 if (item == infoPtr->root)
362 return NULL;
364 return item;
367 /***************************************************************************
368 * This method returns the previous non-hidden item in the list not
369 * considering the tree hierarchy.
371 static TREEVIEW_ITEM *
372 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
374 if (tvItem->prevSibling)
376 /* This item has a prevSibling, get the last item in the sibling's tree. */
377 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
379 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
380 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
381 else
382 return upItem;
384 else
386 /* this item does not have a prevSibling, get the parent */
387 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
392 /***************************************************************************
393 * This method returns the next physical item in the treeview not
394 * considering the tree hierarchy.
396 static TREEVIEW_ITEM *
397 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
400 * If this item has children and is expanded, return the first child
402 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
404 return tvItem->firstChild;
409 * try to get the sibling
411 if (tvItem->nextSibling)
412 return tvItem->nextSibling;
415 * Otherwise, get the parent's sibling.
417 while (tvItem->parent)
419 tvItem = tvItem->parent;
421 if (tvItem->nextSibling)
422 return tvItem->nextSibling;
425 return NULL;
428 /***************************************************************************
429 * This method returns the nth item starting at the given item. It returns
430 * the last item (or first) we we run out of items.
432 * Will scroll backward if count is <0.
433 * forward if count is >0.
435 static TREEVIEW_ITEM *
436 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
437 LONG count)
439 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
440 TREEVIEW_ITEM *previousItem;
442 assert(item != NULL);
444 if (count > 0)
446 next_item = TREEVIEW_GetNextListItem;
448 else if (count < 0)
450 count = -count;
451 next_item = TREEVIEW_GetPrevListItem;
453 else
454 return item;
458 previousItem = item;
459 item = next_item(infoPtr, item);
461 } while (--count && item != NULL);
464 return item ? item : previousItem;
467 /* Notifications ************************************************************/
469 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
471 if (!infoPtr->bNtfUnicode) {
472 switch (code) {
473 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
474 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
475 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
476 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
477 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
478 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
479 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
480 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
481 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
482 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
483 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
484 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
487 return code;
490 static inline BOOL
491 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPNMHDR pnmh)
493 TRACE("wParam=%ld, lParam=%p\n", wParam, pnmh);
494 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, (LPARAM)pnmh);
497 static BOOL
498 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
500 NMHDR nmhdr;
501 HWND hwnd = infoPtr->hwnd;
503 TRACE("%d\n", code);
504 nmhdr.hwndFrom = hwnd;
505 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
506 nmhdr.code = get_notifycode(infoPtr, code);
508 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, &nmhdr);
511 static VOID
512 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
514 tvItem->mask = mask;
515 tvItem->hItem = item;
516 tvItem->state = item->state;
517 tvItem->stateMask = 0;
518 tvItem->iImage = item->iImage;
519 tvItem->iSelectedImage = item->iSelectedImage;
520 tvItem->cChildren = item->cChildren;
521 tvItem->lParam = item->lParam;
523 if(mask & TVIF_TEXT)
525 if (!infoPtr->bNtfUnicode)
527 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
528 tvItem->pszText = Alloc (tvItem->cchTextMax);
529 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
531 else
533 tvItem->cchTextMax = item->cchTextMax;
534 tvItem->pszText = item->pszText;
537 else
539 tvItem->cchTextMax = 0;
540 tvItem->pszText = NULL;
544 static BOOL
545 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
546 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
548 HWND hwnd = infoPtr->hwnd;
549 NMTREEVIEWW nmhdr;
550 BOOL ret;
552 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
553 code, action, oldItem, newItem);
555 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
557 nmhdr.hdr.hwndFrom = hwnd;
558 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
559 nmhdr.hdr.code = get_notifycode(infoPtr, code);
560 nmhdr.action = action;
562 if (oldItem)
563 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
565 if (newItem)
566 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
568 nmhdr.ptDrag.x = 0;
569 nmhdr.ptDrag.y = 0;
571 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
572 if (!infoPtr->bNtfUnicode)
574 Free(nmhdr.itemOld.pszText);
575 Free(nmhdr.itemNew.pszText);
577 return ret;
580 static BOOL
581 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
582 HTREEITEM dragItem, POINT pt)
584 HWND hwnd = infoPtr->hwnd;
585 NMTREEVIEWW nmhdr;
587 TRACE("code:%d dragitem:%p\n", code, dragItem);
589 nmhdr.hdr.hwndFrom = hwnd;
590 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
591 nmhdr.hdr.code = get_notifycode(infoPtr, code);
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, nmhdr.hdr.idFrom, &nmhdr.hdr);
605 static BOOL
606 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
607 HDC hdc, RECT rc)
609 HWND hwnd = infoPtr->hwnd;
610 NMTVCUSTOMDRAW nmcdhdr;
611 LPNMCUSTOMDRAW nmcd;
613 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
615 nmcd = &nmcdhdr.nmcd;
616 nmcd->hdr.hwndFrom = hwnd;
617 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
618 nmcd->hdr.code = NM_CUSTOMDRAW;
619 nmcd->dwDrawStage = dwDrawStage;
620 nmcd->hdc = hdc;
621 nmcd->rc = rc;
622 nmcd->dwItemSpec = 0;
623 nmcd->uItemState = 0;
624 nmcd->lItemlParam = 0;
625 nmcdhdr.clrText = infoPtr->clrText;
626 nmcdhdr.clrTextBk = infoPtr->clrBk;
627 nmcdhdr.iLevel = 0;
629 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr.nmcd.hdr);
634 /* FIXME: need to find out when the flags in uItemState need to be set */
636 static BOOL
637 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
638 TREEVIEW_ITEM *item, UINT uItemDrawState,
639 NMTVCUSTOMDRAW *nmcdhdr)
641 HWND hwnd = infoPtr->hwnd;
642 LPNMCUSTOMDRAW nmcd;
643 DWORD dwDrawStage;
644 DWORD_PTR dwItemSpec;
645 UINT uItemState;
647 dwDrawStage = CDDS_ITEM | uItemDrawState;
648 dwItemSpec = (DWORD_PTR)item;
649 uItemState = 0;
650 if (item->state & TVIS_SELECTED)
651 uItemState |= CDIS_SELECTED;
652 if (item == infoPtr->selectedItem)
653 uItemState |= CDIS_FOCUS;
654 if (item == infoPtr->hotItem)
655 uItemState |= CDIS_HOT;
657 nmcd = &nmcdhdr->nmcd;
658 nmcd->hdr.hwndFrom = hwnd;
659 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
660 nmcd->hdr.code = NM_CUSTOMDRAW;
661 nmcd->dwDrawStage = dwDrawStage;
662 nmcd->hdc = hdc;
663 nmcd->rc = item->rect;
664 nmcd->dwItemSpec = dwItemSpec;
665 nmcd->uItemState = uItemState;
666 nmcd->lItemlParam = item->lParam;
667 nmcdhdr->iLevel = item->iLevel;
669 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
670 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
671 nmcd->uItemState, nmcd->lItemlParam);
673 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr->nmcd.hdr);
676 static BOOL
677 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
679 HWND hwnd = infoPtr->hwnd;
680 NMTVDISPINFOW tvdi;
681 BOOL ret;
683 tvdi.hdr.hwndFrom = hwnd;
684 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
685 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
687 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
688 &tvdi.item, editItem);
690 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
692 if (!infoPtr->bNtfUnicode)
693 Free(tvdi.item.pszText);
695 return ret;
698 static void
699 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
700 UINT mask)
702 NMTVDISPINFOEXW callback;
703 HWND hwnd = infoPtr->hwnd;
705 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
706 mask &= item->callbackMask;
708 if (mask == 0) return;
710 callback.hdr.hwndFrom = hwnd;
711 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
712 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
714 /* 'state' always contains valid value, as well as 'lParam'.
715 * All other parameters are uninitialized.
717 callback.item.pszText = item->pszText;
718 callback.item.cchTextMax = item->cchTextMax;
719 callback.item.mask = mask;
720 callback.item.hItem = item;
721 callback.item.state = item->state;
722 callback.item.lParam = item->lParam;
724 /* If text is changed we need to recalculate textWidth */
725 if (mask & TVIF_TEXT)
726 item->textWidth = 0;
728 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, &callback.hdr);
729 TRACE("resulting code 0x%08x\n", callback.hdr.code);
731 /* It may have changed due to a call to SetItem. */
732 mask &= item->callbackMask;
734 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
736 /* Instead of copying text into our buffer user specified his own */
737 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
738 LPWSTR newText;
739 int buflen;
740 int len = MultiByteToWideChar( CP_ACP, 0,
741 (LPSTR)callback.item.pszText, -1,
742 NULL, 0);
743 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
744 newText = ReAlloc(item->pszText, buflen);
746 TRACE("returned str %s, len=%d, buflen=%d\n",
747 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
749 if (newText)
751 item->pszText = newText;
752 MultiByteToWideChar( CP_ACP, 0,
753 (LPSTR)callback.item.pszText, -1,
754 item->pszText, buflen/sizeof(WCHAR));
755 item->cchTextMax = buflen/sizeof(WCHAR);
757 /* If ReAlloc fails we have nothing to do, but keep original text */
759 else {
760 int len = max(lstrlenW(callback.item.pszText) + 1,
761 TEXT_CALLBACK_SIZE);
762 LPWSTR newText = ReAlloc(item->pszText, len);
764 TRACE("returned wstr %s, len=%d\n",
765 debugstr_w(callback.item.pszText), len);
767 if (newText)
769 item->pszText = newText;
770 strcpyW(item->pszText, callback.item.pszText);
771 item->cchTextMax = len;
773 /* If ReAlloc fails we have nothing to do, but keep original text */
776 else if (mask & TVIF_TEXT) {
777 /* User put text into our buffer, that is ok unless A string */
778 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
779 LPWSTR newText;
780 int buflen;
781 int len = MultiByteToWideChar( CP_ACP, 0,
782 (LPSTR)callback.item.pszText, -1,
783 NULL, 0);
784 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
785 newText = Alloc(buflen);
787 TRACE("same buffer str %s, len=%d, buflen=%d\n",
788 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
790 if (newText)
792 LPWSTR oldText = item->pszText;
793 item->pszText = newText;
794 MultiByteToWideChar( CP_ACP, 0,
795 (LPSTR)callback.item.pszText, -1,
796 item->pszText, buflen/sizeof(WCHAR));
797 item->cchTextMax = buflen/sizeof(WCHAR);
798 Free(oldText);
803 if (mask & TVIF_IMAGE)
804 item->iImage = callback.item.iImage;
806 if (mask & TVIF_SELECTEDIMAGE)
807 item->iSelectedImage = callback.item.iSelectedImage;
809 if (mask & TVIF_EXPANDEDIMAGE)
810 item->iExpandedImage = callback.item.iExpandedImage;
812 if (mask & TVIF_CHILDREN)
813 item->cChildren = callback.item.cChildren;
815 if (callback.item.mask & TVIF_STATE)
817 item->state &= ~callback.item.stateMask;
818 item->state |= (callback.item.state & callback.item.stateMask);
821 /* These members are now permanently set. */
822 if (callback.item.mask & TVIF_DI_SETITEM)
823 item->callbackMask &= ~callback.item.mask;
826 /***************************************************************************
827 * This function uses cChildren field to decide whether the item has
828 * children or not.
829 * Note: if this returns TRUE, the child items may not actually exist,
830 * they could be virtual.
832 * Just use item->firstChild to check for physical children.
834 static BOOL
835 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
837 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
839 return item->cChildren > 0;
842 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
844 INT format;
846 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
848 if (nCommand != NF_REQUERY) return 0;
850 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
851 TRACE("format=%d\n", format);
853 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
855 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
857 return format;
860 /* Item Position ********************************************************/
862 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
863 static VOID
864 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
866 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
867 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
868 > TVS_LINESATROOT);
870 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
871 - infoPtr->scrollX;
872 item->stateOffset = item->linesOffset + infoPtr->uIndent;
873 item->imageOffset = item->stateOffset
874 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
875 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
878 static VOID
879 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
881 HDC hdc;
882 HFONT hOldFont=0;
883 SIZE sz;
885 /* DRAW's OM docker creates items like this */
886 if (item->pszText == NULL)
888 item->textWidth = 0;
889 return;
892 if (hDC != 0)
894 hdc = hDC;
896 else
898 hdc = GetDC(infoPtr->hwnd);
899 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
902 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
903 item->textWidth = sz.cx;
905 if (hDC == 0)
907 SelectObject(hdc, hOldFont);
908 ReleaseDC(0, hdc);
912 static VOID
913 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
915 item->rect.top = infoPtr->uItemHeight *
916 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
918 item->rect.bottom = item->rect.top
919 + infoPtr->uItemHeight * item->iIntegral - 1;
921 item->rect.left = 0;
922 item->rect.right = infoPtr->clientWidth;
925 /* We know that only items after start need their order updated. */
926 static void
927 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
929 TREEVIEW_ITEM *item;
930 int order;
932 if (!start)
934 start = infoPtr->root->firstChild;
935 order = 0;
937 else
938 order = start->visibleOrder;
940 for (item = start; item != NULL;
941 item = TREEVIEW_GetNextListItem(infoPtr, item))
943 if (!ISVISIBLE(item) && order > 0)
944 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
945 item->visibleOrder = order;
946 order += item->iIntegral;
949 infoPtr->maxVisibleOrder = order;
951 for (item = start; item != NULL;
952 item = TREEVIEW_GetNextListItem(infoPtr, item))
954 TREEVIEW_ComputeItemRect(infoPtr, item);
959 /* Update metrics of all items in selected subtree.
960 * root must be expanded
962 static VOID
963 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
965 TREEVIEW_ITEM *sibling;
966 HDC hdc;
967 HFONT hOldFont;
969 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
970 return;
972 root->state &= ~TVIS_EXPANDED;
973 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
974 root->state |= TVIS_EXPANDED;
976 hdc = GetDC(infoPtr->hwnd);
977 hOldFont = SelectObject(hdc, infoPtr->hFont);
979 for (; root != sibling;
980 root = TREEVIEW_GetNextListItem(infoPtr, root))
982 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
984 if (root->callbackMask & TVIF_TEXT)
985 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
987 if (root->textWidth == 0)
989 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
990 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
994 SelectObject(hdc, hOldFont);
995 ReleaseDC(infoPtr->hwnd, hdc);
998 /* Item Allocation **********************************************************/
1000 static TREEVIEW_ITEM *
1001 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1003 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1005 if (!newItem)
1006 return NULL;
1008 /* I_IMAGENONE would make more sense but this is neither what is
1009 * documented (MSDN doesn't specify) nor what Windows actually does
1010 * (it sets it to zero)... and I can so imagine an application using
1011 * inc/dec to toggle the images. */
1012 newItem->iImage = 0;
1013 newItem->iSelectedImage = 0;
1014 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1016 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1018 Free(newItem);
1019 return NULL;
1022 return newItem;
1025 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1026 * free item->pszText. */
1027 static void
1028 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1030 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1031 if (infoPtr->selectedItem == item)
1032 infoPtr->selectedItem = NULL;
1033 if (infoPtr->hotItem == item)
1034 infoPtr->hotItem = NULL;
1035 if (infoPtr->focusedItem == item)
1036 infoPtr->focusedItem = NULL;
1037 if (infoPtr->firstVisible == item)
1038 infoPtr->firstVisible = NULL;
1039 if (infoPtr->dropItem == item)
1040 infoPtr->dropItem = NULL;
1041 if (infoPtr->insertMarkItem == item)
1042 infoPtr->insertMarkItem = NULL;
1043 Free(item);
1047 /* Item Insertion *******************************************************/
1049 /***************************************************************************
1050 * This method inserts newItem before sibling as a child of parent.
1051 * sibling can be NULL, but only if parent has no children.
1053 static void
1054 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1055 TREEVIEW_ITEM *parent)
1057 assert(parent != NULL);
1059 if (sibling != NULL)
1061 assert(sibling->parent == parent);
1063 if (sibling->prevSibling != NULL)
1064 sibling->prevSibling->nextSibling = newItem;
1066 newItem->prevSibling = sibling->prevSibling;
1067 sibling->prevSibling = newItem;
1069 else
1070 newItem->prevSibling = NULL;
1072 newItem->nextSibling = sibling;
1074 if (parent->firstChild == sibling)
1075 parent->firstChild = newItem;
1077 if (parent->lastChild == NULL)
1078 parent->lastChild = newItem;
1081 /***************************************************************************
1082 * This method inserts newItem after sibling as a child of parent.
1083 * sibling can be NULL, but only if parent has no children.
1085 static void
1086 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1087 TREEVIEW_ITEM *parent)
1089 assert(parent != NULL);
1091 if (sibling != NULL)
1093 assert(sibling->parent == parent);
1095 if (sibling->nextSibling != NULL)
1096 sibling->nextSibling->prevSibling = newItem;
1098 newItem->nextSibling = sibling->nextSibling;
1099 sibling->nextSibling = newItem;
1101 else
1102 newItem->nextSibling = NULL;
1104 newItem->prevSibling = sibling;
1106 if (parent->lastChild == sibling)
1107 parent->lastChild = newItem;
1109 if (parent->firstChild == NULL)
1110 parent->firstChild = newItem;
1113 static BOOL
1114 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1115 const TVITEMEXW *tvItem, BOOL isW)
1117 UINT callbackClear = 0;
1118 UINT callbackSet = 0;
1120 TRACE("item %p\n", item);
1121 /* Do this first in case it fails. */
1122 if (tvItem->mask & TVIF_TEXT)
1124 item->textWidth = 0; /* force width recalculation */
1125 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1127 int len;
1128 LPWSTR newText;
1129 if (isW)
1130 len = lstrlenW(tvItem->pszText) + 1;
1131 else
1132 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1134 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1136 if (newText == NULL) return FALSE;
1138 callbackClear |= TVIF_TEXT;
1140 item->pszText = newText;
1141 item->cchTextMax = len;
1142 if (isW)
1143 lstrcpynW(item->pszText, tvItem->pszText, len);
1144 else
1145 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1146 item->pszText, len);
1148 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1150 else
1152 callbackSet |= TVIF_TEXT;
1154 item->pszText = ReAlloc(item->pszText,
1155 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1156 item->cchTextMax = TEXT_CALLBACK_SIZE;
1157 TRACE("setting callback, item %p\n", item);
1161 if (tvItem->mask & TVIF_CHILDREN)
1163 item->cChildren = tvItem->cChildren;
1165 if (item->cChildren == I_CHILDRENCALLBACK)
1166 callbackSet |= TVIF_CHILDREN;
1167 else
1168 callbackClear |= TVIF_CHILDREN;
1171 if (tvItem->mask & TVIF_IMAGE)
1173 item->iImage = tvItem->iImage;
1175 if (item->iImage == I_IMAGECALLBACK)
1176 callbackSet |= TVIF_IMAGE;
1177 else
1178 callbackClear |= TVIF_IMAGE;
1181 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1183 item->iSelectedImage = tvItem->iSelectedImage;
1185 if (item->iSelectedImage == I_IMAGECALLBACK)
1186 callbackSet |= TVIF_SELECTEDIMAGE;
1187 else
1188 callbackClear |= TVIF_SELECTEDIMAGE;
1191 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1193 item->iExpandedImage = tvItem->iExpandedImage;
1195 if (item->iExpandedImage == I_IMAGECALLBACK)
1196 callbackSet |= TVIF_EXPANDEDIMAGE;
1197 else
1198 callbackClear |= TVIF_EXPANDEDIMAGE;
1201 if (tvItem->mask & TVIF_PARAM)
1202 item->lParam = tvItem->lParam;
1204 /* If the application sets TVIF_INTEGRAL without
1205 * supplying a TVITEMEX structure, it's toast. */
1206 if (tvItem->mask & TVIF_INTEGRAL)
1207 item->iIntegral = tvItem->iIntegral;
1209 if (tvItem->mask & TVIF_STATE)
1211 TRACE("prevstate,state,mask:%x,%x,%x\n", item->state, tvItem->state,
1212 tvItem->stateMask);
1213 item->state &= ~tvItem->stateMask;
1214 item->state |= (tvItem->state & tvItem->stateMask);
1217 if (tvItem->mask & TVIF_STATEEX)
1219 FIXME("New extended state: %x\n", tvItem->uStateEx);
1222 item->callbackMask |= callbackSet;
1223 item->callbackMask &= ~callbackClear;
1225 return TRUE;
1228 /* Note that the new item is pre-zeroed. */
1229 static LRESULT
1230 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1232 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1233 HTREEITEM insertAfter;
1234 TREEVIEW_ITEM *newItem, *parentItem;
1235 BOOL bTextUpdated = FALSE;
1237 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1239 parentItem = infoPtr->root;
1241 else
1243 parentItem = ptdi->hParent;
1245 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1247 WARN("invalid parent %p\n", parentItem);
1248 return 0;
1252 insertAfter = ptdi->hInsertAfter;
1254 /* Validate this now for convenience. */
1255 switch ((DWORD_PTR)insertAfter)
1257 case (DWORD_PTR)TVI_FIRST:
1258 case (DWORD_PTR)TVI_LAST:
1259 case (DWORD_PTR)TVI_SORT:
1260 break;
1262 default:
1263 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1264 insertAfter->parent != parentItem)
1266 WARN("invalid insert after %p\n", insertAfter);
1267 insertAfter = TVI_LAST;
1271 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1272 (tvItem->mask & TVIF_TEXT)
1273 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1274 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1275 : "<no label>");
1277 newItem = TREEVIEW_AllocateItem(infoPtr);
1278 if (newItem == NULL)
1279 return 0;
1281 newItem->parent = parentItem;
1282 newItem->iIntegral = 1;
1283 newItem->visibleOrder = -1;
1285 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1286 return 0;
1288 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1290 infoPtr->uNumItems++;
1292 switch ((DWORD_PTR)insertAfter)
1294 case (DWORD_PTR)TVI_FIRST:
1296 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1297 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1298 if (infoPtr->firstVisible == originalFirst)
1299 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1301 break;
1303 case (DWORD_PTR)TVI_LAST:
1304 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1305 break;
1307 /* hInsertAfter names a specific item we want to insert after */
1308 default:
1309 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1310 break;
1312 case (DWORD_PTR)TVI_SORT:
1314 TREEVIEW_ITEM *aChild;
1315 TREEVIEW_ITEM *previousChild = NULL;
1316 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1317 BOOL bItemInserted = FALSE;
1319 aChild = parentItem->firstChild;
1321 bTextUpdated = TRUE;
1322 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1324 /* Iterate the parent children to see where we fit in */
1325 while (aChild != NULL)
1327 INT comp;
1329 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1330 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1332 if (comp < 0) /* we are smaller than the current one */
1334 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1335 if (infoPtr->firstVisible == originalFirst &&
1336 aChild == originalFirst)
1337 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1338 bItemInserted = TRUE;
1339 break;
1341 else if (comp > 0) /* we are bigger than the current one */
1343 previousChild = aChild;
1345 /* This will help us to exit if there is no more sibling */
1346 aChild = (aChild->nextSibling == 0)
1347 ? NULL
1348 : aChild->nextSibling;
1350 /* Look at the next item */
1351 continue;
1353 else if (comp == 0)
1356 * An item with this name is already existing, therefore,
1357 * we add after the one we found
1359 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1360 bItemInserted = TRUE;
1361 break;
1366 * we reach the end of the child list and the item has not
1367 * yet been inserted, therefore, insert it after the last child.
1369 if ((!bItemInserted) && (aChild == NULL))
1370 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1372 break;
1377 TRACE("new item %p; parent %p, mask %x\n", newItem,
1378 newItem->parent, tvItem->mask);
1380 newItem->iLevel = newItem->parent->iLevel + 1;
1382 if (newItem->parent->cChildren == 0)
1383 newItem->parent->cChildren = 1;
1385 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1387 if (STATEIMAGEINDEX(newItem->state) == 0)
1388 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1391 if (infoPtr->firstVisible == NULL)
1392 infoPtr->firstVisible = newItem;
1394 TREEVIEW_VerifyTree(infoPtr);
1396 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1398 if (parentItem == infoPtr->root ||
1399 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1401 TREEVIEW_ITEM *item;
1402 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1404 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1405 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1407 if (!bTextUpdated)
1408 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1410 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1411 TREEVIEW_UpdateScrollBars(infoPtr);
1413 * if the item was inserted in a visible part of the tree,
1414 * invalidate it, as well as those after it
1416 for (item = newItem;
1417 item != NULL;
1418 item = TREEVIEW_GetNextListItem(infoPtr, item))
1419 TREEVIEW_Invalidate(infoPtr, item);
1421 else
1423 /* refresh treeview if newItem is the first item inserted under parentItem */
1424 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1426 /* parent got '+' - update it */
1427 TREEVIEW_Invalidate(infoPtr, parentItem);
1431 return (LRESULT)newItem;
1434 /* Item Deletion ************************************************************/
1435 static void
1436 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1438 static void
1439 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1441 TREEVIEW_ITEM *kill = parentItem->firstChild;
1443 while (kill != NULL)
1445 TREEVIEW_ITEM *next = kill->nextSibling;
1447 TREEVIEW_RemoveItem(infoPtr, kill);
1449 kill = next;
1452 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1453 assert(parentItem->firstChild == NULL);
1454 assert(parentItem->lastChild == NULL);
1457 static void
1458 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1460 TREEVIEW_ITEM *parentItem = item->parent;
1462 assert(item != NULL);
1463 assert(item->parent != NULL); /* i.e. it must not be the root */
1465 if (parentItem->firstChild == item)
1466 parentItem->firstChild = item->nextSibling;
1468 if (parentItem->lastChild == item)
1469 parentItem->lastChild = item->prevSibling;
1471 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1472 && parentItem->cChildren > 0)
1473 parentItem->cChildren = 0;
1475 if (item->prevSibling)
1476 item->prevSibling->nextSibling = item->nextSibling;
1478 if (item->nextSibling)
1479 item->nextSibling->prevSibling = item->prevSibling;
1482 static void
1483 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1485 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1487 if (item->firstChild)
1488 TREEVIEW_RemoveAllChildren(infoPtr, item);
1490 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1491 TVIF_HANDLE | TVIF_PARAM, item, 0);
1493 TREEVIEW_UnlinkItem(item);
1495 infoPtr->uNumItems--;
1497 if (item->pszText != LPSTR_TEXTCALLBACKW)
1498 Free(item->pszText);
1500 TREEVIEW_FreeItem(infoPtr, item);
1504 /* Empty out the tree. */
1505 static void
1506 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1508 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1510 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1513 static LRESULT
1514 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1516 TREEVIEW_ITEM *newSelection = NULL;
1517 TREEVIEW_ITEM *newFirstVisible = NULL;
1518 TREEVIEW_ITEM *parent, *prev = NULL;
1519 BOOL visible = FALSE;
1521 if (item == TVI_ROOT || !item)
1523 TRACE("TVI_ROOT\n");
1524 parent = infoPtr->root;
1525 newSelection = NULL;
1526 visible = TRUE;
1527 TREEVIEW_RemoveTree(infoPtr);
1529 else
1531 if (!TREEVIEW_ValidItem(infoPtr, item))
1532 return FALSE;
1534 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1535 parent = item->parent;
1537 if (ISVISIBLE(item))
1539 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1540 visible = TRUE;
1543 if (infoPtr->selectedItem != NULL
1544 && (item == infoPtr->selectedItem
1545 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1547 if (item->nextSibling)
1548 newSelection = item->nextSibling;
1549 else if (item->parent != infoPtr->root)
1550 newSelection = item->parent;
1551 else
1552 newSelection = item->prevSibling;
1553 TRACE("newSelection = %p\n", newSelection);
1556 if (infoPtr->firstVisible == item)
1558 if (item->nextSibling)
1559 newFirstVisible = item->nextSibling;
1560 else if (item->prevSibling)
1561 newFirstVisible = item->prevSibling;
1562 else if (item->parent != infoPtr->root)
1563 newFirstVisible = item->parent;
1564 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1566 else
1567 newFirstVisible = infoPtr->firstVisible;
1569 TREEVIEW_RemoveItem(infoPtr, item);
1572 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1573 if (!infoPtr->selectedItem && newSelection)
1575 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1576 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1579 /* Validate insertMark dropItem.
1580 * hotItem ??? - used for comparison only.
1582 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1583 infoPtr->insertMarkItem = 0;
1585 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1586 infoPtr->dropItem = 0;
1588 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1589 newFirstVisible = infoPtr->root->firstChild;
1591 TREEVIEW_VerifyTree(infoPtr);
1593 if (!infoPtr->bRedraw) return TRUE;
1595 if (visible)
1597 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1598 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1599 TREEVIEW_UpdateScrollBars(infoPtr);
1600 TREEVIEW_Invalidate(infoPtr, NULL);
1602 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1604 /* parent lost '+/-' - update it */
1605 TREEVIEW_Invalidate(infoPtr, parent);
1608 return TRUE;
1612 /* Get/Set Messages *********************************************************/
1613 static LRESULT
1614 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1616 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1618 if (infoPtr->bRedraw)
1620 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1621 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1622 TREEVIEW_UpdateScrollBars(infoPtr);
1623 TREEVIEW_Invalidate(infoPtr, NULL);
1625 return 0;
1628 static LRESULT
1629 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1631 TRACE("\n");
1632 return infoPtr->uIndent;
1635 static LRESULT
1636 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1638 TRACE("\n");
1640 if (newIndent < MINIMUM_INDENT)
1641 newIndent = MINIMUM_INDENT;
1643 if (infoPtr->uIndent != newIndent)
1645 infoPtr->uIndent = newIndent;
1646 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1647 TREEVIEW_UpdateScrollBars(infoPtr);
1648 TREEVIEW_Invalidate(infoPtr, NULL);
1651 return 0;
1655 static LRESULT
1656 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1658 TRACE("\n");
1659 return (LRESULT)infoPtr->hwndToolTip;
1662 static LRESULT
1663 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1665 HWND prevToolTip;
1667 TRACE("\n");
1668 prevToolTip = infoPtr->hwndToolTip;
1669 infoPtr->hwndToolTip = hwndTT;
1671 return (LRESULT)prevToolTip;
1674 static LRESULT
1675 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1677 BOOL rc = infoPtr->bNtfUnicode;
1678 infoPtr->bNtfUnicode = fUnicode;
1679 return rc;
1682 static LRESULT
1683 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1685 return infoPtr->bNtfUnicode;
1688 static LRESULT
1689 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1691 return infoPtr->uScrollTime;
1694 static LRESULT
1695 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1697 UINT uOldScrollTime = infoPtr->uScrollTime;
1699 infoPtr->uScrollTime = min(uScrollTime, 100);
1701 return uOldScrollTime;
1705 static LRESULT
1706 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1708 TRACE("\n");
1710 switch (wParam)
1712 case TVSIL_NORMAL:
1713 return (LRESULT)infoPtr->himlNormal;
1715 case TVSIL_STATE:
1716 return (LRESULT)infoPtr->himlState;
1718 default:
1719 return 0;
1723 #define TVHEIGHT_MIN 16
1724 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1726 /* Compute the natural height for items. */
1727 static UINT
1728 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1730 TEXTMETRICW tm;
1731 HDC hdc = GetDC(0);
1732 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1733 UINT height;
1735 /* Height is the maximum of:
1736 * 16 (a hack because our fonts are tiny), and
1737 * The text height + border & margin, and
1738 * The size of the normal image list
1740 GetTextMetricsW(hdc, &tm);
1741 SelectObject(hdc, hOldFont);
1742 ReleaseDC(0, hdc);
1744 height = TVHEIGHT_MIN;
1745 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1746 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1747 if (height < infoPtr->normalImageHeight)
1748 height = infoPtr->normalImageHeight;
1750 /* Round down, unless we support odd ("non even") heights. */
1751 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1752 height &= ~1;
1754 return height;
1757 static LRESULT
1758 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1760 HIMAGELIST himlOld = 0;
1761 int oldWidth = infoPtr->normalImageWidth;
1762 int oldHeight = infoPtr->normalImageHeight;
1764 TRACE("%u,%p\n", type, himlNew);
1766 switch (type)
1768 case TVSIL_NORMAL:
1769 himlOld = infoPtr->himlNormal;
1770 infoPtr->himlNormal = himlNew;
1772 if (himlNew)
1773 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1774 &infoPtr->normalImageHeight);
1775 else
1777 infoPtr->normalImageWidth = 0;
1778 infoPtr->normalImageHeight = 0;
1781 break;
1783 case TVSIL_STATE:
1784 himlOld = infoPtr->himlState;
1785 infoPtr->himlState = himlNew;
1787 if (himlNew)
1788 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1789 &infoPtr->stateImageHeight);
1790 else
1792 infoPtr->stateImageWidth = 0;
1793 infoPtr->stateImageHeight = 0;
1796 break;
1798 default:
1799 ERR("unknown imagelist type %u\n", type);
1802 if (oldWidth != infoPtr->normalImageWidth ||
1803 oldHeight != infoPtr->normalImageHeight)
1805 BOOL bRecalcVisible = FALSE;
1807 if (oldHeight != infoPtr->normalImageHeight &&
1808 !infoPtr->bHeightSet)
1810 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1811 bRecalcVisible = TRUE;
1814 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1815 infoPtr->normalImageWidth != infoPtr->uIndent)
1817 infoPtr->uIndent = infoPtr->normalImageWidth;
1818 bRecalcVisible = TRUE;
1821 if (bRecalcVisible)
1822 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1824 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1825 TREEVIEW_UpdateScrollBars(infoPtr);
1828 TREEVIEW_Invalidate(infoPtr, NULL);
1830 return (LRESULT)himlOld;
1833 static LRESULT
1834 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1836 INT prevHeight = infoPtr->uItemHeight;
1838 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1839 if (newHeight == -1)
1841 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1842 infoPtr->bHeightSet = FALSE;
1844 else
1846 if (newHeight == 0) newHeight = 1;
1847 infoPtr->uItemHeight = newHeight;
1848 infoPtr->bHeightSet = TRUE;
1851 /* Round down, unless we support odd ("non even") heights. */
1852 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1854 infoPtr->uItemHeight &= ~1;
1855 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1858 if (infoPtr->uItemHeight != prevHeight)
1860 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1861 TREEVIEW_UpdateScrollBars(infoPtr);
1862 TREEVIEW_Invalidate(infoPtr, NULL);
1865 return prevHeight;
1868 static LRESULT
1869 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1871 TRACE("\n");
1872 return infoPtr->uItemHeight;
1876 static LRESULT
1877 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1879 TRACE("%p\n", infoPtr->hFont);
1880 return (LRESULT)infoPtr->hFont;
1884 static INT CALLBACK
1885 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1887 (void)unused;
1889 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1891 return 1;
1894 static LRESULT
1895 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1897 UINT uHeight = infoPtr->uItemHeight;
1899 TRACE("%p %i\n", hFont, bRedraw);
1901 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1903 DeleteObject(infoPtr->hBoldFont);
1904 DeleteObject(infoPtr->hUnderlineFont);
1905 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1906 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1908 if (!infoPtr->bHeightSet)
1909 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1911 if (uHeight != infoPtr->uItemHeight)
1912 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1914 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1916 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1917 TREEVIEW_UpdateScrollBars(infoPtr);
1919 if (bRedraw)
1920 TREEVIEW_Invalidate(infoPtr, NULL);
1922 return 0;
1926 static LRESULT
1927 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1929 TRACE("\n");
1930 return (LRESULT)infoPtr->clrLine;
1933 static LRESULT
1934 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1936 COLORREF prevColor = infoPtr->clrLine;
1938 TRACE("\n");
1939 infoPtr->clrLine = color;
1940 return (LRESULT)prevColor;
1944 static LRESULT
1945 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1947 TRACE("\n");
1948 return (LRESULT)infoPtr->clrText;
1951 static LRESULT
1952 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1954 COLORREF prevColor = infoPtr->clrText;
1956 TRACE("\n");
1957 infoPtr->clrText = color;
1959 if (infoPtr->clrText != prevColor)
1960 TREEVIEW_Invalidate(infoPtr, NULL);
1962 return (LRESULT)prevColor;
1966 static LRESULT
1967 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1969 TRACE("\n");
1970 return (LRESULT)infoPtr->clrBk;
1973 static LRESULT
1974 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1976 COLORREF prevColor = infoPtr->clrBk;
1978 TRACE("\n");
1979 infoPtr->clrBk = newColor;
1981 if (newColor != prevColor)
1982 TREEVIEW_Invalidate(infoPtr, NULL);
1984 return (LRESULT)prevColor;
1988 static LRESULT
1989 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1991 TRACE("\n");
1992 return (LRESULT)infoPtr->clrInsertMark;
1995 static LRESULT
1996 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1998 COLORREF prevColor = infoPtr->clrInsertMark;
2000 TRACE("%x\n", color);
2001 infoPtr->clrInsertMark = color;
2003 return (LRESULT)prevColor;
2007 static LRESULT
2008 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2010 TRACE("%d %p\n", wParam, item);
2012 if (!TREEVIEW_ValidItem(infoPtr, item))
2013 return 0;
2015 infoPtr->insertBeforeorAfter = wParam;
2016 infoPtr->insertMarkItem = item;
2018 TREEVIEW_Invalidate(infoPtr, NULL);
2020 return 1;
2024 /************************************************************************
2025 * Some serious braindamage here. lParam is a pointer to both the
2026 * input HTREEITEM and the output RECT.
2028 static LRESULT
2029 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2031 TREEVIEW_ITEM *item;
2032 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2034 TRACE("\n");
2036 if (pItem == NULL)
2037 return FALSE;
2039 item = *pItem;
2040 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2041 return FALSE;
2044 * If wParam is TRUE return the text size otherwise return
2045 * the whole item size
2047 if (fTextRect)
2049 /* Windows does not send TVN_GETDISPINFO here. */
2051 lpRect->top = item->rect.top;
2052 lpRect->bottom = item->rect.bottom;
2054 lpRect->left = item->textOffset;
2055 if (!item->textWidth)
2056 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2058 lpRect->right = item->textOffset + item->textWidth + 4;
2060 else
2062 *lpRect = item->rect;
2065 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2067 return TRUE;
2070 static inline LRESULT
2071 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2073 /* Surprise! This does not take integral height into account. */
2074 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2075 return infoPtr->clientHeight / infoPtr->uItemHeight;
2079 static LRESULT
2080 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2082 TREEVIEW_ITEM *item = tvItem->hItem;
2084 if (!TREEVIEW_ValidItem(infoPtr, item))
2085 return FALSE;
2087 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2089 if (tvItem->mask & TVIF_CHILDREN)
2091 if (item->cChildren==I_CHILDRENCALLBACK)
2092 FIXME("I_CHILDRENCALLBACK not supported\n");
2093 tvItem->cChildren = item->cChildren;
2096 if (tvItem->mask & TVIF_HANDLE)
2097 tvItem->hItem = item;
2099 if (tvItem->mask & TVIF_IMAGE)
2100 tvItem->iImage = item->iImage;
2102 if (tvItem->mask & TVIF_INTEGRAL)
2103 tvItem->iIntegral = item->iIntegral;
2105 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2106 tvItem->lParam = item->lParam;
2108 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2109 tvItem->iSelectedImage = item->iSelectedImage;
2111 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2112 tvItem->iExpandedImage = item->iExpandedImage;
2114 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2115 tvItem->state = item->state;
2117 if (tvItem->mask & TVIF_TEXT)
2119 if (item->pszText == NULL)
2121 if (tvItem->cchTextMax > 0)
2122 tvItem->pszText[0] = '\0';
2124 else if (isW)
2126 if (item->pszText == LPSTR_TEXTCALLBACKW)
2128 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2129 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2131 else
2133 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2136 else
2138 if (item->pszText == LPSTR_TEXTCALLBACKW)
2140 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2141 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2143 else
2145 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2146 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2151 if (tvItem->mask & TVIF_STATEEX)
2153 FIXME("Extended item state not supported, returning 0.\n");
2154 tvItem->uStateEx = 0;
2157 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2158 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2160 return TRUE;
2163 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2164 * which is wrong. */
2165 static LRESULT
2166 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2168 TREEVIEW_ITEM *item;
2169 TREEVIEW_ITEM originalItem;
2171 item = tvItem->hItem;
2173 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2174 tvItem->mask);
2176 if (!TREEVIEW_ValidItem(infoPtr, item))
2177 return FALSE;
2179 /* store the original item values */
2180 originalItem = *item;
2182 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2183 return FALSE;
2185 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2186 if ((tvItem->mask & TVIF_TEXT
2187 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2188 && ISVISIBLE(item))
2190 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2191 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2194 if (tvItem->mask != 0 && ISVISIBLE(item))
2196 /* The refresh updates everything, but we can't wait until then. */
2197 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2199 /* if any of the item's values changed and it's not a callback, redraw the item */
2200 if (item_changed(&originalItem, item, tvItem))
2202 if (tvItem->mask & TVIF_INTEGRAL)
2204 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2205 TREEVIEW_UpdateScrollBars(infoPtr);
2207 TREEVIEW_Invalidate(infoPtr, NULL);
2209 else
2211 TREEVIEW_UpdateScrollBars(infoPtr);
2212 TREEVIEW_Invalidate(infoPtr, item);
2217 return TRUE;
2220 static LRESULT
2221 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2223 TRACE("\n");
2225 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2226 return 0;
2228 return (item->state & mask);
2231 static LRESULT
2232 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2234 TREEVIEW_ITEM *retval;
2236 retval = 0;
2238 /* handle all the global data here */
2239 switch (which)
2241 case TVGN_CHILD: /* Special case: child of 0 is root */
2242 if (item)
2243 break;
2244 /* fall through */
2245 case TVGN_ROOT:
2246 retval = infoPtr->root->firstChild;
2247 break;
2249 case TVGN_CARET:
2250 retval = infoPtr->selectedItem;
2251 break;
2253 case TVGN_FIRSTVISIBLE:
2254 retval = infoPtr->firstVisible;
2255 break;
2257 case TVGN_DROPHILITE:
2258 retval = infoPtr->dropItem;
2259 break;
2261 case TVGN_LASTVISIBLE:
2262 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2263 break;
2266 if (retval)
2268 TRACE("flags:%x, returns %p\n", which, retval);
2269 return (LRESULT)retval;
2272 if (item == TVI_ROOT) item = infoPtr->root;
2274 if (!TREEVIEW_ValidItem(infoPtr, item))
2275 return FALSE;
2277 switch (which)
2279 case TVGN_NEXT:
2280 retval = item->nextSibling;
2281 break;
2282 case TVGN_PREVIOUS:
2283 retval = item->prevSibling;
2284 break;
2285 case TVGN_PARENT:
2286 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2287 break;
2288 case TVGN_CHILD:
2289 retval = item->firstChild;
2290 break;
2291 case TVGN_NEXTVISIBLE:
2292 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2293 break;
2294 case TVGN_PREVIOUSVISIBLE:
2295 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2296 break;
2297 default:
2298 TRACE("Unknown msg %x,item %p\n", which, item);
2299 break;
2302 TRACE("flags:%x, item %p;returns %p\n", which, item, retval);
2303 return (LRESULT)retval;
2307 static LRESULT
2308 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2310 TRACE(" %d\n", infoPtr->uNumItems);
2311 return (LRESULT)infoPtr->uNumItems;
2314 static VOID
2315 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2317 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2319 static const unsigned int state_table[] = { 0, 2, 1 };
2321 unsigned int state;
2323 state = STATEIMAGEINDEX(item->state);
2324 TRACE("state:%x\n", state);
2325 item->state &= ~TVIS_STATEIMAGEMASK;
2327 if (state < 3)
2328 state = state_table[state];
2330 item->state |= INDEXTOSTATEIMAGEMASK(state);
2332 TRACE("state:%x\n", state);
2333 TREEVIEW_Invalidate(infoPtr, item);
2338 /* Painting *************************************************************/
2340 /* Draw the lines and expand button for an item. Also draws one section
2341 * of the line from item's parent to item's parent's next sibling. */
2342 static void
2343 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2345 LONG centerx, centery;
2346 BOOL lar = ((infoPtr->dwStyle
2347 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2348 > TVS_LINESATROOT);
2349 HBRUSH hbr, hbrOld;
2350 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2352 if (!lar && item->iLevel == 0)
2353 return;
2355 hbr = CreateSolidBrush(clrBk);
2356 hbrOld = SelectObject(hdc, hbr);
2358 centerx = (item->linesOffset + item->stateOffset) / 2;
2359 centery = (item->rect.top + item->rect.bottom) / 2;
2361 if (infoPtr->dwStyle & TVS_HASLINES)
2363 HPEN hOldPen, hNewPen;
2364 HTREEITEM parent;
2365 LOGBRUSH lb;
2367 /* Get a dotted grey pen */
2368 lb.lbStyle = BS_SOLID;
2369 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2370 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2371 hOldPen = SelectObject(hdc, hNewPen);
2373 /* Make sure the center is on a dot (using +2 instead
2374 * of +1 gives us pixel-by-pixel compat with native) */
2375 centery = (centery + 2) & ~1;
2377 MoveToEx(hdc, item->stateOffset, centery, NULL);
2378 LineTo(hdc, centerx - 1, centery);
2380 if (item->prevSibling || item->parent != infoPtr->root)
2382 MoveToEx(hdc, centerx, item->rect.top, NULL);
2383 LineTo(hdc, centerx, centery);
2386 if (item->nextSibling)
2388 MoveToEx(hdc, centerx, centery, NULL);
2389 LineTo(hdc, centerx, item->rect.bottom + 1);
2392 /* Draw the line from our parent to its next sibling. */
2393 parent = item->parent;
2394 while (parent != infoPtr->root)
2396 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2398 if (parent->nextSibling
2399 /* skip top-levels unless TVS_LINESATROOT */
2400 && parent->stateOffset > parent->linesOffset)
2402 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2403 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2406 parent = parent->parent;
2409 SelectObject(hdc, hOldPen);
2410 DeleteObject(hNewPen);
2414 * Display the (+/-) signs
2417 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2419 if (item->cChildren)
2421 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2422 if (theme)
2424 RECT glyphRect = item->rect;
2425 glyphRect.left = item->linesOffset;
2426 glyphRect.right = item->stateOffset;
2427 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2428 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2429 &glyphRect, NULL);
2431 else
2433 LONG height = item->rect.bottom - item->rect.top;
2434 LONG width = item->stateOffset - item->linesOffset;
2435 LONG rectsize = min(height, width) / 4;
2436 /* plussize = ceil(rectsize * 3/4) */
2437 LONG plussize = (rectsize + 1) * 3 / 4;
2439 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2440 HPEN old_pen = SelectObject(hdc, new_pen);
2442 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2443 centerx + rectsize + 2, centery + rectsize + 2);
2445 SelectObject(hdc, old_pen);
2446 DeleteObject(new_pen);
2448 /* draw +/- signs with current text color */
2449 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2450 old_pen = SelectObject(hdc, new_pen);
2452 if (height < 18 || width < 18)
2454 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2455 LineTo(hdc, centerx + plussize, centery);
2457 if (!(item->state & TVIS_EXPANDED) ||
2458 (item->state & TVIS_EXPANDPARTIAL))
2460 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2461 LineTo(hdc, centerx, centery + plussize);
2464 else
2466 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2467 centerx + plussize, centery + 2);
2469 if (!(item->state & TVIS_EXPANDED) ||
2470 (item->state & TVIS_EXPANDPARTIAL))
2472 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2473 centerx + 2, centery + plussize);
2474 SetPixel(hdc, centerx - 1, centery, clrBk);
2475 SetPixel(hdc, centerx + 1, centery, clrBk);
2479 SelectObject(hdc, old_pen);
2480 DeleteObject(new_pen);
2484 SelectObject(hdc, hbrOld);
2485 DeleteObject(hbr);
2488 static void
2489 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2491 INT cditem;
2492 HFONT hOldFont;
2493 COLORREF oldTextColor, oldTextBkColor;
2494 int centery;
2495 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2496 NMTVCUSTOMDRAW nmcdhdr;
2498 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2500 /* - If item is drop target or it is selected and window is in focus -
2501 * use blue background (COLOR_HIGHLIGHT).
2502 * - If item is selected, window is not in focus, but it has style
2503 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2504 * - Otherwise - use background color
2506 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2507 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2508 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2510 if ((item->state & TVIS_DROPHILITED) || inFocus)
2512 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2513 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2515 else
2517 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2518 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2521 else
2523 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2524 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2525 nmcdhdr.clrText = comctl32_color.clrHighlight;
2526 else
2527 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2530 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2532 /* The custom draw handler can query the text rectangle,
2533 * so get ready. */
2534 /* should already be known, set to 0 when changed */
2535 if (!item->textWidth)
2536 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2538 cditem = 0;
2540 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2542 cditem = TREEVIEW_SendCustomDrawItemNotify
2543 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2544 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2546 if (cditem & CDRF_SKIPDEFAULT)
2548 SelectObject(hdc, hOldFont);
2549 return;
2553 if (cditem & CDRF_NEWFONT)
2554 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2556 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2558 /* Set colors. Custom draw handler can change these so we do this after it. */
2559 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2560 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2562 centery = (item->rect.top + item->rect.bottom) / 2;
2565 * Display the images associated with this item
2568 INT imageIndex;
2570 /* State images are displayed to the left of the Normal image
2571 * image number is in state; zero should be `display no image'.
2573 imageIndex = STATEIMAGEINDEX(item->state);
2575 if (infoPtr->himlState && imageIndex)
2577 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2578 item->stateOffset,
2579 centery - infoPtr->stateImageHeight / 2,
2580 ILD_NORMAL);
2583 /* Now, draw the normal image; can be either selected,
2584 * non-selected or expanded image.
2587 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2589 /* The item is currently selected */
2590 imageIndex = item->iSelectedImage;
2592 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2594 /* The item is currently not selected but expanded */
2595 imageIndex = item->iExpandedImage;
2597 else
2599 /* The item is not selected and not expanded */
2600 imageIndex = item->iImage;
2603 if (infoPtr->himlNormal)
2605 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2607 style |= item->state & TVIS_OVERLAYMASK;
2609 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2610 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2611 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2612 style);
2618 * Display the text associated with this item
2621 /* Don't paint item's text if it's being edited */
2622 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2624 if (item->pszText)
2626 RECT rcText;
2627 UINT align;
2628 SIZE sz;
2630 rcText.top = item->rect.top;
2631 rcText.bottom = item->rect.bottom;
2632 rcText.left = item->textOffset;
2633 rcText.right = rcText.left + item->textWidth + 4;
2635 TRACE("drawing text %s at (%s)\n",
2636 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2638 /* Draw it */
2639 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2641 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2642 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2643 ETO_CLIPPED | ETO_OPAQUE,
2644 &rcText,
2645 item->pszText,
2646 lstrlenW(item->pszText),
2647 NULL);
2648 SetTextAlign(hdc, align);
2650 /* Draw focus box around the selected item */
2651 if ((item == infoPtr->selectedItem) && inFocus)
2653 DrawFocusRect(hdc,&rcText);
2658 /* Draw insertion mark if necessary */
2660 if (infoPtr->insertMarkItem)
2661 TRACE("item:%d,mark:%p\n",
2662 TREEVIEW_GetItemIndex(infoPtr, item),
2663 infoPtr->insertMarkItem);
2665 if (item == infoPtr->insertMarkItem)
2667 HPEN hNewPen, hOldPen;
2668 int offset;
2669 int left, right;
2671 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2672 hOldPen = SelectObject(hdc, hNewPen);
2674 if (infoPtr->insertBeforeorAfter)
2675 offset = item->rect.bottom - 1;
2676 else
2677 offset = item->rect.top + 1;
2679 left = item->textOffset - 2;
2680 right = item->textOffset + item->textWidth + 2;
2682 MoveToEx(hdc, left, offset - 3, NULL);
2683 LineTo(hdc, left, offset + 4);
2685 MoveToEx(hdc, left, offset, NULL);
2686 LineTo(hdc, right + 1, offset);
2688 MoveToEx(hdc, right, offset + 3, NULL);
2689 LineTo(hdc, right, offset - 4);
2691 SelectObject(hdc, hOldPen);
2692 DeleteObject(hNewPen);
2695 if (cditem & CDRF_NOTIFYPOSTPAINT)
2697 cditem = TREEVIEW_SendCustomDrawItemNotify
2698 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2699 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2702 /* Restore the hdc state */
2703 SetTextColor(hdc, oldTextColor);
2704 SetBkColor(hdc, oldTextBkColor);
2705 SelectObject(hdc, hOldFont);
2708 /* Computes treeHeight and treeWidth and updates the scroll bars.
2710 static void
2711 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2713 TREEVIEW_ITEM *item;
2714 HWND hwnd = infoPtr->hwnd;
2715 BOOL vert = FALSE;
2716 BOOL horz = FALSE;
2717 SCROLLINFO si;
2718 LONG scrollX = infoPtr->scrollX;
2720 infoPtr->treeWidth = 0;
2721 infoPtr->treeHeight = 0;
2723 /* We iterate through all visible items in order to get the tree height
2724 * and width */
2725 item = infoPtr->root->firstChild;
2727 while (item != NULL)
2729 if (ISVISIBLE(item))
2731 /* actually we draw text at textOffset + 2 */
2732 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2733 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2735 /* This is scroll-adjusted, but we fix this below. */
2736 infoPtr->treeHeight = item->rect.bottom;
2739 item = TREEVIEW_GetNextListItem(infoPtr, item);
2742 /* Fix the scroll adjusted treeHeight and treeWidth. */
2743 if (infoPtr->root->firstChild)
2744 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2746 infoPtr->treeWidth += infoPtr->scrollX;
2748 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2750 /* Adding one scroll bar may take up enough space that it forces us
2751 * to add the other as well. */
2752 if (infoPtr->treeHeight > infoPtr->clientHeight)
2754 vert = TRUE;
2756 if (infoPtr->treeWidth
2757 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2758 horz = TRUE;
2760 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2761 horz = TRUE;
2763 if (!vert && horz && infoPtr->treeHeight
2764 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2765 vert = TRUE;
2767 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2769 si.cbSize = sizeof(SCROLLINFO);
2770 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2771 si.nMin = 0;
2773 if (vert)
2775 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2776 if ( si.nPage && NULL != infoPtr->firstVisible)
2778 si.nPos = infoPtr->firstVisible->visibleOrder;
2779 si.nMax = infoPtr->maxVisibleOrder - 1;
2781 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2783 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2784 ShowScrollBar(hwnd, SB_VERT, TRUE);
2785 infoPtr->uInternalStatus |= TV_VSCROLL;
2787 else
2789 if (infoPtr->uInternalStatus & TV_VSCROLL)
2790 ShowScrollBar(hwnd, SB_VERT, FALSE);
2791 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2794 else
2796 if (infoPtr->uInternalStatus & TV_VSCROLL)
2797 ShowScrollBar(hwnd, SB_VERT, FALSE);
2798 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2801 if (horz)
2803 si.nPage = infoPtr->clientWidth;
2804 si.nPos = infoPtr->scrollX;
2805 si.nMax = infoPtr->treeWidth - 1;
2807 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2809 si.nPos = si.nMax - max( si.nPage-1, 0 );
2810 scrollX = si.nPos;
2813 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2814 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2815 infoPtr->uInternalStatus |= TV_HSCROLL;
2817 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2818 TREEVIEW_HScroll(infoPtr,
2819 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2821 else
2823 if (infoPtr->uInternalStatus & TV_HSCROLL)
2824 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2825 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2827 scrollX = 0;
2828 if (infoPtr->scrollX != 0)
2830 TREEVIEW_HScroll(infoPtr,
2831 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2835 if (!horz)
2836 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2839 static void
2840 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2842 HBRUSH hBrush;
2843 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2845 hBrush = CreateSolidBrush(clrBk);
2846 FillRect(hdc, rc, hBrush);
2847 DeleteObject(hBrush);
2850 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2851 static LRESULT
2852 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2854 RECT rect;
2856 TRACE("%p\n", infoPtr);
2858 GetClientRect(infoPtr->hwnd, &rect);
2859 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2861 return 1;
2864 static void
2865 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2867 HWND hwnd = infoPtr->hwnd;
2868 RECT rect = *rc;
2869 TREEVIEW_ITEM *item;
2871 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2873 TRACE("empty window\n");
2874 return;
2877 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2878 hdc, rect);
2880 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2882 ReleaseDC(hwnd, hdc);
2883 return;
2886 for (item = infoPtr->root->firstChild;
2887 item != NULL;
2888 item = TREEVIEW_GetNextListItem(infoPtr, item))
2890 if (ISVISIBLE(item))
2892 /* Avoid unneeded calculations */
2893 if (item->rect.top > rect.bottom)
2894 break;
2895 if (item->rect.bottom < rect.top)
2896 continue;
2898 TREEVIEW_DrawItem(infoPtr, hdc, item);
2902 TREEVIEW_UpdateScrollBars(infoPtr);
2904 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2905 infoPtr->cdmode =
2906 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2909 static inline void
2910 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2912 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2915 static void
2916 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2918 if (item)
2919 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2920 else
2921 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2924 static LRESULT
2925 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2927 HDC hdc;
2928 PAINTSTRUCT ps;
2929 RECT rc;
2931 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2933 if (hdc_ref)
2935 hdc = hdc_ref;
2936 GetClientRect(infoPtr->hwnd, &rc);
2937 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2939 else
2941 hdc = BeginPaint(infoPtr->hwnd, &ps);
2942 rc = ps.rcPaint;
2943 if(ps.fErase)
2944 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2947 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2948 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2950 if (!hdc_ref)
2951 EndPaint(infoPtr->hwnd, &ps);
2953 return 0;
2956 static LRESULT
2957 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2959 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2961 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2962 return 0;
2964 if (options & PRF_ERASEBKGND)
2965 TREEVIEW_EraseBackground(infoPtr, hdc);
2967 if (options & PRF_CLIENT)
2969 RECT rc;
2970 GetClientRect(infoPtr->hwnd, &rc);
2971 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2974 return 0;
2977 /* Sorting **************************************************************/
2979 /***************************************************************************
2980 * Forward the DPA local callback to the treeview owner callback
2982 static INT WINAPI
2983 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2984 const TVSORTCB *pCallBackSort)
2986 /* Forward the call to the client-defined callback */
2987 return pCallBackSort->lpfnCompare(first->lParam,
2988 second->lParam,
2989 pCallBackSort->lParam);
2992 /***************************************************************************
2993 * Treeview native sort routine: sort on item text.
2995 static INT WINAPI
2996 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2997 const TREEVIEW_INFO *infoPtr)
2999 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3000 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3002 if(first->pszText && second->pszText)
3003 return lstrcmpiW(first->pszText, second->pszText);
3004 else if(first->pszText)
3005 return -1;
3006 else if(second->pszText)
3007 return 1;
3008 else
3009 return 0;
3012 /* Returns the number of physical children belonging to item. */
3013 static INT
3014 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3016 INT cChildren = 0;
3017 HTREEITEM hti;
3019 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3020 cChildren++;
3022 return cChildren;
3025 /* Returns a DPA containing a pointer to each physical child of item in
3026 * sibling order. If item has no children, an empty DPA is returned. */
3027 static HDPA
3028 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3030 HTREEITEM child;
3032 HDPA list = DPA_Create(8);
3033 if (list == 0) return NULL;
3035 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3037 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3039 DPA_Destroy(list);
3040 return NULL;
3044 return list;
3047 /***************************************************************************
3048 * Setup the treeview structure with regards of the sort method
3049 * and sort the children of the TV item specified in lParam
3050 * fRecurse: currently unused. Should be zero.
3051 * parent: if pSort!=NULL, should equal pSort->hParent.
3052 * otherwise, item which child items are to be sorted.
3053 * pSort: sort method info. if NULL, sort on item text.
3054 * if non-NULL, sort on item's lParam content, and let the
3055 * application decide what that means. See also TVM_SORTCHILDRENCB.
3058 static LRESULT
3059 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3060 LPTVSORTCB pSort)
3062 INT cChildren;
3063 PFNDPACOMPARE pfnCompare;
3064 LPARAM lpCompare;
3066 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3067 if (parent == TVI_ROOT || parent == NULL)
3068 parent = infoPtr->root;
3070 /* Check for a valid handle to the parent item */
3071 if (!TREEVIEW_ValidItem(infoPtr, parent))
3073 ERR("invalid item hParent=%p\n", parent);
3074 return FALSE;
3077 if (pSort)
3079 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3080 lpCompare = (LPARAM)pSort;
3082 else
3084 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3085 lpCompare = (LPARAM)infoPtr;
3088 cChildren = TREEVIEW_CountChildren(parent);
3090 /* Make sure there is something to sort */
3091 if (cChildren > 1)
3093 /* TREEVIEW_ITEM rechaining */
3094 INT count = 0;
3095 HTREEITEM item = 0;
3096 HTREEITEM nextItem = 0;
3097 HTREEITEM prevItem = 0;
3099 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3101 if (sortList == NULL)
3102 return FALSE;
3104 /* let DPA sort the list */
3105 DPA_Sort(sortList, pfnCompare, lpCompare);
3107 /* The order of DPA entries has been changed, so fixup the
3108 * nextSibling and prevSibling pointers. */
3110 item = DPA_GetPtr(sortList, count++);
3111 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3113 /* link the two current item together */
3114 item->nextSibling = nextItem;
3115 nextItem->prevSibling = item;
3117 if (prevItem == NULL)
3119 /* this is the first item, update the parent */
3120 parent->firstChild = item;
3121 item->prevSibling = NULL;
3123 else
3125 /* fix the back chaining */
3126 item->prevSibling = prevItem;
3129 /* get ready for the next one */
3130 prevItem = item;
3131 item = nextItem;
3134 /* the last item is pointed to by item and never has a sibling */
3135 item->nextSibling = NULL;
3136 parent->lastChild = item;
3138 DPA_Destroy(sortList);
3140 TREEVIEW_VerifyTree(infoPtr);
3142 if (parent->state & TVIS_EXPANDED)
3144 int visOrder = infoPtr->firstVisible->visibleOrder;
3146 if (parent == infoPtr->root)
3147 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3148 else
3149 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3151 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3153 TREEVIEW_ITEM *item;
3155 for (item = infoPtr->root->firstChild; item != NULL;
3156 item = TREEVIEW_GetNextListItem(infoPtr, item))
3158 if (item->visibleOrder == visOrder)
3159 break;
3162 if (!item) item = parent->firstChild;
3163 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3166 TREEVIEW_Invalidate(infoPtr, NULL);
3169 return TRUE;
3171 return FALSE;
3175 /***************************************************************************
3176 * Setup the treeview structure with regards of the sort method
3177 * and sort the children of the TV item specified in lParam
3179 static LRESULT
3180 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3182 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3186 /***************************************************************************
3187 * Sort the children of the TV item specified in lParam.
3189 static LRESULT
3190 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3192 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3196 /* Expansion/Collapse ***************************************************/
3198 static BOOL
3199 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3200 UINT action)
3202 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3203 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3204 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3205 0, item);
3208 static VOID
3209 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3210 UINT action)
3212 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3213 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3214 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3215 0, item);
3219 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3220 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3221 static BOOL
3222 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3223 BOOL bRemoveChildren, BOOL bUser)
3225 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3226 BOOL bSetSelection, bSetFirstVisible;
3227 RECT scrollRect;
3228 LONG scrollDist = 0;
3229 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3231 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3233 if (!(item->state & TVIS_EXPANDED))
3234 return FALSE;
3236 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3237 TREEVIEW_SendExpanding(infoPtr, item, action);
3239 if (item->firstChild == NULL)
3240 return FALSE;
3242 item->state &= ~TVIS_EXPANDED;
3244 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3245 TREEVIEW_SendExpanded(infoPtr, item, action);
3247 bSetSelection = (infoPtr->selectedItem != NULL
3248 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3250 bSetFirstVisible = (infoPtr->firstVisible != NULL
3251 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3253 tmpItem = item;
3254 while (tmpItem)
3256 if (tmpItem->nextSibling)
3258 nextItem = tmpItem->nextSibling;
3259 break;
3261 tmpItem = tmpItem->parent;
3264 if (nextItem)
3265 scrollDist = nextItem->rect.top;
3267 if (bRemoveChildren)
3269 INT old_cChildren = item->cChildren;
3270 TRACE("TVE_COLLAPSERESET\n");
3271 item->state &= ~TVIS_EXPANDEDONCE;
3272 TREEVIEW_RemoveAllChildren(infoPtr, item);
3273 item->cChildren = old_cChildren;
3276 if (item->firstChild)
3278 TREEVIEW_ITEM *i, *sibling;
3280 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3282 for (i = item->firstChild; i != sibling;
3283 i = TREEVIEW_GetNextListItem(infoPtr, i))
3285 i->visibleOrder = -1;
3289 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3291 if (nextItem)
3292 scrollDist = -(scrollDist - nextItem->rect.top);
3294 if (bSetSelection)
3296 /* Don't call DoSelectItem, it sends notifications. */
3297 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3298 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3299 item->state |= TVIS_SELECTED;
3300 infoPtr->selectedItem = item;
3303 TREEVIEW_UpdateScrollBars(infoPtr);
3305 scrollRect.left = 0;
3306 scrollRect.right = infoPtr->clientWidth;
3307 scrollRect.bottom = infoPtr->clientHeight;
3309 if (nextItem)
3311 scrollRect.top = nextItem->rect.top;
3313 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3314 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3315 TREEVIEW_Invalidate(infoPtr, item);
3316 } else {
3317 scrollRect.top = item->rect.top;
3318 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3321 TREEVIEW_SetFirstVisible(infoPtr,
3322 bSetFirstVisible ? item : infoPtr->firstVisible,
3323 TRUE);
3325 return TRUE;
3328 static BOOL
3329 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3330 BOOL partial, BOOL user)
3332 LONG scrollDist;
3333 LONG orgNextTop = 0;
3334 RECT scrollRect;
3335 TREEVIEW_ITEM *nextItem, *tmpItem;
3336 BOOL sendsNotifications;
3338 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, item, partial, user);
3340 if (item->state & TVIS_EXPANDED)
3341 return TRUE;
3343 tmpItem = item; nextItem = NULL;
3344 while (tmpItem)
3346 if (tmpItem->nextSibling)
3348 nextItem = tmpItem->nextSibling;
3349 break;
3351 tmpItem = tmpItem->parent;
3354 if (nextItem)
3355 orgNextTop = nextItem->rect.top;
3357 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3359 sendsNotifications = user || ((item->cChildren != 0) &&
3360 !(item->state & TVIS_EXPANDEDONCE));
3361 if (sendsNotifications)
3363 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3365 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3366 return FALSE;
3369 if (!item->firstChild)
3370 return FALSE;
3372 item->state |= TVIS_EXPANDED;
3374 if (partial)
3375 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3377 if (ISVISIBLE(item))
3379 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3380 TREEVIEW_UpdateSubTree(infoPtr, item);
3381 TREEVIEW_UpdateScrollBars(infoPtr);
3383 scrollRect.left = 0;
3384 scrollRect.bottom = infoPtr->treeHeight;
3385 scrollRect.right = infoPtr->clientWidth;
3386 if (nextItem)
3388 scrollDist = nextItem->rect.top - orgNextTop;
3389 scrollRect.top = orgNextTop;
3391 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3392 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3393 TREEVIEW_Invalidate (infoPtr, item);
3394 } else {
3395 scrollRect.top = item->rect.top;
3396 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3399 /* Scroll up so that as many children as possible are visible.
3400 * This fails when expanding causes an HScroll bar to appear, but we
3401 * don't know that yet, so the last item is obscured. */
3402 if (item->firstChild != NULL)
3404 int nChildren = item->lastChild->visibleOrder
3405 - item->firstChild->visibleOrder + 1;
3407 int visible_pos = item->visibleOrder
3408 - infoPtr->firstVisible->visibleOrder;
3410 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3412 if (visible_pos > 0 && nChildren > rows_below)
3414 int scroll = nChildren - rows_below;
3416 if (scroll > visible_pos)
3417 scroll = visible_pos;
3419 if (scroll > 0)
3421 TREEVIEW_ITEM *newFirstVisible
3422 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3423 scroll);
3426 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3432 if (sendsNotifications) {
3433 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3434 item->state |= TVIS_EXPANDEDONCE;
3437 return TRUE;
3440 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3441 to mouse messages and TVM_SELECTITEM.
3443 selection - previously selected item, used to collapse a part of a tree
3444 item - new selected item
3446 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3447 HTREEITEM selection, HTREEITEM item)
3449 TREEVIEW_ITEM *SelItem;
3451 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3453 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3456 * Close the previous selection all the way to the root
3457 * as long as the new selection is not a child
3459 if(selection && (selection != item))
3461 BOOL closeit = TRUE;
3462 SelItem = item;
3464 /* determine if the hitItem is a child of the currently selected item */
3465 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3466 (SelItem->parent != infoPtr->root))
3468 closeit = (SelItem != selection);
3469 SelItem = SelItem->parent;
3472 if(closeit)
3474 if(TREEVIEW_ValidItem(infoPtr, selection))
3475 SelItem = selection;
3477 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3478 SelItem->parent != infoPtr->root)
3480 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3481 SelItem = SelItem->parent;
3487 * Expand the current item
3489 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3492 static BOOL
3493 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3495 TRACE("item=%p, user=%d\n", item, user);
3497 if (item->state & TVIS_EXPANDED)
3498 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3499 else
3500 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3503 static VOID
3504 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3506 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3508 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3510 if (TREEVIEW_HasChildren(infoPtr, item))
3511 TREEVIEW_ExpandAll(infoPtr, item);
3515 /* Note:If the specified item is the child of a collapsed parent item,
3516 the parent's list of child items is (recursively) expanded to reveal the
3517 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3518 know if it also applies here.
3521 static LRESULT
3522 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3524 if (!TREEVIEW_ValidItem(infoPtr, item))
3525 return 0;
3527 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3528 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3529 flag, item->state);
3531 switch (flag & TVE_TOGGLE)
3533 case TVE_COLLAPSE:
3534 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3535 FALSE);
3537 case TVE_EXPAND:
3538 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3539 FALSE);
3541 case TVE_TOGGLE:
3542 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3544 default:
3545 return 0;
3549 /* Hit-Testing **********************************************************/
3551 static TREEVIEW_ITEM *
3552 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3554 TREEVIEW_ITEM *item;
3555 LONG row;
3557 if (!infoPtr->firstVisible)
3558 return NULL;
3560 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3562 for (item = infoPtr->firstVisible; item != NULL;
3563 item = TREEVIEW_GetNextListItem(infoPtr, item))
3565 if (row >= item->visibleOrder
3566 && row < item->visibleOrder + item->iIntegral)
3567 break;
3570 return item;
3573 static LRESULT
3574 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3576 TREEVIEW_ITEM *item;
3577 RECT rect;
3578 UINT status;
3579 LONG x, y;
3581 lpht->hItem = 0;
3582 GetClientRect(infoPtr->hwnd, &rect);
3583 status = 0;
3584 x = lpht->pt.x;
3585 y = lpht->pt.y;
3587 if (x < rect.left)
3589 status |= TVHT_TOLEFT;
3591 else if (x > rect.right)
3593 status |= TVHT_TORIGHT;
3596 if (y < rect.top)
3598 status |= TVHT_ABOVE;
3600 else if (y > rect.bottom)
3602 status |= TVHT_BELOW;
3605 if (status)
3607 lpht->flags = status;
3608 return 0;
3611 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3612 if (!item)
3614 lpht->flags = TVHT_NOWHERE;
3615 return 0;
3618 if (x >= item->textOffset + item->textWidth)
3620 lpht->flags = TVHT_ONITEMRIGHT;
3622 else if (x >= item->textOffset)
3624 lpht->flags = TVHT_ONITEMLABEL;
3626 else if (x >= item->imageOffset)
3628 lpht->flags = TVHT_ONITEMICON;
3630 else if (x >= item->stateOffset)
3632 lpht->flags = TVHT_ONITEMSTATEICON;
3634 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3636 lpht->flags = TVHT_ONITEMBUTTON;
3638 else
3640 lpht->flags = TVHT_ONITEMINDENT;
3643 lpht->hItem = item;
3644 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3646 return (LRESULT)item;
3649 /* Item Label Editing ***************************************************/
3651 static LRESULT
3652 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3654 return (LRESULT)infoPtr->hwndEdit;
3657 static LRESULT CALLBACK
3658 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3660 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3661 BOOL bCancel = FALSE;
3662 LRESULT rc;
3664 switch (uMsg)
3666 case WM_PAINT:
3667 TRACE("WM_PAINT start\n");
3668 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3669 lParam);
3670 TRACE("WM_PAINT done\n");
3671 return rc;
3673 case WM_KILLFOCUS:
3674 if (infoPtr->bIgnoreEditKillFocus)
3675 return TRUE;
3676 break;
3678 case WM_DESTROY:
3680 WNDPROC editProc = infoPtr->wpEditOrig;
3681 infoPtr->wpEditOrig = 0;
3682 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3683 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3686 case WM_GETDLGCODE:
3687 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3689 case WM_KEYDOWN:
3690 if (wParam == VK_ESCAPE)
3692 bCancel = TRUE;
3693 break;
3695 else if (wParam == VK_RETURN)
3697 break;
3700 /* fall through */
3701 default:
3702 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3705 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3706 /* eg. Using a messagebox */
3708 infoPtr->bIgnoreEditKillFocus = TRUE;
3709 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3710 infoPtr->bIgnoreEditKillFocus = FALSE;
3712 return 0;
3716 /* should handle edit control messages here */
3718 static LRESULT
3719 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3721 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3723 switch (HIWORD(wParam))
3725 case EN_UPDATE:
3728 * Adjust the edit window size
3730 WCHAR buffer[1024];
3731 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3732 HDC hdc = GetDC(infoPtr->hwndEdit);
3733 SIZE sz;
3734 HFONT hFont, hOldFont = 0;
3736 TRACE("edit=%p\n", infoPtr->hwndEdit);
3738 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3740 infoPtr->bLabelChanged = TRUE;
3742 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3744 /* Select font to get the right dimension of the string */
3745 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3747 if (hFont != 0)
3749 hOldFont = SelectObject(hdc, hFont);
3752 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3754 TEXTMETRICW textMetric;
3756 /* Add Extra spacing for the next character */
3757 GetTextMetricsW(hdc, &textMetric);
3758 sz.cx += (textMetric.tmMaxCharWidth * 2);
3760 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3761 sz.cx = min(sz.cx,
3762 infoPtr->clientWidth - editItem->textOffset + 2);
3764 SetWindowPos(infoPtr->hwndEdit,
3765 HWND_TOP,
3768 sz.cx,
3769 editItem->rect.bottom - editItem->rect.top + 3,
3770 SWP_NOMOVE | SWP_DRAWFRAME);
3773 if (hFont != 0)
3775 SelectObject(hdc, hOldFont);
3778 ReleaseDC(infoPtr->hwnd, hdc);
3779 break;
3781 case EN_KILLFOCUS:
3782 /* apparently we should respect passed handle value */
3783 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3785 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3786 break;
3788 default:
3789 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3792 return 0;
3795 static HWND
3796 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3798 HWND hwnd = infoPtr->hwnd;
3799 HWND hwndEdit;
3800 SIZE sz;
3801 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3802 HDC hdc;
3803 HFONT hOldFont=0;
3804 TEXTMETRICW textMetric;
3806 TRACE("%p %p\n", hwnd, hItem);
3807 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3808 return NULL;
3810 if (infoPtr->hwndEdit)
3811 return infoPtr->hwndEdit;
3813 infoPtr->bLabelChanged = FALSE;
3815 /* make edit item visible */
3816 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3818 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3820 hdc = GetDC(hwnd);
3821 /* Select the font to get appropriate metric dimensions */
3822 if (infoPtr->hFont != 0)
3824 hOldFont = SelectObject(hdc, infoPtr->hFont);
3827 /* Get string length in pixels */
3828 if (hItem->pszText)
3829 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3830 &sz);
3831 else
3832 GetTextExtentPoint32A(hdc, "", 0, &sz);
3834 /* Add Extra spacing for the next character */
3835 GetTextMetricsW(hdc, &textMetric);
3836 sz.cx += (textMetric.tmMaxCharWidth * 2);
3838 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3839 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3841 if (infoPtr->hFont != 0)
3843 SelectObject(hdc, hOldFont);
3846 ReleaseDC(hwnd, hdc);
3848 infoPtr->editItem = hItem;
3850 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3851 WC_EDITW,
3853 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3854 WS_CLIPSIBLINGS | ES_WANTRETURN |
3855 ES_LEFT, hItem->textOffset - 2,
3856 hItem->rect.top - 1, sz.cx + 3,
3857 hItem->rect.bottom -
3858 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3859 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3861 infoPtr->hwndEdit = hwndEdit;
3863 /* Get a 2D border. */
3864 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3865 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3866 SetWindowLongW(hwndEdit, GWL_STYLE,
3867 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3869 SendMessageW(hwndEdit, WM_SETFONT,
3870 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3872 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3873 (DWORD_PTR)
3874 TREEVIEW_Edit_SubclassProc);
3875 if (hItem->pszText)
3876 SetWindowTextW(hwndEdit, hItem->pszText);
3878 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3880 DestroyWindow(hwndEdit);
3881 infoPtr->hwndEdit = 0;
3882 infoPtr->editItem = NULL;
3883 return NULL;
3886 SetFocus(hwndEdit);
3887 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3888 ShowWindow(hwndEdit, SW_SHOW);
3890 return hwndEdit;
3894 static LRESULT
3895 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3897 HWND hwnd = infoPtr->hwnd;
3898 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3899 NMTVDISPINFOW tvdi;
3900 BOOL bCommit;
3901 WCHAR tmpText[1024] = { '\0' };
3902 WCHAR *newText = tmpText;
3903 int iLength = 0;
3905 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3907 tvdi.hdr.hwndFrom = hwnd;
3908 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3909 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3910 tvdi.item.mask = 0;
3911 tvdi.item.hItem = editedItem;
3912 tvdi.item.state = editedItem->state;
3913 tvdi.item.lParam = editedItem->lParam;
3915 if (!bCancel)
3917 if (!infoPtr->bNtfUnicode)
3918 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3919 else
3920 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3922 if (iLength >= 1023)
3924 ERR("Insufficient space to retrieve new item label\n");
3927 tvdi.item.mask = TVIF_TEXT;
3928 tvdi.item.pszText = tmpText;
3929 tvdi.item.cchTextMax = iLength + 1;
3931 else
3933 tvdi.item.pszText = NULL;
3934 tvdi.item.cchTextMax = 0;
3937 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
3939 if (!bCancel && bCommit) /* Apply the changes */
3941 if (!infoPtr->bNtfUnicode)
3943 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3944 newText = Alloc(len * sizeof(WCHAR));
3945 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3946 iLength = len - 1;
3949 if (strcmpW(newText, editedItem->pszText) != 0)
3951 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3952 if (ptr == NULL)
3954 ERR("OutOfMemory, cannot allocate space for label\n");
3955 if(newText != tmpText) Free(newText);
3956 DestroyWindow(infoPtr->hwndEdit);
3957 infoPtr->hwndEdit = 0;
3958 infoPtr->editItem = NULL;
3959 return FALSE;
3961 else
3963 editedItem->pszText = ptr;
3964 editedItem->cchTextMax = iLength + 1;
3965 strcpyW(editedItem->pszText, newText);
3966 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3969 if(newText != tmpText) Free(newText);
3972 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3973 DestroyWindow(infoPtr->hwndEdit);
3974 infoPtr->hwndEdit = 0;
3975 infoPtr->editItem = NULL;
3976 return TRUE;
3979 static LRESULT
3980 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3982 if (wParam != TV_EDIT_TIMER)
3984 ERR("got unknown timer\n");
3985 return 1;
3988 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3989 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3991 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3993 return 0;
3997 /* Mouse Tracking/Drag **************************************************/
3999 /***************************************************************************
4000 * This is quite unusual piece of code, but that's how it's implemented in
4001 * Windows.
4003 static LRESULT
4004 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4006 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4007 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4008 RECT r;
4009 MSG msg;
4011 r.top = pt.y - cyDrag;
4012 r.left = pt.x - cxDrag;
4013 r.bottom = pt.y + cyDrag;
4014 r.right = pt.x + cxDrag;
4016 SetCapture(infoPtr->hwnd);
4018 while (1)
4020 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4022 if (msg.message == WM_MOUSEMOVE)
4024 pt.x = (short)LOWORD(msg.lParam);
4025 pt.y = (short)HIWORD(msg.lParam);
4026 if (PtInRect(&r, pt))
4027 continue;
4028 else
4030 ReleaseCapture();
4031 return 1;
4034 else if (msg.message >= WM_LBUTTONDOWN &&
4035 msg.message <= WM_RBUTTONDBLCLK)
4037 if (msg.message == WM_RBUTTONUP)
4038 TREEVIEW_RButtonUp(infoPtr, &pt);
4039 break;
4042 DispatchMessageW(&msg);
4045 if (GetCapture() != infoPtr->hwnd)
4046 return 0;
4049 ReleaseCapture();
4050 return 0;
4054 static LRESULT
4055 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4057 TREEVIEW_ITEM *item;
4058 TVHITTESTINFO hit;
4060 TRACE("\n");
4061 SetFocus(infoPtr->hwnd);
4063 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4065 /* If there is pending 'edit label' event - kill it now */
4066 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4069 hit.pt.x = (short)LOWORD(lParam);
4070 hit.pt.y = (short)HIWORD(lParam);
4072 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4073 if (!item)
4074 return 0;
4075 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4077 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4078 { /* FIXME! */
4079 switch (hit.flags)
4081 case TVHT_ONITEMRIGHT:
4082 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4083 break;
4085 case TVHT_ONITEMINDENT:
4086 if (!(infoPtr->dwStyle & TVS_HASLINES))
4088 break;
4090 else
4092 int level = hit.pt.x / infoPtr->uIndent;
4093 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4095 while (item->iLevel > level)
4097 item = item->parent;
4100 /* fall through */
4103 case TVHT_ONITEMLABEL:
4104 case TVHT_ONITEMICON:
4105 case TVHT_ONITEMBUTTON:
4106 TREEVIEW_Toggle(infoPtr, item, TRUE);
4107 break;
4109 case TVHT_ONITEMSTATEICON:
4110 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4111 TREEVIEW_ToggleItemState(infoPtr, item);
4112 else
4113 TREEVIEW_Toggle(infoPtr, item, TRUE);
4114 break;
4117 return TRUE;
4121 static LRESULT
4122 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4124 HWND hwnd = infoPtr->hwnd;
4125 TVHITTESTINFO ht;
4126 BOOL bTrack, bDoLabelEdit;
4128 /* If Edit control is active - kill it and return.
4129 * The best way to do it is to set focus to itself.
4130 * Edit control subclassed procedure will automatically call
4131 * EndEditLabelNow.
4133 if (infoPtr->hwndEdit)
4135 SetFocus(hwnd);
4136 return 0;
4139 ht.pt.x = (short)LOWORD(lParam);
4140 ht.pt.y = (short)HIWORD(lParam);
4142 TREEVIEW_HitTest(infoPtr, &ht);
4143 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4145 /* update focusedItem and redraw both items */
4146 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4148 infoPtr->focusedItem = ht.hItem;
4149 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4150 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4153 bTrack = (ht.flags & TVHT_ONITEM)
4154 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4157 * If the style allows editing and the node is already selected
4158 * and the click occurred on the item label...
4160 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4161 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4163 /* Send NM_CLICK right away */
4164 if (!bTrack)
4165 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4166 goto setfocus;
4168 if (ht.flags & TVHT_ONITEMBUTTON)
4170 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4171 goto setfocus;
4173 else if (bTrack)
4174 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4175 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4177 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4178 infoPtr->dropItem = ht.hItem;
4180 /* clean up focusedItem as we dragged and won't select this item */
4181 if(infoPtr->focusedItem)
4183 /* refresh the item that was focused */
4184 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4185 infoPtr->focusedItem = NULL;
4187 /* refresh the selected item to return the filled background */
4188 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4191 return 0;
4195 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4196 goto setfocus;
4198 if (bDoLabelEdit)
4200 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4201 KillTimer(hwnd, TV_EDIT_TIMER);
4203 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4204 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4206 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4208 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4210 /* Select the current item */
4211 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4212 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4214 else if (ht.flags & TVHT_ONITEMSTATEICON)
4216 /* TVS_CHECKBOXES requires us to toggle the current state */
4217 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4218 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4221 setfocus:
4222 SetFocus(hwnd);
4223 return 0;
4227 static LRESULT
4228 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4230 TVHITTESTINFO ht;
4232 if (infoPtr->hwndEdit)
4234 SetFocus(infoPtr->hwnd);
4235 return 0;
4238 ht.pt.x = (short)LOWORD(lParam);
4239 ht.pt.y = (short)HIWORD(lParam);
4241 TREEVIEW_HitTest(infoPtr, &ht);
4243 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4245 if (ht.hItem)
4247 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4248 infoPtr->dropItem = ht.hItem;
4251 else
4253 SetFocus(infoPtr->hwnd);
4254 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4257 return 0;
4260 static LRESULT
4261 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4263 TVHITTESTINFO ht;
4265 ht.pt = *pPt;
4267 TREEVIEW_HitTest(infoPtr, &ht);
4269 if (ht.hItem)
4271 /* Change to screen coordinate for WM_CONTEXTMENU */
4272 ClientToScreen(infoPtr->hwnd, &ht.pt);
4274 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4275 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4276 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4278 return 0;
4282 static LRESULT
4283 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4285 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4286 INT cx, cy;
4287 HDC hdc, htopdc;
4288 HWND hwtop;
4289 HBITMAP hbmp, hOldbmp;
4290 SIZE size;
4291 RECT rc;
4292 HFONT hOldFont;
4294 TRACE("\n");
4296 if (!(infoPtr->himlNormal))
4297 return 0;
4299 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4300 return 0;
4302 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4304 hwtop = GetDesktopWindow();
4305 htopdc = GetDC(hwtop);
4306 hdc = CreateCompatibleDC(htopdc);
4308 hOldFont = SelectObject(hdc, infoPtr->hFont);
4310 if (dragItem->pszText)
4311 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4312 &size);
4313 else
4314 GetTextExtentPoint32A(hdc, "", 0, &size);
4316 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4317 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4318 hOldbmp = SelectObject(hdc, hbmp);
4320 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4321 size.cx += cx;
4322 if (cy > size.cy)
4323 size.cy = cy;
4325 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4326 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4327 ILD_NORMAL);
4330 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4331 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4334 /* draw item text */
4336 SetRect(&rc, cx, 0, size.cx, size.cy);
4338 if (dragItem->pszText)
4339 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4340 DT_LEFT);
4342 SelectObject(hdc, hOldFont);
4343 SelectObject(hdc, hOldbmp);
4345 ImageList_Add(infoPtr->dragList, hbmp, 0);
4347 DeleteDC(hdc);
4348 DeleteObject(hbmp);
4349 ReleaseDC(hwtop, htopdc);
4351 return (LRESULT)infoPtr->dragList;
4354 /* Selection ************************************************************/
4356 static LRESULT
4357 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4358 INT cause)
4360 TREEVIEW_ITEM *prevSelect;
4362 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4364 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4365 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4366 newSelect ? newSelect->state : 0);
4368 /* reset and redraw focusedItem if focusedItem was set so we don't */
4369 /* have to worry about the previously focused item when we set a new one */
4370 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4371 infoPtr->focusedItem = NULL;
4373 switch (action)
4375 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4376 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4377 /* Fall through */
4378 case TVGN_CARET:
4379 prevSelect = infoPtr->selectedItem;
4381 if (prevSelect == newSelect) {
4382 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4383 break;
4386 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4387 TVN_SELCHANGINGW,
4388 cause,
4389 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4390 prevSelect,
4391 newSelect))
4392 return FALSE;
4394 if (prevSelect)
4395 prevSelect->state &= ~TVIS_SELECTED;
4396 if (newSelect)
4397 newSelect->state |= TVIS_SELECTED;
4399 infoPtr->selectedItem = newSelect;
4401 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4403 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4404 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4406 TREEVIEW_SendTreeviewNotify(infoPtr,
4407 TVN_SELCHANGEDW,
4408 cause,
4409 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4410 prevSelect,
4411 newSelect);
4412 break;
4414 case TVGN_DROPHILITE:
4415 prevSelect = infoPtr->dropItem;
4417 if (prevSelect)
4418 prevSelect->state &= ~TVIS_DROPHILITED;
4420 infoPtr->dropItem = newSelect;
4422 if (newSelect)
4423 newSelect->state |= TVIS_DROPHILITED;
4425 TREEVIEW_Invalidate(infoPtr, prevSelect);
4426 TREEVIEW_Invalidate(infoPtr, newSelect);
4427 break;
4429 case TVGN_FIRSTVISIBLE:
4430 if (newSelect != NULL)
4432 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4433 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4434 TREEVIEW_Invalidate(infoPtr, NULL);
4436 break;
4439 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4440 return TRUE;
4443 /* FIXME: handle NM_KILLFOCUS etc */
4444 static LRESULT
4445 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4447 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4449 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4450 return FALSE;
4452 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4454 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4455 return FALSE;
4457 TREEVIEW_SingleExpand(infoPtr, selection, item);
4459 return TRUE;
4462 /*************************************************************************
4463 * TREEVIEW_ProcessLetterKeys
4465 * Processes keyboard messages generated by pressing the letter keys
4466 * on the keyboard.
4467 * What this does is perform a case insensitive search from the
4468 * current position with the following quirks:
4469 * - If two chars or more are pressed in quick succession we search
4470 * for the corresponding string (e.g. 'abc').
4471 * - If there is a delay we wipe away the current search string and
4472 * restart with just that char.
4473 * - If the user keeps pressing the same character, whether slowly or
4474 * fast, so that the search string is entirely composed of this
4475 * character ('aaaaa' for instance), then we search for first item
4476 * that starting with that character.
4477 * - If the user types the above character in quick succession, then
4478 * we must also search for the corresponding string ('aaaaa'), and
4479 * go to that string if there is a match.
4481 * RETURNS
4483 * Zero.
4485 * BUGS
4487 * - The current implementation has a list of characters it will
4488 * accept and it ignores everything else. In particular it will
4489 * ignore accentuated characters which seems to match what
4490 * Windows does. But I'm not sure it makes sense to follow
4491 * Windows there.
4492 * - We don't sound a beep when the search fails.
4493 * - The search should start from the focused item, not from the selected
4494 * item. One reason for this is to allow for multiple selections in trees.
4495 * But currently infoPtr->focusedItem does not seem very usable.
4497 * SEE ALSO
4499 * TREEVIEW_ProcessLetterKeys
4501 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4503 HTREEITEM nItem;
4504 HTREEITEM endidx,idx;
4505 TVITEMEXW item;
4506 WCHAR buffer[MAX_PATH];
4507 DWORD timestamp,elapsed;
4509 /* simple parameter checking */
4510 if (!charCode || !keyData) return 0;
4512 /* only allow the valid WM_CHARs through */
4513 if (!isalnum(charCode) &&
4514 charCode != '.' && charCode != '`' && charCode != '!' &&
4515 charCode != '@' && charCode != '#' && charCode != '$' &&
4516 charCode != '%' && charCode != '^' && charCode != '&' &&
4517 charCode != '*' && charCode != '(' && charCode != ')' &&
4518 charCode != '-' && charCode != '_' && charCode != '+' &&
4519 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4520 charCode != '}' && charCode != '[' && charCode != '{' &&
4521 charCode != '/' && charCode != '?' && charCode != '>' &&
4522 charCode != '<' && charCode != ',' && charCode != '~')
4523 return 0;
4525 /* compute how much time elapsed since last keypress */
4526 timestamp = GetTickCount();
4527 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4528 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4529 } else {
4530 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4533 /* update the search parameters */
4534 infoPtr->lastKeyPressTimestamp=timestamp;
4535 if (elapsed < KEY_DELAY) {
4536 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4537 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4539 if (infoPtr->charCode != charCode) {
4540 infoPtr->charCode=charCode=0;
4542 } else {
4543 infoPtr->charCode=charCode;
4544 infoPtr->szSearchParam[0]=charCode;
4545 infoPtr->nSearchParamLength=1;
4546 /* Redundant with the 1 char string */
4547 charCode=0;
4550 /* and search from the current position */
4551 nItem=NULL;
4552 if (infoPtr->selectedItem != NULL) {
4553 endidx=infoPtr->selectedItem;
4554 /* if looking for single character match,
4555 * then we must always move forward
4557 if (infoPtr->nSearchParamLength == 1)
4558 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4559 else
4560 idx=endidx;
4561 } else {
4562 endidx=NULL;
4563 idx=infoPtr->root->firstChild;
4565 do {
4566 /* At the end point, sort out wrapping */
4567 if (idx == NULL) {
4569 /* If endidx is null, stop at the last item (ie top to bottom) */
4570 if (endidx == NULL)
4571 break;
4573 /* Otherwise, start again at the very beginning */
4574 idx=infoPtr->root->firstChild;
4576 /* But if we are stopping on the first child, end now! */
4577 if (idx == endidx) break;
4580 /* get item */
4581 ZeroMemory(&item, sizeof(item));
4582 item.mask = TVIF_TEXT;
4583 item.hItem = idx;
4584 item.pszText = buffer;
4585 item.cchTextMax = sizeof(buffer);
4586 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4588 /* check for a match */
4589 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4590 nItem=idx;
4591 break;
4592 } else if ( (charCode != 0) && (nItem == NULL) &&
4593 (nItem != infoPtr->selectedItem) &&
4594 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4595 /* This would work but we must keep looking for a longer match */
4596 nItem=idx;
4598 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4599 } while (idx != endidx);
4601 if (nItem != NULL) {
4602 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4603 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4607 return 0;
4610 /* Scrolling ************************************************************/
4612 static LRESULT
4613 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4615 int viscount;
4616 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4617 HTREEITEM newFirstVisible = NULL;
4618 int visible_pos = -1;
4620 if (!TREEVIEW_ValidItem(infoPtr, item))
4621 return FALSE;
4623 if (!ISVISIBLE(item))
4625 /* Expand parents as necessary. */
4626 HTREEITEM parent;
4628 /* see if we are trying to ensure that root is visible */
4629 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4630 parent = item->parent;
4631 else
4632 parent = item; /* this item is the topmost item */
4634 while (parent != infoPtr->root)
4636 if (!(parent->state & TVIS_EXPANDED))
4637 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4639 parent = parent->parent;
4643 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4645 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4646 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4648 if (hasFirstVisible)
4649 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4651 if (visible_pos < 0)
4653 /* item is before the start of the list: put it at the top. */
4654 newFirstVisible = item;
4656 else if (visible_pos >= viscount
4657 /* Sometimes, before we are displayed, GVC is 0, causing us to
4658 * spuriously scroll up. */
4659 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4661 /* item is past the end of the list. */
4662 int scroll = visible_pos - viscount;
4664 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4665 scroll + 1);
4668 if (bHScroll)
4670 /* Scroll window so item's text is visible as much as possible */
4671 /* Calculation of amount of extra space is taken from EditLabel code */
4672 INT pos, x;
4673 TEXTMETRICW textMetric;
4674 HDC hdc = GetWindowDC(infoPtr->hwnd);
4676 x = item->textWidth;
4678 GetTextMetricsW(hdc, &textMetric);
4679 ReleaseDC(infoPtr->hwnd, hdc);
4681 x += (textMetric.tmMaxCharWidth * 2);
4682 x = max(x, textMetric.tmMaxCharWidth * 3);
4684 if (item->textOffset < 0)
4685 pos = item->textOffset;
4686 else if (item->textOffset + x > infoPtr->clientWidth)
4688 if (x > infoPtr->clientWidth)
4689 pos = item->textOffset;
4690 else
4691 pos = item->textOffset + x - infoPtr->clientWidth;
4693 else
4694 pos = 0;
4696 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4699 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4701 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4703 return TRUE;
4706 return FALSE;
4709 static VOID
4710 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4711 TREEVIEW_ITEM *newFirstVisible,
4712 BOOL bUpdateScrollPos)
4714 int gap_size;
4716 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4718 if (newFirstVisible != NULL)
4720 /* Prevent an empty gap from appearing at the bottom... */
4721 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4722 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4724 if (gap_size > 0)
4726 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4727 -gap_size);
4729 /* ... unless we just don't have enough items. */
4730 if (newFirstVisible == NULL)
4731 newFirstVisible = infoPtr->root->firstChild;
4735 if (infoPtr->firstVisible != newFirstVisible)
4737 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4739 infoPtr->firstVisible = newFirstVisible;
4740 TREEVIEW_Invalidate(infoPtr, NULL);
4742 else
4744 TREEVIEW_ITEM *item;
4745 int scroll = infoPtr->uItemHeight *
4746 (infoPtr->firstVisible->visibleOrder
4747 - newFirstVisible->visibleOrder);
4749 infoPtr->firstVisible = newFirstVisible;
4751 for (item = infoPtr->root->firstChild; item != NULL;
4752 item = TREEVIEW_GetNextListItem(infoPtr, item))
4754 item->rect.top += scroll;
4755 item->rect.bottom += scroll;
4758 if (bUpdateScrollPos)
4759 SetScrollPos(infoPtr->hwnd, SB_VERT,
4760 newFirstVisible->visibleOrder, TRUE);
4762 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4767 /************************************************************************
4768 * VScroll is always in units of visible items. i.e. we always have a
4769 * visible item aligned to the top of the control. (Unless we have no
4770 * items at all.)
4772 static LRESULT
4773 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4775 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4776 TREEVIEW_ITEM *newFirstVisible = NULL;
4778 int nScrollCode = LOWORD(wParam);
4780 TRACE("wp %lx\n", wParam);
4782 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4783 return 0;
4785 if (!oldFirstVisible)
4787 assert(infoPtr->root->firstChild == NULL);
4788 return 0;
4791 switch (nScrollCode)
4793 case SB_TOP:
4794 newFirstVisible = infoPtr->root->firstChild;
4795 break;
4797 case SB_BOTTOM:
4798 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4799 break;
4801 case SB_LINEUP:
4802 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4803 break;
4805 case SB_LINEDOWN:
4806 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4807 break;
4809 case SB_PAGEUP:
4810 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4811 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4812 break;
4814 case SB_PAGEDOWN:
4815 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4816 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4817 break;
4819 case SB_THUMBTRACK:
4820 case SB_THUMBPOSITION:
4821 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4822 infoPtr->root->firstChild,
4823 (LONG)(SHORT)HIWORD(wParam));
4824 break;
4826 case SB_ENDSCROLL:
4827 return 0;
4830 if (newFirstVisible != NULL)
4832 if (newFirstVisible != oldFirstVisible)
4833 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4834 nScrollCode != SB_THUMBTRACK);
4835 else if (nScrollCode == SB_THUMBPOSITION)
4836 SetScrollPos(infoPtr->hwnd, SB_VERT,
4837 newFirstVisible->visibleOrder, TRUE);
4840 return 0;
4843 static LRESULT
4844 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4846 int maxWidth;
4847 int scrollX = infoPtr->scrollX;
4848 int nScrollCode = LOWORD(wParam);
4850 TRACE("wp %lx\n", wParam);
4852 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4853 return FALSE;
4855 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4856 /* shall never occur */
4857 if (maxWidth <= 0)
4859 scrollX = 0;
4860 goto scroll;
4863 switch (nScrollCode)
4865 case SB_LINELEFT:
4866 scrollX -= infoPtr->uItemHeight;
4867 break;
4868 case SB_LINERIGHT:
4869 scrollX += infoPtr->uItemHeight;
4870 break;
4871 case SB_PAGELEFT:
4872 scrollX -= infoPtr->clientWidth;
4873 break;
4874 case SB_PAGERIGHT:
4875 scrollX += infoPtr->clientWidth;
4876 break;
4878 case SB_THUMBTRACK:
4879 case SB_THUMBPOSITION:
4880 scrollX = (int)(SHORT)HIWORD(wParam);
4881 break;
4883 case SB_ENDSCROLL:
4884 return 0;
4887 if (scrollX > maxWidth)
4888 scrollX = maxWidth;
4889 else if (scrollX < 0)
4890 scrollX = 0;
4892 scroll:
4893 if (scrollX != infoPtr->scrollX)
4895 TREEVIEW_ITEM *item;
4896 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4898 for (item = infoPtr->root->firstChild; item != NULL;
4899 item = TREEVIEW_GetNextListItem(infoPtr, item))
4901 item->linesOffset += scroll_pixels;
4902 item->stateOffset += scroll_pixels;
4903 item->imageOffset += scroll_pixels;
4904 item->textOffset += scroll_pixels;
4907 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4908 infoPtr->scrollX = scrollX;
4909 UpdateWindow(infoPtr->hwnd);
4912 if (nScrollCode != SB_THUMBTRACK)
4913 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4915 return 0;
4918 static LRESULT
4919 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4921 short gcWheelDelta;
4922 UINT pulScrollLines = 3;
4924 if (wParam & (MK_SHIFT | MK_CONTROL))
4925 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4927 if (infoPtr->firstVisible == NULL)
4928 return TRUE;
4930 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4932 gcWheelDelta = -(short)HIWORD(wParam);
4933 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4935 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4937 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4938 int maxDy = infoPtr->maxVisibleOrder;
4940 if (newDy > maxDy)
4941 newDy = maxDy;
4943 if (newDy < 0)
4944 newDy = 0;
4946 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4948 return TRUE;
4951 /* Create/Destroy *******************************************************/
4953 static void
4954 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
4956 RECT rc;
4957 HBITMAP hbm, hbmOld;
4958 HDC hdc, hdcScreen;
4959 int nIndex;
4961 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4963 hdcScreen = GetDC(0);
4965 hdc = CreateCompatibleDC(hdcScreen);
4966 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4967 hbmOld = SelectObject(hdc, hbm);
4969 SetRect(&rc, 0, 0, 48, 16);
4970 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4972 SetRect(&rc, 18, 2, 30, 14);
4973 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4974 DFCS_BUTTONCHECK|DFCS_FLAT);
4976 SetRect(&rc, 34, 2, 46, 14);
4977 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4978 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4980 SelectObject(hdc, hbmOld);
4981 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4982 comctl32_color.clrWindow);
4983 TRACE("checkbox index %d\n", nIndex);
4985 DeleteObject(hbm);
4986 DeleteDC(hdc);
4987 ReleaseDC(0, hdcScreen);
4989 infoPtr->stateImageWidth = 16;
4990 infoPtr->stateImageHeight = 16;
4993 static LRESULT
4994 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4996 RECT rcClient;
4997 TREEVIEW_INFO *infoPtr;
4998 LOGFONTW lf;
5000 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5002 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
5004 if (infoPtr == NULL)
5006 ERR("could not allocate info memory!\n");
5007 return 0;
5010 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5012 infoPtr->hwnd = hwnd;
5013 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5014 infoPtr->Timer = 0;
5015 infoPtr->uNumItems = 0;
5016 infoPtr->cdmode = 0;
5017 infoPtr->uScrollTime = 300; /* milliseconds */
5018 infoPtr->bRedraw = TRUE;
5020 GetClientRect(hwnd, &rcClient);
5022 /* No scroll bars yet. */
5023 infoPtr->clientWidth = rcClient.right;
5024 infoPtr->clientHeight = rcClient.bottom;
5025 infoPtr->uInternalStatus = 0;
5027 infoPtr->treeWidth = 0;
5028 infoPtr->treeHeight = 0;
5030 infoPtr->uIndent = MINIMUM_INDENT;
5031 infoPtr->selectedItem = NULL;
5032 infoPtr->focusedItem = NULL;
5033 infoPtr->hotItem = NULL;
5034 infoPtr->editItem = NULL;
5035 infoPtr->firstVisible = NULL;
5036 infoPtr->maxVisibleOrder = 0;
5037 infoPtr->dropItem = NULL;
5038 infoPtr->insertMarkItem = NULL;
5039 infoPtr->insertBeforeorAfter = 0;
5040 /* dragList */
5042 infoPtr->scrollX = 0;
5044 infoPtr->clrBk = CLR_NONE; /* use system color */
5045 infoPtr->clrText = CLR_NONE; /* use system color */
5046 infoPtr->clrLine = CLR_DEFAULT;
5047 infoPtr->clrInsertMark = CLR_DEFAULT;
5049 /* hwndToolTip */
5051 infoPtr->hwndEdit = NULL;
5052 infoPtr->wpEditOrig = NULL;
5053 infoPtr->bIgnoreEditKillFocus = FALSE;
5054 infoPtr->bLabelChanged = FALSE;
5056 infoPtr->himlNormal = NULL;
5057 infoPtr->himlState = NULL;
5058 infoPtr->normalImageWidth = 0;
5059 infoPtr->normalImageHeight = 0;
5060 infoPtr->stateImageWidth = 0;
5061 infoPtr->stateImageHeight = 0;
5063 infoPtr->items = DPA_Create(16);
5065 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5066 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5067 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5068 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5069 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5071 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5073 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5074 infoPtr->root->state = TVIS_EXPANDED;
5075 infoPtr->root->iLevel = -1;
5076 infoPtr->root->visibleOrder = -1;
5078 infoPtr->hwndNotify = lpcs->hwndParent;
5079 infoPtr->hwndToolTip = 0;
5081 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5083 /* Determine what type of notify should be issued */
5084 /* 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 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5093 TREEVIEW_InitCheckboxes(infoPtr);
5095 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5096 ShowScrollBar(hwnd, SB_VERT, FALSE);
5097 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5099 OpenThemeData (hwnd, themeClass);
5101 return 0;
5105 static LRESULT
5106 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5108 TRACE("\n");
5110 /* free item data */
5111 TREEVIEW_RemoveTree(infoPtr);
5112 /* root isn't freed with other items */
5113 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5114 DPA_Destroy(infoPtr->items);
5116 /* tool tip is automatically destroyed: we are its owner */
5118 /* Restore original wndproc */
5119 if (infoPtr->hwndEdit)
5120 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5121 (DWORD_PTR)infoPtr->wpEditOrig);
5123 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5125 /* Deassociate treeview from the window before doing anything drastic. */
5126 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5128 DeleteObject(infoPtr->hDefaultFont);
5129 DeleteObject(infoPtr->hBoldFont);
5130 DeleteObject(infoPtr->hUnderlineFont);
5131 Free(infoPtr);
5133 return 0;
5136 /* Miscellaneous Messages ***********************************************/
5138 static LRESULT
5139 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5141 static const struct
5143 unsigned char code;
5145 scroll[] =
5147 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5148 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5149 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5150 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5151 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5152 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5153 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5154 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5155 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5156 #undef SCROLL_ENTRY
5159 if (key >= VK_PRIOR && key <= VK_DOWN)
5161 unsigned char code = scroll[key - VK_PRIOR].code;
5163 (((code & (1 << 7)) == (SB_HORZ << 7))
5164 ? TREEVIEW_HScroll
5165 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5168 return 0;
5171 /************************************************************************
5172 * TREEVIEW_KeyDown
5174 * VK_UP Move selection to the previous non-hidden item.
5175 * VK_DOWN Move selection to the next non-hidden item.
5176 * VK_HOME Move selection to the first item.
5177 * VK_END Move selection to the last item.
5178 * VK_LEFT If expanded then collapse, otherwise move to parent.
5179 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5180 * VK_ADD Expand.
5181 * VK_SUBTRACT Collapse.
5182 * VK_MULTIPLY Expand all.
5183 * VK_PRIOR Move up GetVisibleCount items.
5184 * VK_NEXT Move down GetVisibleCount items.
5185 * VK_BACK Move to parent.
5186 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5188 static LRESULT
5189 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5191 /* If it is non-NULL and different, it will be selected and visible. */
5192 TREEVIEW_ITEM *newSelection = NULL;
5194 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5196 TRACE("%lx\n", wParam);
5198 if (prevItem == NULL)
5199 return FALSE;
5201 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5202 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5204 switch (wParam)
5206 case VK_UP:
5207 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5208 if (!newSelection)
5209 newSelection = infoPtr->root->firstChild;
5210 break;
5212 case VK_DOWN:
5213 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5214 break;
5216 case VK_HOME:
5217 newSelection = infoPtr->root->firstChild;
5218 break;
5220 case VK_END:
5221 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5222 break;
5224 case VK_LEFT:
5225 if (prevItem->state & TVIS_EXPANDED)
5227 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5229 else if (prevItem->parent != infoPtr->root)
5231 newSelection = prevItem->parent;
5233 break;
5235 case VK_RIGHT:
5236 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5238 if (!(prevItem->state & TVIS_EXPANDED))
5239 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5240 else
5242 newSelection = prevItem->firstChild;
5246 break;
5248 case VK_MULTIPLY:
5249 TREEVIEW_ExpandAll(infoPtr, prevItem);
5250 break;
5252 case VK_ADD:
5253 if (!(prevItem->state & TVIS_EXPANDED))
5254 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5255 break;
5257 case VK_SUBTRACT:
5258 if (prevItem->state & TVIS_EXPANDED)
5259 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5260 break;
5262 case VK_PRIOR:
5263 newSelection
5264 = TREEVIEW_GetListItem(infoPtr, prevItem,
5265 -TREEVIEW_GetVisibleCount(infoPtr));
5266 break;
5268 case VK_NEXT:
5269 newSelection
5270 = TREEVIEW_GetListItem(infoPtr, prevItem,
5271 TREEVIEW_GetVisibleCount(infoPtr));
5272 break;
5274 case VK_BACK:
5275 newSelection = prevItem->parent;
5276 if (newSelection == infoPtr->root)
5277 newSelection = NULL;
5278 break;
5280 case VK_SPACE:
5281 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5282 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5283 break;
5286 if (newSelection && newSelection != prevItem)
5288 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5289 TVC_BYKEYBOARD))
5291 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5295 return FALSE;
5298 static LRESULT
5299 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5301 /* remove hot effect from item */
5302 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5303 infoPtr->hotItem = NULL;
5305 return 0;
5308 static LRESULT
5309 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5311 POINT pt;
5312 TRACKMOUSEEVENT trackinfo;
5313 TREEVIEW_ITEM * item;
5315 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5317 /* fill in the TRACKMOUSEEVENT struct */
5318 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5319 trackinfo.dwFlags = TME_QUERY;
5320 trackinfo.hwndTrack = infoPtr->hwnd;
5322 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5323 _TrackMouseEvent(&trackinfo);
5325 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5326 if(!(trackinfo.dwFlags & TME_LEAVE))
5328 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5329 trackinfo.hwndTrack = infoPtr->hwnd;
5330 /* do it as fast as possible, minimal systimer latency will be used */
5331 trackinfo.dwHoverTime = 1;
5333 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5334 /* and can properly deactivate the hot item */
5335 _TrackMouseEvent(&trackinfo);
5338 pt.x = (short)LOWORD(lParam);
5339 pt.y = (short)HIWORD(lParam);
5341 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5343 if (item != infoPtr->hotItem)
5345 /* redraw old hot item */
5346 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5347 infoPtr->hotItem = item;
5348 /* redraw new hot item */
5349 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5352 return 0;
5355 /* Draw themed border */
5356 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5358 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5359 HDC dc;
5360 RECT r;
5361 HRGN cliprgn;
5362 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5363 cyEdge = GetSystemMetrics (SM_CYEDGE);
5365 if (!theme)
5366 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5368 GetWindowRect(infoPtr->hwnd, &r);
5370 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5371 r.right - cxEdge, r.bottom - cyEdge);
5372 if (region != (HRGN)1)
5373 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5374 OffsetRect(&r, -r.left, -r.top);
5376 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5377 OffsetRect(&r, -r.left, -r.top);
5379 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5380 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5381 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5382 ReleaseDC(infoPtr->hwnd, dc);
5384 /* Call default proc to get the scrollbars etc. painted */
5385 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5387 return TRUE;
5390 static LRESULT
5391 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5393 LPNMHDR lpnmh = (LPNMHDR)lParam;
5395 if (lpnmh->code == PGN_CALCSIZE) {
5396 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5398 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5399 lppgc->iWidth = infoPtr->treeWidth;
5400 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5401 infoPtr->treeWidth, infoPtr->clientWidth);
5403 else {
5404 lppgc->iHeight = infoPtr->treeHeight;
5405 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5406 infoPtr->treeHeight, infoPtr->clientHeight);
5408 return 0;
5410 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5413 static LRESULT
5414 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5416 if (wParam == SIZE_RESTORED)
5418 infoPtr->clientWidth = (short)LOWORD(lParam);
5419 infoPtr->clientHeight = (short)HIWORD(lParam);
5421 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5422 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5423 TREEVIEW_UpdateScrollBars(infoPtr);
5425 else
5427 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5430 TREEVIEW_Invalidate(infoPtr, NULL);
5431 return 0;
5434 static void TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5436 TREEVIEW_ITEM *child = item->firstChild;
5438 item->state &= ~TVIS_STATEIMAGEMASK;
5439 item->state |= INDEXTOSTATEIMAGEMASK(1);
5441 while (child)
5443 TREEVIEW_ITEM *next = child->nextSibling;
5444 TREEVIEW_ResetImageStateIndex(infoPtr, child);
5445 child = next;
5449 static LRESULT
5450 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5452 TRACE("(%lx %lx)\n", wParam, lParam);
5454 if (wParam == GWL_STYLE)
5456 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5458 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5460 if (dwNewStyle & TVS_CHECKBOXES)
5462 TREEVIEW_InitCheckboxes(infoPtr);
5463 TRACE("checkboxes enabled\n");
5465 /* set all items to state image index 1 */
5466 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5468 else
5470 FIXME("tried to disable checkboxes\n");
5474 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5476 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5478 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5479 TRACE("tooltips enabled\n");
5481 else
5483 DestroyWindow(infoPtr->hwndToolTip);
5484 infoPtr->hwndToolTip = 0;
5485 TRACE("tooltips disabled\n");
5489 infoPtr->dwStyle = dwNewStyle;
5492 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5493 TREEVIEW_UpdateScrollBars(infoPtr);
5494 TREEVIEW_Invalidate(infoPtr, NULL);
5496 return 0;
5499 static LRESULT
5500 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5502 POINT pt;
5503 TREEVIEW_ITEM * item;
5504 NMMOUSE nmmouse;
5506 GetCursorPos(&pt);
5507 ScreenToClient(infoPtr->hwnd, &pt);
5509 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5511 memset(&nmmouse, 0, sizeof(nmmouse));
5512 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5513 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5514 nmmouse.hdr.code = NM_SETCURSOR;
5515 if (item)
5517 nmmouse.dwItemSpec = (DWORD_PTR)item;
5518 nmmouse.dwItemData = item->lParam;
5520 nmmouse.pt.x = 0;
5521 nmmouse.pt.y = 0;
5522 nmmouse.dwHitInfo = lParam;
5523 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, &nmmouse.hdr))
5524 return 0;
5526 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5528 SetCursor(infoPtr->hcurHand);
5529 return 0;
5531 else
5532 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5535 static LRESULT
5536 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5538 TRACE("\n");
5540 if (!infoPtr->selectedItem)
5542 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5543 TVC_UNKNOWN);
5546 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5547 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5548 return 0;
5551 static LRESULT
5552 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5554 TRACE("\n");
5556 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5557 UpdateWindow(infoPtr->hwnd);
5558 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5559 return 0;
5562 /* update theme after a WM_THEMECHANGED message */
5563 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5565 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5566 CloseThemeData (theme);
5567 OpenThemeData (infoPtr->hwnd, themeClass);
5568 return 0;
5572 static LRESULT WINAPI
5573 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5575 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5577 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5579 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5580 else
5582 if (uMsg == WM_CREATE)
5583 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5584 else
5585 goto def;
5588 switch (uMsg)
5590 case TVM_CREATEDRAGIMAGE:
5591 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5593 case TVM_DELETEITEM:
5594 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5596 case TVM_EDITLABELA:
5597 case TVM_EDITLABELW:
5598 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5600 case TVM_ENDEDITLABELNOW:
5601 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5603 case TVM_ENSUREVISIBLE:
5604 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5606 case TVM_EXPAND:
5607 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5609 case TVM_GETBKCOLOR:
5610 return TREEVIEW_GetBkColor(infoPtr);
5612 case TVM_GETCOUNT:
5613 return TREEVIEW_GetCount(infoPtr);
5615 case TVM_GETEDITCONTROL:
5616 return TREEVIEW_GetEditControl(infoPtr);
5618 case TVM_GETIMAGELIST:
5619 return TREEVIEW_GetImageList(infoPtr, wParam);
5621 case TVM_GETINDENT:
5622 return TREEVIEW_GetIndent(infoPtr);
5624 case TVM_GETINSERTMARKCOLOR:
5625 return TREEVIEW_GetInsertMarkColor(infoPtr);
5627 case TVM_GETISEARCHSTRINGA:
5628 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5629 return 0;
5631 case TVM_GETISEARCHSTRINGW:
5632 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5633 return 0;
5635 case TVM_GETITEMA:
5636 case TVM_GETITEMW:
5637 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5638 uMsg == TVM_GETITEMW);
5639 case TVM_GETITEMHEIGHT:
5640 return TREEVIEW_GetItemHeight(infoPtr);
5642 case TVM_GETITEMRECT:
5643 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5645 case TVM_GETITEMSTATE:
5646 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5648 case TVM_GETLINECOLOR:
5649 return TREEVIEW_GetLineColor(infoPtr);
5651 case TVM_GETNEXTITEM:
5652 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5654 case TVM_GETSCROLLTIME:
5655 return TREEVIEW_GetScrollTime(infoPtr);
5657 case TVM_GETTEXTCOLOR:
5658 return TREEVIEW_GetTextColor(infoPtr);
5660 case TVM_GETTOOLTIPS:
5661 return TREEVIEW_GetToolTips(infoPtr);
5663 case TVM_GETUNICODEFORMAT:
5664 return TREEVIEW_GetUnicodeFormat(infoPtr);
5666 case TVM_GETVISIBLECOUNT:
5667 return TREEVIEW_GetVisibleCount(infoPtr);
5669 case TVM_HITTEST:
5670 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5672 case TVM_INSERTITEMA:
5673 case TVM_INSERTITEMW:
5674 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5675 uMsg == TVM_INSERTITEMW);
5676 case TVM_SELECTITEM:
5677 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5679 case TVM_SETBKCOLOR:
5680 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5682 case TVM_SETIMAGELIST:
5683 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5685 case TVM_SETINDENT:
5686 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5688 case TVM_SETINSERTMARK:
5689 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5691 case TVM_SETINSERTMARKCOLOR:
5692 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5694 case TVM_SETITEMA:
5695 case TVM_SETITEMW:
5696 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5697 uMsg == TVM_SETITEMW);
5698 case TVM_SETLINECOLOR:
5699 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5701 case TVM_SETITEMHEIGHT:
5702 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5704 case TVM_SETSCROLLTIME:
5705 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5707 case TVM_SETTEXTCOLOR:
5708 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5710 case TVM_SETTOOLTIPS:
5711 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5713 case TVM_SETUNICODEFORMAT:
5714 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5716 case TVM_SORTCHILDREN:
5717 return TREEVIEW_SortChildren(infoPtr, lParam);
5719 case TVM_SORTCHILDRENCB:
5720 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5722 case WM_CHAR:
5723 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5725 case WM_COMMAND:
5726 return TREEVIEW_Command(infoPtr, wParam, lParam);
5728 case WM_DESTROY:
5729 return TREEVIEW_Destroy(infoPtr);
5731 /* WM_ENABLE */
5733 case WM_ERASEBKGND:
5734 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5736 case WM_GETDLGCODE:
5737 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5739 case WM_GETFONT:
5740 return TREEVIEW_GetFont(infoPtr);
5742 case WM_HSCROLL:
5743 return TREEVIEW_HScroll(infoPtr, wParam);
5745 case WM_KEYDOWN:
5746 return TREEVIEW_KeyDown(infoPtr, wParam);
5748 case WM_KILLFOCUS:
5749 return TREEVIEW_KillFocus(infoPtr);
5751 case WM_LBUTTONDBLCLK:
5752 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5754 case WM_LBUTTONDOWN:
5755 return TREEVIEW_LButtonDown(infoPtr, lParam);
5757 /* WM_MBUTTONDOWN */
5759 case WM_MOUSELEAVE:
5760 return TREEVIEW_MouseLeave(infoPtr);
5762 case WM_MOUSEMOVE:
5763 return TREEVIEW_MouseMove(infoPtr, lParam);
5765 case WM_NCLBUTTONDOWN:
5766 if (infoPtr->hwndEdit)
5767 SetFocus(infoPtr->hwnd);
5768 goto def;
5770 case WM_NCPAINT:
5771 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5773 case WM_NOTIFY:
5774 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5776 case WM_NOTIFYFORMAT:
5777 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5779 case WM_PRINTCLIENT:
5780 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5782 case WM_PAINT:
5783 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5785 case WM_RBUTTONDOWN:
5786 return TREEVIEW_RButtonDown(infoPtr, lParam);
5788 case WM_SETCURSOR:
5789 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5791 case WM_SETFOCUS:
5792 return TREEVIEW_SetFocus(infoPtr);
5794 case WM_SETFONT:
5795 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5797 case WM_SETREDRAW:
5798 return TREEVIEW_SetRedraw(infoPtr, wParam);
5800 case WM_SIZE:
5801 return TREEVIEW_Size(infoPtr, wParam, lParam);
5803 case WM_STYLECHANGED:
5804 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5806 case WM_SYSCOLORCHANGE:
5807 COMCTL32_RefreshSysColors();
5808 return 0;
5810 /* WM_SYSKEYDOWN */
5812 case WM_TIMER:
5813 return TREEVIEW_HandleTimer(infoPtr, wParam);
5815 case WM_THEMECHANGED:
5816 return TREEVIEW_ThemeChanged (infoPtr);
5818 case WM_VSCROLL:
5819 return TREEVIEW_VScroll(infoPtr, wParam);
5821 /* WM_WININICHANGE */
5823 case WM_MOUSEWHEEL:
5824 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5826 case WM_DRAWITEM:
5827 TRACE("drawItem\n");
5828 goto def;
5830 default:
5831 /* This mostly catches MFC and Delphi messages. :( */
5832 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5833 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5834 def:
5835 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5840 /* Class Registration ***************************************************/
5842 VOID
5843 TREEVIEW_Register(void)
5845 WNDCLASSW wndClass;
5847 TRACE("\n");
5849 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5850 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5851 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5852 wndClass.cbClsExtra = 0;
5853 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5855 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5856 wndClass.hbrBackground = 0;
5857 wndClass.lpszClassName = WC_TREEVIEWW;
5859 RegisterClassW(&wndClass);
5863 VOID
5864 TREEVIEW_Unregister(void)
5866 UnregisterClassW(WC_TREEVIEWW, NULL);
5870 /* Tree Verification ****************************************************/
5872 static inline void
5873 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5875 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5876 const TREEVIEW_ITEM *item)
5878 assert(infoPtr != NULL);
5879 assert(item != NULL);
5881 /* both NULL, or both non-null */
5882 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5884 assert(item->firstChild != item);
5885 assert(item->lastChild != item);
5887 if (item->firstChild)
5889 assert(item->firstChild->parent == item);
5890 assert(item->firstChild->prevSibling == NULL);
5893 if (item->lastChild)
5895 assert(item->lastChild->parent == item);
5896 assert(item->lastChild->nextSibling == NULL);
5899 assert(item->nextSibling != item);
5900 if (item->nextSibling)
5902 assert(item->nextSibling->parent == item->parent);
5903 assert(item->nextSibling->prevSibling == item);
5906 assert(item->prevSibling != item);
5907 if (item->prevSibling)
5909 assert(item->prevSibling->parent == item->parent);
5910 assert(item->prevSibling->nextSibling == item);
5914 static inline void
5915 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5917 assert(item != NULL);
5919 assert(item->parent != NULL);
5920 assert(item->parent != item);
5921 assert(item->iLevel == item->parent->iLevel + 1);
5923 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5925 TREEVIEW_VerifyItemCommon(infoPtr, item);
5927 TREEVIEW_VerifyChildren(infoPtr, item);
5930 static inline void
5931 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5933 const TREEVIEW_ITEM *child;
5934 assert(item != NULL);
5936 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5937 TREEVIEW_VerifyItem(infoPtr, child);
5940 static inline void
5941 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5943 TREEVIEW_ITEM *root = infoPtr->root;
5945 assert(root != NULL);
5946 assert(root->iLevel == -1);
5947 assert(root->parent == NULL);
5948 assert(root->prevSibling == NULL);
5950 TREEVIEW_VerifyItemCommon(infoPtr, root);
5952 TREEVIEW_VerifyChildren(infoPtr, root);
5955 static void
5956 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5958 if (!TRACE_ON(treeview)) return;
5960 assert(infoPtr != NULL);
5961 TREEVIEW_VerifyRoot(infoPtr);