Assorted typos fixes.
[wine.git] / dlls / comctl32 / treeview.c
blobb2eed3c2d0b91bd710f79d372075b46e8cd97a60
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 tagTREEVIEW_INFO
72 HWND hwnd;
73 HWND hwndNotify; /* Owner window to send notifications to */
74 DWORD dwStyle;
75 HTREEITEM root;
76 UINT uInternalStatus;
77 INT Timer;
78 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
79 INT cdmode; /* last custom draw setting */
80 UINT uScrollTime; /* max. time for scrolling in milliseconds */
81 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
83 UINT uItemHeight; /* item height */
84 BOOL bHeightSet;
86 LONG clientWidth; /* width of control window */
87 LONG clientHeight; /* height of control window */
89 LONG treeWidth; /* width of visible tree items */
90 LONG treeHeight; /* height of visible tree items */
92 UINT uIndent; /* indentation in pixels */
93 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
94 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
95 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
96 HTREEITEM editItem; /* item being edited with builtin edit box */
98 HTREEITEM firstVisible; /* handle to first visible item */
99 LONG maxVisibleOrder;
100 HTREEITEM dropItem; /* handle to item selected by drag cursor */
101 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
102 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
103 HIMAGELIST dragList; /* Bitmap of dragged item */
104 LONG scrollX;
105 INT wheelRemainder;
106 COLORREF clrBk;
107 COLORREF clrText;
108 COLORREF clrLine;
109 COLORREF clrInsertMark;
110 HFONT hFont;
111 HFONT hDefaultFont;
112 HFONT hBoldFont;
113 HFONT hUnderlineFont;
114 HFONT hBoldUnderlineFont;
115 HCURSOR hcurHand;
116 HWND hwndToolTip;
118 HWND hwndEdit;
119 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
120 BOOL bIgnoreEditKillFocus;
121 BOOL bLabelChanged;
123 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
124 HIMAGELIST himlNormal;
125 int normalImageHeight;
126 int normalImageWidth;
127 HIMAGELIST himlState;
128 int stateImageHeight;
129 int stateImageWidth;
130 HDPA items;
132 DWORD lastKeyPressTimestamp;
133 WPARAM charCode;
134 INT nSearchParamLength;
135 WCHAR szSearchParam[ MAX_PATH ];
136 } TREEVIEW_INFO;
138 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
140 HTREEITEM parent; /* handle to parent or 0 if at root */
141 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
142 HTREEITEM firstChild; /* handle to first child or 0 if no child */
144 UINT callbackMask;
145 UINT state;
146 UINT stateMask;
147 LPWSTR pszText;
148 int cchTextMax;
149 int iImage;
150 int iSelectedImage;
151 int iExpandedImage;
152 int cChildren;
153 LPARAM lParam;
154 int iIntegral; /* item height multiplier (1 is normal) */
155 int iLevel; /* indentation level:0=root level */
156 HTREEITEM lastChild;
157 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
158 RECT rect;
159 LONG linesOffset;
160 LONG stateOffset;
161 LONG imageOffset;
162 LONG textOffset;
163 LONG textWidth; /* horizontal text extent for pszText */
164 LONG visibleOrder; /* visible ordering, 0 is first visible item */
165 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
166 } TREEVIEW_ITEM;
168 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
169 #define KEY_DELAY 450
171 /* bitflags for infoPtr->uInternalStatus */
173 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
174 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
175 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
176 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
177 #define TV_RDRAG 0x10 /* ditto Rbutton */
178 #define TV_RDRAGGING 0x20
180 /* bitflags for infoPtr->timer */
182 #define TV_EDIT_TIMER 2
183 #define TV_EDIT_TIMER_SET 2
185 #define TEXT_CALLBACK_SIZE 260
187 #define TREEVIEW_LEFT_MARGIN 8
189 #define MINIMUM_INDENT 19
191 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
193 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
194 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
195 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
197 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
198 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
199 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
200 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
202 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
205 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
208 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
210 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
211 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
212 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
213 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
214 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
215 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
217 /* Random Utilities *****************************************************/
218 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
220 /* Returns the treeview private data if hwnd is a treeview.
221 * Otherwise returns an undefined value. */
222 static inline TREEVIEW_INFO *
223 TREEVIEW_GetInfoPtr(HWND hwnd)
225 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
228 /* Don't call this. Nothing wants an item index. */
229 static inline int
230 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
232 return DPA_GetPtrIndex(infoPtr->items, handle);
235 /* Checks if item has changed and needs to be redrawn */
236 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
237 const TVITEMEXW *tvChange)
239 /* Number of children has changed */
240 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
241 return TRUE;
243 /* Image has changed and it's not a callback */
244 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
245 tiNew->iImage != I_IMAGECALLBACK)
246 return TRUE;
248 /* Selected image has changed and it's not a callback */
249 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
250 tiNew->iSelectedImage != I_IMAGECALLBACK)
251 return TRUE;
253 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
254 tiNew->iExpandedImage != I_IMAGECALLBACK)
255 return TRUE;
257 /* Text has changed and it's not a callback */
258 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
259 tiNew->pszText != LPSTR_TEXTCALLBACKW)
260 return TRUE;
262 /* Indent has changed */
263 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
264 return TRUE;
266 /* Item state has changed */
267 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
268 return TRUE;
270 return FALSE;
273 /***************************************************************************
274 * This method checks that handle is an item for this tree.
276 static BOOL
277 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
279 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
281 TRACE("invalid item %p\n", handle);
282 return FALSE;
284 else
285 return TRUE;
288 static HFONT
289 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
291 LOGFONTW font;
293 GetObjectW(hOrigFont, sizeof(font), &font);
294 font.lfWeight = FW_BOLD;
295 return CreateFontIndirectW(&font);
298 static HFONT
299 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
301 LOGFONTW font;
303 GetObjectW(hOrigFont, sizeof(font), &font);
304 font.lfUnderline = TRUE;
305 return CreateFontIndirectW(&font);
308 static HFONT
309 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont)
311 LOGFONTW font;
313 GetObjectW(hfont, sizeof(font), &font);
314 font.lfWeight = FW_BOLD;
315 font.lfUnderline = TRUE;
316 return CreateFontIndirectW(&font);
319 static inline HFONT
320 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
322 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
323 return item->state & TVIS_BOLD ? infoPtr->hBoldUnderlineFont : infoPtr->hUnderlineFont;
324 if (item->state & TVIS_BOLD)
325 return infoPtr->hBoldFont;
326 return infoPtr->hFont;
329 /* for trace/debugging purposes only */
330 static const char *
331 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
333 if (item == NULL) return "<null item>";
334 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
335 if (item->pszText == NULL) return "<null>";
336 return debugstr_w(item->pszText);
339 /* An item is not a child of itself. */
340 static BOOL
341 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
345 child = child->parent;
346 if (child == parent) return TRUE;
347 } while (child != NULL);
349 return FALSE;
353 /* Tree Traversal *******************************************************/
355 /***************************************************************************
356 * This method returns the last expanded sibling or child child item
357 * of a tree node
359 static TREEVIEW_ITEM *
360 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
362 if (!item) return NULL;
364 while (item->lastChild)
366 if (item->state & TVIS_EXPANDED)
367 item = item->lastChild;
368 else
369 break;
372 if (item == infoPtr->root)
373 return NULL;
375 return item;
378 /***************************************************************************
379 * This method returns the previous non-hidden item in the list not
380 * considering the tree hierarchy.
382 static TREEVIEW_ITEM *
383 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
385 if (tvItem->prevSibling)
387 /* This item has a prevSibling, get the last item in the sibling's tree. */
388 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
390 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
391 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
392 else
393 return upItem;
395 else
397 /* this item does not have a prevSibling, get the parent */
398 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
403 /***************************************************************************
404 * This method returns the next physical item in the treeview not
405 * considering the tree hierarchy.
407 static TREEVIEW_ITEM *
408 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
411 * If this item has children and is expanded, return the first child
413 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
415 return tvItem->firstChild;
420 * try to get the sibling
422 if (tvItem->nextSibling)
423 return tvItem->nextSibling;
426 * Otherwise, get the parent's sibling.
428 while (tvItem->parent)
430 tvItem = tvItem->parent;
432 if (tvItem->nextSibling)
433 return tvItem->nextSibling;
436 return NULL;
439 /***************************************************************************
440 * This method returns the nth item starting at the given item. It returns
441 * the last item (or first) we we run out of items.
443 * Will scroll backward if count is <0.
444 * forward if count is >0.
446 static TREEVIEW_ITEM *
447 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
448 LONG count)
450 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
451 TREEVIEW_ITEM *previousItem;
453 assert(item != NULL);
455 if (count > 0)
457 next_item = TREEVIEW_GetNextListItem;
459 else if (count < 0)
461 count = -count;
462 next_item = TREEVIEW_GetPrevListItem;
464 else
465 return item;
469 previousItem = item;
470 item = next_item(infoPtr, item);
472 } while (--count && item != NULL);
475 return item ? item : previousItem;
478 /* Notifications ************************************************************/
480 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
482 if (!infoPtr->bNtfUnicode) {
483 switch (code) {
484 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
485 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
486 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
487 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
488 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
489 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
490 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
491 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
492 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
493 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
494 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
495 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
498 return code;
501 static inline BOOL
502 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPNMHDR pnmh)
504 TRACE("wParam=%ld, lParam=%p\n", wParam, pnmh);
505 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, (LPARAM)pnmh);
508 static BOOL
509 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
511 NMHDR nmhdr;
512 HWND hwnd = infoPtr->hwnd;
514 TRACE("%d\n", code);
515 nmhdr.hwndFrom = hwnd;
516 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
517 nmhdr.code = get_notifycode(infoPtr, code);
519 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, &nmhdr);
522 static VOID
523 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
525 tvItem->mask = mask;
526 tvItem->hItem = item;
527 tvItem->state = item->state;
528 tvItem->stateMask = 0;
529 tvItem->iImage = item->iImage;
530 tvItem->iSelectedImage = item->iSelectedImage;
531 tvItem->cChildren = item->cChildren;
532 tvItem->lParam = item->lParam;
534 if(mask & TVIF_TEXT)
536 if (!infoPtr->bNtfUnicode)
538 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
539 tvItem->pszText = Alloc (tvItem->cchTextMax);
540 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
542 else
544 tvItem->cchTextMax = item->cchTextMax;
545 tvItem->pszText = item->pszText;
548 else
550 tvItem->cchTextMax = 0;
551 tvItem->pszText = NULL;
555 static BOOL
556 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
557 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
559 HWND hwnd = infoPtr->hwnd;
560 NMTREEVIEWW nmhdr;
561 BOOL ret;
563 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
564 code, action, oldItem, newItem);
566 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
568 nmhdr.hdr.hwndFrom = hwnd;
569 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
570 nmhdr.hdr.code = get_notifycode(infoPtr, code);
571 nmhdr.action = action;
573 if (oldItem)
574 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
576 if (newItem)
577 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
579 nmhdr.ptDrag.x = 0;
580 nmhdr.ptDrag.y = 0;
582 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
583 if (!infoPtr->bNtfUnicode)
585 Free(nmhdr.itemOld.pszText);
586 Free(nmhdr.itemNew.pszText);
588 return ret;
591 static BOOL
592 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
593 HTREEITEM dragItem, POINT pt)
595 HWND hwnd = infoPtr->hwnd;
596 NMTREEVIEWW nmhdr;
598 TRACE("code:%d dragitem:%p\n", code, dragItem);
600 nmhdr.hdr.hwndFrom = hwnd;
601 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
602 nmhdr.hdr.code = get_notifycode(infoPtr, code);
603 nmhdr.action = 0;
604 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
605 nmhdr.itemNew.hItem = dragItem;
606 nmhdr.itemNew.state = dragItem->state;
607 nmhdr.itemNew.lParam = dragItem->lParam;
609 nmhdr.ptDrag.x = pt.x;
610 nmhdr.ptDrag.y = pt.y;
612 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
616 static BOOL
617 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
618 HDC hdc, RECT rc)
620 HWND hwnd = infoPtr->hwnd;
621 NMTVCUSTOMDRAW nmcdhdr;
622 LPNMCUSTOMDRAW nmcd;
624 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
626 nmcd = &nmcdhdr.nmcd;
627 nmcd->hdr.hwndFrom = hwnd;
628 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
629 nmcd->hdr.code = NM_CUSTOMDRAW;
630 nmcd->dwDrawStage = dwDrawStage;
631 nmcd->hdc = hdc;
632 nmcd->rc = rc;
633 nmcd->dwItemSpec = 0;
634 nmcd->uItemState = 0;
635 nmcd->lItemlParam = 0;
636 nmcdhdr.clrText = infoPtr->clrText;
637 nmcdhdr.clrTextBk = infoPtr->clrBk;
638 nmcdhdr.iLevel = 0;
640 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr.nmcd.hdr);
645 /* FIXME: need to find out when the flags in uItemState need to be set */
647 static BOOL
648 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
649 TREEVIEW_ITEM *item, UINT uItemDrawState,
650 NMTVCUSTOMDRAW *nmcdhdr)
652 HWND hwnd = infoPtr->hwnd;
653 LPNMCUSTOMDRAW nmcd;
654 DWORD dwDrawStage;
655 DWORD_PTR dwItemSpec;
656 UINT uItemState;
658 dwDrawStage = CDDS_ITEM | uItemDrawState;
659 dwItemSpec = (DWORD_PTR)item;
660 uItemState = 0;
661 if (item->state & TVIS_SELECTED)
662 uItemState |= CDIS_SELECTED;
663 if (item == infoPtr->selectedItem)
664 uItemState |= CDIS_FOCUS;
665 if (item == infoPtr->hotItem)
666 uItemState |= CDIS_HOT;
668 nmcd = &nmcdhdr->nmcd;
669 nmcd->hdr.hwndFrom = hwnd;
670 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
671 nmcd->hdr.code = NM_CUSTOMDRAW;
672 nmcd->dwDrawStage = dwDrawStage;
673 nmcd->hdc = hdc;
674 nmcd->rc = item->rect;
675 nmcd->dwItemSpec = dwItemSpec;
676 nmcd->uItemState = uItemState;
677 nmcd->lItemlParam = item->lParam;
678 nmcdhdr->iLevel = item->iLevel;
680 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
681 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
682 nmcd->uItemState, nmcd->lItemlParam);
684 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr->nmcd.hdr);
687 static BOOL
688 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
690 HWND hwnd = infoPtr->hwnd;
691 NMTVDISPINFOW tvdi;
692 BOOL ret;
694 tvdi.hdr.hwndFrom = hwnd;
695 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
696 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
698 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
699 &tvdi.item, editItem);
701 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
703 if (!infoPtr->bNtfUnicode)
704 Free(tvdi.item.pszText);
706 return ret;
709 static void
710 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
711 UINT mask)
713 NMTVDISPINFOEXW callback;
714 HWND hwnd = infoPtr->hwnd;
716 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
717 mask &= item->callbackMask;
719 if (mask == 0) return;
721 callback.hdr.hwndFrom = hwnd;
722 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
723 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
725 /* 'state' always contains valid value, as well as 'lParam'.
726 * All other parameters are uninitialized.
728 callback.item.pszText = item->pszText;
729 callback.item.cchTextMax = item->cchTextMax;
730 callback.item.mask = mask;
731 callback.item.hItem = item;
732 callback.item.state = item->state;
733 callback.item.lParam = item->lParam;
735 /* If text is changed we need to recalculate textWidth */
736 if (mask & TVIF_TEXT)
737 item->textWidth = 0;
739 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, &callback.hdr);
740 TRACE("resulting code 0x%08x\n", callback.hdr.code);
742 /* It may have changed due to a call to SetItem. */
743 mask &= item->callbackMask;
745 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
747 /* Instead of copying text into our buffer user specified his own */
748 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
749 LPWSTR newText;
750 int buflen;
751 int len = MultiByteToWideChar( CP_ACP, 0,
752 (LPSTR)callback.item.pszText, -1,
753 NULL, 0);
754 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
755 newText = ReAlloc(item->pszText, buflen);
757 TRACE("returned str %s, len=%d, buflen=%d\n",
758 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
760 if (newText)
762 item->pszText = newText;
763 MultiByteToWideChar( CP_ACP, 0,
764 (LPSTR)callback.item.pszText, -1,
765 item->pszText, buflen/sizeof(WCHAR));
766 item->cchTextMax = buflen/sizeof(WCHAR);
768 /* If ReAlloc fails we have nothing to do, but keep original text */
770 else {
771 int len = max(lstrlenW(callback.item.pszText) + 1,
772 TEXT_CALLBACK_SIZE);
773 LPWSTR newText = ReAlloc(item->pszText, len);
775 TRACE("returned wstr %s, len=%d\n",
776 debugstr_w(callback.item.pszText), len);
778 if (newText)
780 item->pszText = newText;
781 strcpyW(item->pszText, callback.item.pszText);
782 item->cchTextMax = len;
784 /* If ReAlloc fails we have nothing to do, but keep original text */
787 else if (mask & TVIF_TEXT) {
788 /* User put text into our buffer, that is ok unless A string */
789 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
790 LPWSTR newText;
791 int buflen;
792 int len = MultiByteToWideChar( CP_ACP, 0,
793 (LPSTR)callback.item.pszText, -1,
794 NULL, 0);
795 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
796 newText = Alloc(buflen);
798 TRACE("same buffer str %s, len=%d, buflen=%d\n",
799 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
801 if (newText)
803 LPWSTR oldText = item->pszText;
804 item->pszText = newText;
805 MultiByteToWideChar( CP_ACP, 0,
806 (LPSTR)callback.item.pszText, -1,
807 item->pszText, buflen/sizeof(WCHAR));
808 item->cchTextMax = buflen/sizeof(WCHAR);
809 Free(oldText);
814 if (mask & TVIF_IMAGE)
815 item->iImage = callback.item.iImage;
817 if (mask & TVIF_SELECTEDIMAGE)
818 item->iSelectedImage = callback.item.iSelectedImage;
820 if (mask & TVIF_EXPANDEDIMAGE)
821 item->iExpandedImage = callback.item.iExpandedImage;
823 if (mask & TVIF_CHILDREN)
824 item->cChildren = callback.item.cChildren;
826 if (callback.item.mask & TVIF_STATE)
828 item->state &= ~callback.item.stateMask;
829 item->state |= (callback.item.state & callback.item.stateMask);
832 /* These members are now permanently set. */
833 if (callback.item.mask & TVIF_DI_SETITEM)
834 item->callbackMask &= ~callback.item.mask;
837 /***************************************************************************
838 * This function uses cChildren field to decide whether the item has
839 * children or not.
840 * Note: if this returns TRUE, the child items may not actually exist,
841 * they could be virtual.
843 * Just use item->firstChild to check for physical children.
845 static BOOL
846 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
848 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
850 return item->cChildren > 0;
853 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
855 INT format;
857 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
859 if (nCommand != NF_REQUERY) return 0;
861 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
862 TRACE("format=%d\n", format);
864 /* Invalid format returned by NF_QUERY defaults to ANSI*/
865 if (format != NFR_ANSI && format != NFR_UNICODE)
866 format = NFR_ANSI;
868 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
870 return format;
873 /* Item Position ********************************************************/
875 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
876 static VOID
877 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
879 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
880 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
881 > TVS_LINESATROOT);
883 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
884 - infoPtr->scrollX;
885 item->stateOffset = item->linesOffset + infoPtr->uIndent;
886 item->imageOffset = item->stateOffset
887 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
888 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
891 static VOID
892 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
894 HDC hdc;
895 HFONT hOldFont=0;
896 SIZE sz;
898 /* DRAW's OM docker creates items like this */
899 if (item->pszText == NULL)
901 item->textWidth = 0;
902 return;
905 if (hDC != 0)
907 hdc = hDC;
909 else
911 hdc = GetDC(infoPtr->hwnd);
912 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
915 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
916 item->textWidth = sz.cx;
918 if (hDC == 0)
920 SelectObject(hdc, hOldFont);
921 ReleaseDC(0, hdc);
925 static VOID
926 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
928 item->rect.top = infoPtr->uItemHeight *
929 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
931 item->rect.bottom = item->rect.top
932 + infoPtr->uItemHeight * item->iIntegral - 1;
934 item->rect.left = 0;
935 item->rect.right = infoPtr->clientWidth;
938 /* We know that only items after start need their order updated. */
939 static void
940 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
942 TREEVIEW_ITEM *item;
943 int order;
945 if (!start)
947 start = infoPtr->root->firstChild;
948 order = 0;
950 else
951 order = start->visibleOrder;
953 for (item = start; item != NULL;
954 item = TREEVIEW_GetNextListItem(infoPtr, item))
956 if (!ISVISIBLE(item) && order > 0)
957 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
958 item->visibleOrder = order;
959 order += item->iIntegral;
962 infoPtr->maxVisibleOrder = order;
964 for (item = start; item != NULL;
965 item = TREEVIEW_GetNextListItem(infoPtr, item))
967 TREEVIEW_ComputeItemRect(infoPtr, item);
972 /* Update metrics of all items in selected subtree.
973 * root must be expanded
975 static VOID
976 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
978 TREEVIEW_ITEM *sibling;
979 HDC hdc;
980 HFONT hOldFont;
982 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
983 return;
985 root->state &= ~TVIS_EXPANDED;
986 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
987 root->state |= TVIS_EXPANDED;
989 hdc = GetDC(infoPtr->hwnd);
990 hOldFont = SelectObject(hdc, infoPtr->hFont);
992 for (; root != sibling;
993 root = TREEVIEW_GetNextListItem(infoPtr, root))
995 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
997 if (root->callbackMask & TVIF_TEXT)
998 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
1000 if (root->textWidth == 0)
1002 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
1003 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
1007 SelectObject(hdc, hOldFont);
1008 ReleaseDC(infoPtr->hwnd, hdc);
1011 /* Item Allocation **********************************************************/
1013 static TREEVIEW_ITEM *
1014 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1016 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1018 if (!newItem)
1019 return NULL;
1021 /* I_IMAGENONE would make more sense but this is neither what is
1022 * documented (MSDN doesn't specify) nor what Windows actually does
1023 * (it sets it to zero)... and I can so imagine an application using
1024 * inc/dec to toggle the images. */
1025 newItem->iImage = 0;
1026 newItem->iSelectedImage = 0;
1027 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1028 newItem->infoPtr = infoPtr;
1030 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1032 Free(newItem);
1033 return NULL;
1036 return newItem;
1039 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1040 * free item->pszText. */
1041 static void
1042 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1044 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1045 if (infoPtr->selectedItem == item)
1046 infoPtr->selectedItem = NULL;
1047 if (infoPtr->hotItem == item)
1048 infoPtr->hotItem = NULL;
1049 if (infoPtr->focusedItem == item)
1050 infoPtr->focusedItem = NULL;
1051 if (infoPtr->firstVisible == item)
1052 infoPtr->firstVisible = NULL;
1053 if (infoPtr->dropItem == item)
1054 infoPtr->dropItem = NULL;
1055 if (infoPtr->insertMarkItem == item)
1056 infoPtr->insertMarkItem = NULL;
1057 Free(item);
1061 /* Item Insertion *******************************************************/
1063 /***************************************************************************
1064 * This method inserts newItem before sibling as a child of parent.
1065 * sibling can be NULL, but only if parent has no children.
1067 static void
1068 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1069 TREEVIEW_ITEM *parent)
1071 assert(parent != NULL);
1073 if (sibling != NULL)
1075 assert(sibling->parent == parent);
1077 if (sibling->prevSibling != NULL)
1078 sibling->prevSibling->nextSibling = newItem;
1080 newItem->prevSibling = sibling->prevSibling;
1081 sibling->prevSibling = newItem;
1083 else
1084 newItem->prevSibling = NULL;
1086 newItem->nextSibling = sibling;
1088 if (parent->firstChild == sibling)
1089 parent->firstChild = newItem;
1091 if (parent->lastChild == NULL)
1092 parent->lastChild = newItem;
1095 /***************************************************************************
1096 * This method inserts newItem after sibling as a child of parent.
1097 * sibling can be NULL, but only if parent has no children.
1099 static void
1100 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1101 TREEVIEW_ITEM *parent)
1103 assert(parent != NULL);
1105 if (sibling != NULL)
1107 assert(sibling->parent == parent);
1109 if (sibling->nextSibling != NULL)
1110 sibling->nextSibling->prevSibling = newItem;
1112 newItem->nextSibling = sibling->nextSibling;
1113 sibling->nextSibling = newItem;
1115 else
1116 newItem->nextSibling = NULL;
1118 newItem->prevSibling = sibling;
1120 if (parent->lastChild == sibling)
1121 parent->lastChild = newItem;
1123 if (parent->firstChild == NULL)
1124 parent->firstChild = newItem;
1127 static BOOL
1128 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1129 const TVITEMEXW *tvItem, BOOL isW)
1131 UINT callbackClear = 0;
1132 UINT callbackSet = 0;
1134 TRACE("item %p\n", item);
1135 /* Do this first in case it fails. */
1136 if (tvItem->mask & TVIF_TEXT)
1138 item->textWidth = 0; /* force width recalculation */
1139 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1141 int len;
1142 LPWSTR newText;
1143 if (isW)
1144 len = lstrlenW(tvItem->pszText) + 1;
1145 else
1146 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1148 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1150 if (newText == NULL) return FALSE;
1152 callbackClear |= TVIF_TEXT;
1154 item->pszText = newText;
1155 item->cchTextMax = len;
1156 if (isW)
1157 lstrcpynW(item->pszText, tvItem->pszText, len);
1158 else
1159 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1160 item->pszText, len);
1162 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1164 else
1166 callbackSet |= TVIF_TEXT;
1168 item->pszText = ReAlloc(item->pszText,
1169 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1170 item->cchTextMax = TEXT_CALLBACK_SIZE;
1171 TRACE("setting callback, item %p\n", item);
1175 if (tvItem->mask & TVIF_CHILDREN)
1177 item->cChildren = tvItem->cChildren;
1179 if (item->cChildren == I_CHILDRENCALLBACK)
1180 callbackSet |= TVIF_CHILDREN;
1181 else
1182 callbackClear |= TVIF_CHILDREN;
1185 if (tvItem->mask & TVIF_IMAGE)
1187 item->iImage = tvItem->iImage;
1189 if (item->iImage == I_IMAGECALLBACK)
1190 callbackSet |= TVIF_IMAGE;
1191 else
1192 callbackClear |= TVIF_IMAGE;
1195 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1197 item->iSelectedImage = tvItem->iSelectedImage;
1199 if (item->iSelectedImage == I_IMAGECALLBACK)
1200 callbackSet |= TVIF_SELECTEDIMAGE;
1201 else
1202 callbackClear |= TVIF_SELECTEDIMAGE;
1205 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1207 item->iExpandedImage = tvItem->iExpandedImage;
1209 if (item->iExpandedImage == I_IMAGECALLBACK)
1210 callbackSet |= TVIF_EXPANDEDIMAGE;
1211 else
1212 callbackClear |= TVIF_EXPANDEDIMAGE;
1215 if (tvItem->mask & TVIF_PARAM)
1216 item->lParam = tvItem->lParam;
1218 /* If the application sets TVIF_INTEGRAL without
1219 * supplying a TVITEMEX structure, it's toast. */
1220 if (tvItem->mask & TVIF_INTEGRAL)
1221 item->iIntegral = tvItem->iIntegral;
1223 if (tvItem->mask & TVIF_STATE)
1225 TRACE("prevstate,state,mask:%x,%x,%x\n", item->state, tvItem->state,
1226 tvItem->stateMask);
1227 item->state &= ~tvItem->stateMask;
1228 item->state |= (tvItem->state & tvItem->stateMask);
1231 if (tvItem->mask & TVIF_STATEEX)
1233 FIXME("New extended state: %x\n", tvItem->uStateEx);
1236 item->callbackMask |= callbackSet;
1237 item->callbackMask &= ~callbackClear;
1239 return TRUE;
1242 /* Note that the new item is pre-zeroed. */
1243 static LRESULT
1244 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1246 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1247 HTREEITEM insertAfter;
1248 TREEVIEW_ITEM *newItem, *parentItem;
1249 BOOL bTextUpdated = FALSE;
1251 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1253 parentItem = infoPtr->root;
1255 else
1257 parentItem = ptdi->hParent;
1259 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1261 WARN("invalid parent %p\n", parentItem);
1262 return 0;
1266 insertAfter = ptdi->hInsertAfter;
1268 /* Validate this now for convenience. */
1269 switch ((DWORD_PTR)insertAfter)
1271 case (DWORD_PTR)TVI_FIRST:
1272 case (DWORD_PTR)TVI_LAST:
1273 case (DWORD_PTR)TVI_SORT:
1274 break;
1276 default:
1277 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1278 insertAfter->parent != parentItem)
1280 WARN("invalid insert after %p\n", insertAfter);
1281 insertAfter = TVI_LAST;
1285 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1286 (tvItem->mask & TVIF_TEXT)
1287 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1288 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1289 : "<no label>");
1291 newItem = TREEVIEW_AllocateItem(infoPtr);
1292 if (newItem == NULL)
1293 return 0;
1295 newItem->parent = parentItem;
1296 newItem->iIntegral = 1;
1297 newItem->visibleOrder = -1;
1299 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1300 return 0;
1302 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1304 infoPtr->uNumItems++;
1306 switch ((DWORD_PTR)insertAfter)
1308 case (DWORD_PTR)TVI_FIRST:
1310 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1311 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1312 if (infoPtr->firstVisible == originalFirst)
1313 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1315 break;
1317 case (DWORD_PTR)TVI_LAST:
1318 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1319 break;
1321 /* hInsertAfter names a specific item we want to insert after */
1322 default:
1323 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1324 break;
1326 case (DWORD_PTR)TVI_SORT:
1328 TREEVIEW_ITEM *aChild;
1329 TREEVIEW_ITEM *previousChild = NULL;
1330 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1331 BOOL bItemInserted = FALSE;
1333 aChild = parentItem->firstChild;
1335 bTextUpdated = TRUE;
1336 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1338 /* Iterate the parent children to see where we fit in */
1339 while (aChild != NULL)
1341 INT comp;
1343 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1344 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1346 if (comp < 0) /* we are smaller than the current one */
1348 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1349 if (infoPtr->firstVisible == originalFirst &&
1350 aChild == originalFirst)
1351 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1352 bItemInserted = TRUE;
1353 break;
1355 else if (comp > 0) /* we are bigger than the current one */
1357 previousChild = aChild;
1359 /* This will help us to exit if there is no more sibling */
1360 aChild = (aChild->nextSibling == 0)
1361 ? NULL
1362 : aChild->nextSibling;
1364 /* Look at the next item */
1365 continue;
1367 else if (comp == 0)
1370 * An item with this name is already existing, therefore,
1371 * we add after the one we found
1373 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1374 bItemInserted = TRUE;
1375 break;
1380 * we reach the end of the child list and the item has not
1381 * yet been inserted, therefore, insert it after the last child.
1383 if ((!bItemInserted) && (aChild == NULL))
1384 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1386 break;
1391 TRACE("new item %p; parent %p, mask %x\n", newItem,
1392 newItem->parent, tvItem->mask);
1394 newItem->iLevel = newItem->parent->iLevel + 1;
1396 if (newItem->parent->cChildren == 0)
1397 newItem->parent->cChildren = 1;
1399 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1401 if (STATEIMAGEINDEX(newItem->state) == 0)
1402 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1405 if (infoPtr->firstVisible == NULL)
1406 infoPtr->firstVisible = newItem;
1408 TREEVIEW_VerifyTree(infoPtr);
1410 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1412 if (parentItem == infoPtr->root ||
1413 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1415 TREEVIEW_ITEM *item;
1416 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1418 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1419 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1421 if (!bTextUpdated)
1422 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1424 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1425 TREEVIEW_UpdateScrollBars(infoPtr);
1427 * if the item was inserted in a visible part of the tree,
1428 * invalidate it, as well as those after it
1430 for (item = newItem;
1431 item != NULL;
1432 item = TREEVIEW_GetNextListItem(infoPtr, item))
1433 TREEVIEW_Invalidate(infoPtr, item);
1435 else
1437 /* refresh treeview if newItem is the first item inserted under parentItem */
1438 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1440 /* parent got '+' - update it */
1441 TREEVIEW_Invalidate(infoPtr, parentItem);
1445 return (LRESULT)newItem;
1448 /* Item Deletion ************************************************************/
1449 static void
1450 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1452 static void
1453 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1455 TREEVIEW_ITEM *kill = parentItem->firstChild;
1457 while (kill != NULL)
1459 TREEVIEW_ITEM *next = kill->nextSibling;
1461 TREEVIEW_RemoveItem(infoPtr, kill);
1463 kill = next;
1466 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1467 assert(parentItem->firstChild == NULL);
1468 assert(parentItem->lastChild == NULL);
1471 static void
1472 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1474 TREEVIEW_ITEM *parentItem = item->parent;
1476 assert(item != NULL);
1477 assert(item->parent != NULL); /* i.e. it must not be the root */
1479 if (parentItem->firstChild == item)
1480 parentItem->firstChild = item->nextSibling;
1482 if (parentItem->lastChild == item)
1483 parentItem->lastChild = item->prevSibling;
1485 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1486 && parentItem->cChildren > 0)
1487 parentItem->cChildren = 0;
1489 if (item->prevSibling)
1490 item->prevSibling->nextSibling = item->nextSibling;
1492 if (item->nextSibling)
1493 item->nextSibling->prevSibling = item->prevSibling;
1496 static void
1497 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1499 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1501 if (item->firstChild)
1502 TREEVIEW_RemoveAllChildren(infoPtr, item);
1504 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1505 TVIF_HANDLE | TVIF_PARAM, item, 0);
1507 TREEVIEW_UnlinkItem(item);
1509 infoPtr->uNumItems--;
1511 if (item->pszText != LPSTR_TEXTCALLBACKW)
1512 Free(item->pszText);
1514 TREEVIEW_FreeItem(infoPtr, item);
1518 /* Empty out the tree. */
1519 static void
1520 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1522 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1524 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1527 static LRESULT
1528 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1530 TREEVIEW_ITEM *newSelection = NULL;
1531 TREEVIEW_ITEM *newFirstVisible = NULL;
1532 TREEVIEW_ITEM *parent, *prev = NULL;
1533 BOOL visible = FALSE;
1535 if (item == TVI_ROOT || !item)
1537 TRACE("TVI_ROOT\n");
1538 parent = infoPtr->root;
1539 newSelection = NULL;
1540 visible = TRUE;
1541 TREEVIEW_RemoveTree(infoPtr);
1543 else
1545 if (!TREEVIEW_ValidItem(infoPtr, item))
1546 return FALSE;
1548 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1549 parent = item->parent;
1551 if (ISVISIBLE(item))
1553 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1554 visible = TRUE;
1557 if (infoPtr->selectedItem != NULL
1558 && (item == infoPtr->selectedItem
1559 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1561 if (item->nextSibling)
1562 newSelection = item->nextSibling;
1563 else if (item->parent != infoPtr->root)
1564 newSelection = item->parent;
1565 else
1566 newSelection = item->prevSibling;
1567 TRACE("newSelection = %p\n", newSelection);
1570 if (infoPtr->firstVisible == item)
1572 if (item->nextSibling)
1573 newFirstVisible = item->nextSibling;
1574 else if (item->prevSibling)
1575 newFirstVisible = item->prevSibling;
1576 else if (item->parent != infoPtr->root)
1577 newFirstVisible = item->parent;
1578 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1580 else
1581 newFirstVisible = infoPtr->firstVisible;
1583 TREEVIEW_RemoveItem(infoPtr, item);
1586 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1587 if (!infoPtr->selectedItem && newSelection)
1589 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1590 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1593 /* Validate insertMark dropItem.
1594 * hotItem ??? - used for comparison only.
1596 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1597 infoPtr->insertMarkItem = 0;
1599 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1600 infoPtr->dropItem = 0;
1602 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1603 newFirstVisible = infoPtr->root->firstChild;
1605 TREEVIEW_VerifyTree(infoPtr);
1607 if (!infoPtr->bRedraw) return TRUE;
1609 if (visible)
1611 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1612 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1613 TREEVIEW_UpdateScrollBars(infoPtr);
1614 TREEVIEW_Invalidate(infoPtr, NULL);
1616 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1618 /* parent lost '+/-' - update it */
1619 TREEVIEW_Invalidate(infoPtr, parent);
1622 return TRUE;
1626 /* Get/Set Messages *********************************************************/
1627 static LRESULT
1628 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1630 infoPtr->bRedraw = wParam != 0;
1632 if (infoPtr->bRedraw)
1634 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1635 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1636 TREEVIEW_UpdateScrollBars(infoPtr);
1637 TREEVIEW_Invalidate(infoPtr, NULL);
1639 return 0;
1642 static LRESULT
1643 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1645 TRACE("\n");
1646 return infoPtr->uIndent;
1649 static LRESULT
1650 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1652 TRACE("\n");
1654 if (newIndent < MINIMUM_INDENT)
1655 newIndent = MINIMUM_INDENT;
1657 if (infoPtr->uIndent != newIndent)
1659 infoPtr->uIndent = newIndent;
1660 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1661 TREEVIEW_UpdateScrollBars(infoPtr);
1662 TREEVIEW_Invalidate(infoPtr, NULL);
1665 return 0;
1669 static LRESULT
1670 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1672 TRACE("\n");
1673 return (LRESULT)infoPtr->hwndToolTip;
1676 static LRESULT
1677 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1679 HWND prevToolTip;
1681 TRACE("\n");
1682 prevToolTip = infoPtr->hwndToolTip;
1683 infoPtr->hwndToolTip = hwndTT;
1685 return (LRESULT)prevToolTip;
1688 static LRESULT
1689 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1691 BOOL rc = infoPtr->bNtfUnicode;
1692 infoPtr->bNtfUnicode = fUnicode;
1693 return rc;
1696 static LRESULT
1697 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1699 return infoPtr->bNtfUnicode;
1702 static LRESULT
1703 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1705 return infoPtr->uScrollTime;
1708 static LRESULT
1709 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1711 UINT uOldScrollTime = infoPtr->uScrollTime;
1713 infoPtr->uScrollTime = min(uScrollTime, 100);
1715 return uOldScrollTime;
1719 static LRESULT
1720 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1722 TRACE("\n");
1724 switch (wParam)
1726 case TVSIL_NORMAL:
1727 return (LRESULT)infoPtr->himlNormal;
1729 case TVSIL_STATE:
1730 return (LRESULT)infoPtr->himlState;
1732 default:
1733 return 0;
1737 #define TVHEIGHT_MIN 16
1738 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1740 /* Compute the natural height for items. */
1741 static UINT
1742 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1744 TEXTMETRICW tm;
1745 HDC hdc = GetDC(0);
1746 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1747 UINT height;
1749 /* Height is the maximum of:
1750 * 16 (a hack because our fonts are tiny), and
1751 * The text height + border & margin, and
1752 * The size of the normal image list
1754 GetTextMetricsW(hdc, &tm);
1755 SelectObject(hdc, hOldFont);
1756 ReleaseDC(0, hdc);
1758 height = TVHEIGHT_MIN;
1759 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1760 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1761 if (height < infoPtr->normalImageHeight)
1762 height = infoPtr->normalImageHeight;
1764 /* Round down, unless we support odd ("non even") heights. */
1765 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1766 height &= ~1;
1768 return height;
1771 static LRESULT
1772 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1774 HIMAGELIST himlOld = 0;
1775 int oldWidth = infoPtr->normalImageWidth;
1776 int oldHeight = infoPtr->normalImageHeight;
1778 TRACE("%u,%p\n", type, himlNew);
1780 switch (type)
1782 case TVSIL_NORMAL:
1783 himlOld = infoPtr->himlNormal;
1784 infoPtr->himlNormal = himlNew;
1786 if (himlNew)
1787 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1788 &infoPtr->normalImageHeight);
1789 else
1791 infoPtr->normalImageWidth = 0;
1792 infoPtr->normalImageHeight = 0;
1795 break;
1797 case TVSIL_STATE:
1798 himlOld = infoPtr->himlState;
1799 infoPtr->himlState = himlNew;
1801 if (himlNew)
1802 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1803 &infoPtr->stateImageHeight);
1804 else
1806 infoPtr->stateImageWidth = 0;
1807 infoPtr->stateImageHeight = 0;
1810 break;
1812 default:
1813 ERR("unknown imagelist type %u\n", type);
1816 if (oldWidth != infoPtr->normalImageWidth ||
1817 oldHeight != infoPtr->normalImageHeight)
1819 BOOL bRecalcVisible = FALSE;
1821 if (oldHeight != infoPtr->normalImageHeight &&
1822 !infoPtr->bHeightSet)
1824 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1825 bRecalcVisible = TRUE;
1828 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1829 infoPtr->normalImageWidth != infoPtr->uIndent)
1831 infoPtr->uIndent = infoPtr->normalImageWidth;
1832 bRecalcVisible = TRUE;
1835 if (bRecalcVisible)
1836 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1838 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1839 TREEVIEW_UpdateScrollBars(infoPtr);
1842 TREEVIEW_Invalidate(infoPtr, NULL);
1844 return (LRESULT)himlOld;
1847 static LRESULT
1848 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1850 INT prevHeight = infoPtr->uItemHeight;
1852 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1853 if (newHeight == -1)
1855 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1856 infoPtr->bHeightSet = FALSE;
1858 else
1860 if (newHeight == 0) newHeight = 1;
1861 infoPtr->uItemHeight = newHeight;
1862 infoPtr->bHeightSet = TRUE;
1865 /* Round down, unless we support odd ("non even") heights. */
1866 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1868 infoPtr->uItemHeight &= ~1;
1869 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1872 if (infoPtr->uItemHeight != prevHeight)
1874 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1875 TREEVIEW_UpdateScrollBars(infoPtr);
1876 TREEVIEW_Invalidate(infoPtr, NULL);
1879 return prevHeight;
1882 static LRESULT
1883 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1885 TRACE("\n");
1886 return infoPtr->uItemHeight;
1890 static LRESULT
1891 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1893 TRACE("%p\n", infoPtr->hFont);
1894 return (LRESULT)infoPtr->hFont;
1898 static INT CALLBACK
1899 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1901 (void)unused;
1903 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1905 return 1;
1908 static LRESULT
1909 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1911 UINT uHeight = infoPtr->uItemHeight;
1913 TRACE("%p %i\n", hFont, bRedraw);
1915 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1917 DeleteObject(infoPtr->hBoldFont);
1918 DeleteObject(infoPtr->hUnderlineFont);
1919 DeleteObject(infoPtr->hBoldUnderlineFont);
1920 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1921 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1922 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
1924 if (!infoPtr->bHeightSet)
1925 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1927 if (uHeight != infoPtr->uItemHeight)
1928 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1930 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1932 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1933 TREEVIEW_UpdateScrollBars(infoPtr);
1935 if (bRedraw)
1936 TREEVIEW_Invalidate(infoPtr, NULL);
1938 return 0;
1942 static LRESULT
1943 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1945 TRACE("\n");
1946 return (LRESULT)infoPtr->clrLine;
1949 static LRESULT
1950 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1952 COLORREF prevColor = infoPtr->clrLine;
1954 TRACE("\n");
1955 infoPtr->clrLine = color;
1956 return (LRESULT)prevColor;
1960 static LRESULT
1961 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1963 TRACE("\n");
1964 return (LRESULT)infoPtr->clrText;
1967 static LRESULT
1968 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1970 COLORREF prevColor = infoPtr->clrText;
1972 TRACE("\n");
1973 infoPtr->clrText = color;
1975 if (infoPtr->clrText != prevColor)
1976 TREEVIEW_Invalidate(infoPtr, NULL);
1978 return (LRESULT)prevColor;
1982 static LRESULT
1983 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1985 TRACE("\n");
1986 return (LRESULT)infoPtr->clrBk;
1989 static LRESULT
1990 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1992 COLORREF prevColor = infoPtr->clrBk;
1994 TRACE("\n");
1995 infoPtr->clrBk = newColor;
1997 if (newColor != prevColor)
1998 TREEVIEW_Invalidate(infoPtr, NULL);
2000 return (LRESULT)prevColor;
2004 static LRESULT
2005 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
2007 TRACE("\n");
2008 return (LRESULT)infoPtr->clrInsertMark;
2011 static LRESULT
2012 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
2014 COLORREF prevColor = infoPtr->clrInsertMark;
2016 TRACE("%x\n", color);
2017 infoPtr->clrInsertMark = color;
2019 return (LRESULT)prevColor;
2023 static LRESULT
2024 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2026 TRACE("%d %p\n", wParam, item);
2028 if (!TREEVIEW_ValidItem(infoPtr, item))
2029 return 0;
2031 infoPtr->insertBeforeorAfter = wParam;
2032 infoPtr->insertMarkItem = item;
2034 TREEVIEW_Invalidate(infoPtr, NULL);
2036 return 1;
2040 /************************************************************************
2041 * Some serious braindamage here. lParam is a pointer to both the
2042 * input HTREEITEM and the output RECT.
2044 static LRESULT
2045 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2047 TREEVIEW_ITEM *item;
2048 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2050 TRACE("\n");
2052 if (pItem == NULL)
2053 return FALSE;
2055 item = *pItem;
2056 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2057 return FALSE;
2060 * If wParam is TRUE return the text size otherwise return
2061 * the whole item size
2063 if (fTextRect)
2065 /* Windows does not send TVN_GETDISPINFO here. */
2067 lpRect->top = item->rect.top;
2068 lpRect->bottom = item->rect.bottom;
2070 lpRect->left = item->textOffset;
2071 if (!item->textWidth)
2072 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2074 lpRect->right = item->textOffset + item->textWidth + 4;
2076 else
2078 *lpRect = item->rect;
2081 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2083 return TRUE;
2086 static inline LRESULT
2087 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2089 /* Surprise! This does not take integral height into account. */
2090 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2091 return infoPtr->clientHeight / infoPtr->uItemHeight;
2095 static LRESULT
2096 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2098 TREEVIEW_ITEM *item = tvItem->hItem;
2100 if (!TREEVIEW_ValidItem(infoPtr, item))
2102 if (!item) return FALSE;
2104 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2105 infoPtr = item->infoPtr;
2106 if (!TREEVIEW_ValidItem(infoPtr, item)) return FALSE;
2109 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2111 if (tvItem->mask & TVIF_CHILDREN)
2113 if (item->cChildren==I_CHILDRENCALLBACK)
2114 FIXME("I_CHILDRENCALLBACK not supported\n");
2115 tvItem->cChildren = item->cChildren;
2118 if (tvItem->mask & TVIF_HANDLE)
2119 tvItem->hItem = item;
2121 if (tvItem->mask & TVIF_IMAGE)
2122 tvItem->iImage = item->iImage;
2124 if (tvItem->mask & TVIF_INTEGRAL)
2125 tvItem->iIntegral = item->iIntegral;
2127 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2128 tvItem->lParam = item->lParam;
2130 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2131 tvItem->iSelectedImage = item->iSelectedImage;
2133 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2134 tvItem->iExpandedImage = item->iExpandedImage;
2136 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2137 tvItem->state = item->state;
2139 if (tvItem->mask & TVIF_TEXT)
2141 if (item->pszText == NULL)
2143 if (tvItem->cchTextMax > 0)
2144 tvItem->pszText[0] = '\0';
2146 else if (isW)
2148 if (item->pszText == LPSTR_TEXTCALLBACKW)
2150 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2151 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2153 else
2155 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2158 else
2160 if (item->pszText == LPSTR_TEXTCALLBACKW)
2162 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2163 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2165 else
2167 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2168 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2173 if (tvItem->mask & TVIF_STATEEX)
2175 FIXME("Extended item state not supported, returning 0.\n");
2176 tvItem->uStateEx = 0;
2179 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2180 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2182 return TRUE;
2185 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2186 * which is wrong. */
2187 static LRESULT
2188 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2190 TREEVIEW_ITEM *item;
2191 TREEVIEW_ITEM originalItem;
2193 item = tvItem->hItem;
2195 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2196 tvItem->mask);
2198 if (!TREEVIEW_ValidItem(infoPtr, item))
2199 return FALSE;
2201 /* store the original item values */
2202 originalItem = *item;
2204 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2205 return FALSE;
2207 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2208 if ((tvItem->mask & TVIF_TEXT
2209 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2210 && ISVISIBLE(item))
2212 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2213 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2216 if (tvItem->mask != 0 && ISVISIBLE(item))
2218 /* The refresh updates everything, but we can't wait until then. */
2219 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2221 /* if any of the item's values changed and it's not a callback, redraw the item */
2222 if (item_changed(&originalItem, item, tvItem))
2224 if (tvItem->mask & TVIF_INTEGRAL)
2226 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2227 TREEVIEW_UpdateScrollBars(infoPtr);
2229 TREEVIEW_Invalidate(infoPtr, NULL);
2231 else
2233 TREEVIEW_UpdateScrollBars(infoPtr);
2234 TREEVIEW_Invalidate(infoPtr, item);
2239 return TRUE;
2242 static LRESULT
2243 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2245 TRACE("\n");
2247 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2248 return 0;
2250 return (item->state & mask);
2253 static LRESULT
2254 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2256 TREEVIEW_ITEM *retval;
2258 retval = 0;
2260 /* handle all the global data here */
2261 switch (which)
2263 case TVGN_CHILD: /* Special case: child of 0 is root */
2264 if (item)
2265 break;
2266 /* fall through */
2267 case TVGN_ROOT:
2268 retval = infoPtr->root->firstChild;
2269 break;
2271 case TVGN_CARET:
2272 retval = infoPtr->selectedItem;
2273 break;
2275 case TVGN_FIRSTVISIBLE:
2276 retval = infoPtr->firstVisible;
2277 break;
2279 case TVGN_DROPHILITE:
2280 retval = infoPtr->dropItem;
2281 break;
2283 case TVGN_LASTVISIBLE:
2284 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2285 break;
2288 if (retval)
2290 TRACE("flags:%x, returns %p\n", which, retval);
2291 return (LRESULT)retval;
2294 if (item == TVI_ROOT) item = infoPtr->root;
2296 if (!TREEVIEW_ValidItem(infoPtr, item))
2297 return FALSE;
2299 switch (which)
2301 case TVGN_NEXT:
2302 retval = item->nextSibling;
2303 break;
2304 case TVGN_PREVIOUS:
2305 retval = item->prevSibling;
2306 break;
2307 case TVGN_PARENT:
2308 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2309 break;
2310 case TVGN_CHILD:
2311 retval = item->firstChild;
2312 break;
2313 case TVGN_NEXTVISIBLE:
2314 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2315 break;
2316 case TVGN_PREVIOUSVISIBLE:
2317 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2318 break;
2319 default:
2320 TRACE("Unknown msg %x,item %p\n", which, item);
2321 break;
2324 TRACE("flags:%x, item %p;returns %p\n", which, item, retval);
2325 return (LRESULT)retval;
2329 static LRESULT
2330 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2332 TRACE(" %d\n", infoPtr->uNumItems);
2333 return (LRESULT)infoPtr->uNumItems;
2336 static VOID
2337 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2339 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2341 static const unsigned int state_table[] = { 0, 2, 1 };
2343 unsigned int state;
2345 state = STATEIMAGEINDEX(item->state);
2346 TRACE("state:%x\n", state);
2347 item->state &= ~TVIS_STATEIMAGEMASK;
2349 if (state < 3)
2350 state = state_table[state];
2352 item->state |= INDEXTOSTATEIMAGEMASK(state);
2354 TRACE("state:%x\n", state);
2355 TREEVIEW_Invalidate(infoPtr, item);
2360 /* Painting *************************************************************/
2362 /* Draw the lines and expand button for an item. Also draws one section
2363 * of the line from item's parent to item's parent's next sibling. */
2364 static void
2365 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2367 LONG centerx, centery;
2368 BOOL lar = ((infoPtr->dwStyle
2369 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2370 > TVS_LINESATROOT);
2371 HBRUSH hbr, hbrOld;
2372 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2374 if (!lar && item->iLevel == 0)
2375 return;
2377 hbr = CreateSolidBrush(clrBk);
2378 hbrOld = SelectObject(hdc, hbr);
2380 centerx = (item->linesOffset + item->stateOffset) / 2;
2381 centery = (item->rect.top + item->rect.bottom) / 2;
2383 if (infoPtr->dwStyle & TVS_HASLINES)
2385 HPEN hOldPen, hNewPen;
2386 HTREEITEM parent;
2387 LOGBRUSH lb;
2389 /* Get a dotted grey pen */
2390 lb.lbStyle = BS_SOLID;
2391 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2392 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2393 hOldPen = SelectObject(hdc, hNewPen);
2395 /* Make sure the center is on a dot (using +2 instead
2396 * of +1 gives us pixel-by-pixel compat with native) */
2397 centery = (centery + 2) & ~1;
2399 MoveToEx(hdc, item->stateOffset, centery, NULL);
2400 LineTo(hdc, centerx - 1, centery);
2402 if (item->prevSibling || item->parent != infoPtr->root)
2404 MoveToEx(hdc, centerx, item->rect.top, NULL);
2405 LineTo(hdc, centerx, centery);
2408 if (item->nextSibling)
2410 MoveToEx(hdc, centerx, centery, NULL);
2411 LineTo(hdc, centerx, item->rect.bottom + 1);
2414 /* Draw the line from our parent to its next sibling. */
2415 parent = item->parent;
2416 while (parent != infoPtr->root)
2418 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2420 if (parent->nextSibling
2421 /* skip top-levels unless TVS_LINESATROOT */
2422 && parent->stateOffset > parent->linesOffset)
2424 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2425 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2428 parent = parent->parent;
2431 SelectObject(hdc, hOldPen);
2432 DeleteObject(hNewPen);
2436 * Display the (+/-) signs
2439 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2441 if (item->cChildren)
2443 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2444 if (theme)
2446 RECT glyphRect = item->rect;
2447 glyphRect.left = item->linesOffset;
2448 glyphRect.right = item->stateOffset;
2449 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2450 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2451 &glyphRect, NULL);
2453 else
2455 LONG height = item->rect.bottom - item->rect.top;
2456 LONG width = item->stateOffset - item->linesOffset;
2457 LONG rectsize = min(height, width) / 4;
2458 /* plussize = ceil(rectsize * 3/4) */
2459 LONG plussize = (rectsize + 1) * 3 / 4;
2461 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2462 HPEN old_pen = SelectObject(hdc, new_pen);
2464 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2465 centerx + rectsize + 2, centery + rectsize + 2);
2467 SelectObject(hdc, old_pen);
2468 DeleteObject(new_pen);
2470 /* draw +/- signs with current text color */
2471 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2472 old_pen = SelectObject(hdc, new_pen);
2474 if (height < 18 || width < 18)
2476 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2477 LineTo(hdc, centerx + plussize, centery);
2479 if (!(item->state & TVIS_EXPANDED) ||
2480 (item->state & TVIS_EXPANDPARTIAL))
2482 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2483 LineTo(hdc, centerx, centery + plussize);
2486 else
2488 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2489 centerx + plussize, centery + 2);
2491 if (!(item->state & TVIS_EXPANDED) ||
2492 (item->state & TVIS_EXPANDPARTIAL))
2494 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2495 centerx + 2, centery + plussize);
2496 SetPixel(hdc, centerx - 1, centery, clrBk);
2497 SetPixel(hdc, centerx + 1, centery, clrBk);
2501 SelectObject(hdc, old_pen);
2502 DeleteObject(new_pen);
2506 SelectObject(hdc, hbrOld);
2507 DeleteObject(hbr);
2510 static void
2511 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2513 INT cditem;
2514 HFONT hOldFont;
2515 COLORREF oldTextColor, oldTextBkColor;
2516 int centery;
2517 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2518 NMTVCUSTOMDRAW nmcdhdr;
2520 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2522 /* - If item is drop target or it is selected and window is in focus -
2523 * use blue background (COLOR_HIGHLIGHT).
2524 * - If item is selected, window is not in focus, but it has style
2525 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2526 * - Otherwise - use background color
2528 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2529 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem || item == infoPtr->focusedItem) &&
2530 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2532 if ((item->state & TVIS_DROPHILITED) || inFocus)
2534 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2535 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2537 else
2539 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2540 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2543 else
2545 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2546 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2547 nmcdhdr.clrText = comctl32_color.clrHighlight;
2548 else
2549 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2552 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2554 /* The custom draw handler can query the text rectangle,
2555 * so get ready. */
2556 /* should already be known, set to 0 when changed */
2557 if (!item->textWidth)
2558 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2560 cditem = 0;
2562 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2564 cditem = TREEVIEW_SendCustomDrawItemNotify
2565 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2566 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2568 if (cditem & CDRF_SKIPDEFAULT)
2570 SelectObject(hdc, hOldFont);
2571 return;
2575 if (cditem & CDRF_NEWFONT)
2576 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2578 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2580 /* Set colors. Custom draw handler can change these so we do this after it. */
2581 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2582 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2584 centery = (item->rect.top + item->rect.bottom) / 2;
2587 * Display the images associated with this item
2590 INT imageIndex;
2592 /* State images are displayed to the left of the Normal image
2593 * image number is in state; zero should be `display no image'.
2595 imageIndex = STATEIMAGEINDEX(item->state);
2597 if (infoPtr->himlState && imageIndex)
2599 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2600 item->stateOffset,
2601 centery - infoPtr->stateImageHeight / 2,
2602 ILD_NORMAL);
2605 /* Now, draw the normal image; can be either selected,
2606 * non-selected or expanded image.
2609 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2611 /* The item is currently selected */
2612 imageIndex = item->iSelectedImage;
2614 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2616 /* The item is currently not selected but expanded */
2617 imageIndex = item->iExpandedImage;
2619 else
2621 /* The item is not selected and not expanded */
2622 imageIndex = item->iImage;
2625 if (infoPtr->himlNormal)
2627 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2629 style |= item->state & TVIS_OVERLAYMASK;
2631 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2632 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2633 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2634 style);
2640 * Display the text associated with this item
2643 /* Don't paint item's text if it's being edited */
2644 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2646 if (item->pszText)
2648 RECT rcText;
2649 UINT align;
2650 SIZE sz;
2652 rcText.top = item->rect.top;
2653 rcText.bottom = item->rect.bottom;
2654 rcText.left = item->textOffset;
2655 rcText.right = rcText.left + item->textWidth + 4;
2657 TRACE("drawing text %s at (%s)\n",
2658 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2660 /* Draw it */
2661 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2663 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2664 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2665 ETO_CLIPPED | ETO_OPAQUE,
2666 &rcText,
2667 item->pszText,
2668 lstrlenW(item->pszText),
2669 NULL);
2670 SetTextAlign(hdc, align);
2672 /* Draw focus box around the selected item */
2673 if ((item == infoPtr->selectedItem) && inFocus)
2675 DrawFocusRect(hdc,&rcText);
2680 /* Draw insertion mark if necessary */
2682 if (infoPtr->insertMarkItem)
2683 TRACE("item:%d,mark:%p\n",
2684 TREEVIEW_GetItemIndex(infoPtr, item),
2685 infoPtr->insertMarkItem);
2687 if (item == infoPtr->insertMarkItem)
2689 HPEN hNewPen, hOldPen;
2690 int offset;
2691 int left, right;
2693 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2694 hOldPen = SelectObject(hdc, hNewPen);
2696 if (infoPtr->insertBeforeorAfter)
2697 offset = item->rect.bottom - 1;
2698 else
2699 offset = item->rect.top + 1;
2701 left = item->textOffset - 2;
2702 right = item->textOffset + item->textWidth + 2;
2704 MoveToEx(hdc, left, offset - 3, NULL);
2705 LineTo(hdc, left, offset + 4);
2707 MoveToEx(hdc, left, offset, NULL);
2708 LineTo(hdc, right + 1, offset);
2710 MoveToEx(hdc, right, offset + 3, NULL);
2711 LineTo(hdc, right, offset - 4);
2713 SelectObject(hdc, hOldPen);
2714 DeleteObject(hNewPen);
2717 if (cditem & CDRF_NOTIFYPOSTPAINT)
2719 cditem = TREEVIEW_SendCustomDrawItemNotify
2720 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2721 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2724 /* Restore the hdc state */
2725 SetTextColor(hdc, oldTextColor);
2726 SetBkColor(hdc, oldTextBkColor);
2727 SelectObject(hdc, hOldFont);
2730 /* Computes treeHeight and treeWidth and updates the scroll bars.
2732 static void
2733 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2735 TREEVIEW_ITEM *item;
2736 HWND hwnd = infoPtr->hwnd;
2737 BOOL vert = FALSE;
2738 BOOL horz = FALSE;
2739 SCROLLINFO si;
2740 LONG scrollX = infoPtr->scrollX;
2742 infoPtr->treeWidth = 0;
2743 infoPtr->treeHeight = 0;
2745 /* We iterate through all visible items in order to get the tree height
2746 * and width */
2747 item = infoPtr->root->firstChild;
2749 while (item != NULL)
2751 if (ISVISIBLE(item))
2753 /* actually we draw text at textOffset + 2 */
2754 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2755 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2757 /* This is scroll-adjusted, but we fix this below. */
2758 infoPtr->treeHeight = item->rect.bottom;
2761 item = TREEVIEW_GetNextListItem(infoPtr, item);
2764 /* Fix the scroll adjusted treeHeight and treeWidth. */
2765 if (infoPtr->root->firstChild)
2766 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2768 infoPtr->treeWidth += infoPtr->scrollX;
2770 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2772 /* Adding one scroll bar may take up enough space that it forces us
2773 * to add the other as well. */
2774 if (infoPtr->treeHeight > infoPtr->clientHeight)
2776 vert = TRUE;
2778 if (infoPtr->treeWidth
2779 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2780 horz = TRUE;
2782 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2783 horz = TRUE;
2785 if (!vert && horz && infoPtr->treeHeight
2786 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2787 vert = TRUE;
2789 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2791 si.cbSize = sizeof(SCROLLINFO);
2792 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2793 si.nMin = 0;
2795 if (vert)
2797 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2798 if ( si.nPage && NULL != infoPtr->firstVisible)
2800 si.nPos = infoPtr->firstVisible->visibleOrder;
2801 si.nMax = infoPtr->maxVisibleOrder - 1;
2803 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2805 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2806 ShowScrollBar(hwnd, SB_VERT, TRUE);
2807 infoPtr->uInternalStatus |= TV_VSCROLL;
2809 else
2811 if (infoPtr->uInternalStatus & TV_VSCROLL)
2812 ShowScrollBar(hwnd, SB_VERT, FALSE);
2813 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2816 else
2818 if (infoPtr->uInternalStatus & TV_VSCROLL)
2819 ShowScrollBar(hwnd, SB_VERT, FALSE);
2820 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2823 if (horz)
2825 si.nPage = infoPtr->clientWidth;
2826 si.nPos = infoPtr->scrollX;
2827 si.nMax = infoPtr->treeWidth - 1;
2829 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2831 si.nPos = si.nMax - max( si.nPage-1, 0 );
2832 scrollX = si.nPos;
2835 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2836 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2837 infoPtr->uInternalStatus |= TV_HSCROLL;
2839 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2840 TREEVIEW_HScroll(infoPtr,
2841 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2843 else
2845 if (infoPtr->uInternalStatus & TV_HSCROLL)
2846 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2847 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2849 scrollX = 0;
2850 if (infoPtr->scrollX != 0)
2852 TREEVIEW_HScroll(infoPtr,
2853 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2857 if (!horz)
2858 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2861 static void
2862 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2864 HBRUSH hBrush;
2865 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2867 hBrush = CreateSolidBrush(clrBk);
2868 FillRect(hdc, rc, hBrush);
2869 DeleteObject(hBrush);
2872 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2873 static LRESULT
2874 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2876 RECT rect;
2878 TRACE("%p\n", infoPtr);
2880 GetClientRect(infoPtr->hwnd, &rect);
2881 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2883 return 1;
2886 static void
2887 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2889 HWND hwnd = infoPtr->hwnd;
2890 RECT rect = *rc;
2891 TREEVIEW_ITEM *item;
2893 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2895 TRACE("empty window\n");
2896 return;
2899 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2900 hdc, rect);
2902 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2904 ReleaseDC(hwnd, hdc);
2905 return;
2908 for (item = infoPtr->root->firstChild;
2909 item != NULL;
2910 item = TREEVIEW_GetNextListItem(infoPtr, item))
2912 if (ISVISIBLE(item))
2914 /* Avoid unneeded calculations */
2915 if (item->rect.top > rect.bottom)
2916 break;
2917 if (item->rect.bottom < rect.top)
2918 continue;
2920 TREEVIEW_DrawItem(infoPtr, hdc, item);
2924 TREEVIEW_UpdateScrollBars(infoPtr);
2926 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2927 infoPtr->cdmode =
2928 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2931 static inline void
2932 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2934 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2937 static void
2938 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2940 if (item)
2941 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2942 else
2943 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2946 static void
2947 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
2949 RECT rc;
2950 HBITMAP hbm, hbmOld;
2951 HDC hdc, hdcScreen;
2952 int nIndex;
2954 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2956 hdcScreen = GetDC(0);
2958 hdc = CreateCompatibleDC(hdcScreen);
2959 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2960 hbmOld = SelectObject(hdc, hbm);
2962 SetRect(&rc, 0, 0, 48, 16);
2963 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2965 SetRect(&rc, 18, 2, 30, 14);
2966 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2967 DFCS_BUTTONCHECK|DFCS_FLAT);
2969 SetRect(&rc, 34, 2, 46, 14);
2970 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2971 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
2973 SelectObject(hdc, hbmOld);
2974 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
2975 comctl32_color.clrWindow);
2976 TRACE("checkbox index %d\n", nIndex);
2978 DeleteObject(hbm);
2979 DeleteDC(hdc);
2980 ReleaseDC(0, hdcScreen);
2982 infoPtr->stateImageWidth = 16;
2983 infoPtr->stateImageHeight = 16;
2986 static void
2987 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2989 TREEVIEW_ITEM *child = item->firstChild;
2991 item->state &= ~TVIS_STATEIMAGEMASK;
2992 item->state |= INDEXTOSTATEIMAGEMASK(1);
2994 while (child)
2996 TREEVIEW_ITEM *next = child->nextSibling;
2997 TREEVIEW_ResetImageStateIndex(infoPtr, child);
2998 child = next;
3002 static LRESULT
3003 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
3005 HDC hdc;
3006 PAINTSTRUCT ps;
3007 RECT rc;
3009 TRACE("(%p %p)\n", infoPtr, hdc_ref);
3011 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
3013 TREEVIEW_InitCheckboxes(infoPtr);
3014 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
3016 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
3017 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
3018 TREEVIEW_UpdateScrollBars(infoPtr);
3019 TREEVIEW_Invalidate(infoPtr, NULL);
3022 if (hdc_ref)
3024 hdc = hdc_ref;
3025 GetClientRect(infoPtr->hwnd, &rc);
3026 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3028 else
3030 hdc = BeginPaint(infoPtr->hwnd, &ps);
3031 rc = ps.rcPaint;
3032 if(ps.fErase)
3033 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3036 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3037 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3039 if (!hdc_ref)
3040 EndPaint(infoPtr->hwnd, &ps);
3042 return 0;
3045 static LRESULT
3046 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
3048 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3050 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3051 return 0;
3053 if (options & PRF_ERASEBKGND)
3054 TREEVIEW_EraseBackground(infoPtr, hdc);
3056 if (options & PRF_CLIENT)
3058 RECT rc;
3059 GetClientRect(infoPtr->hwnd, &rc);
3060 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3063 return 0;
3066 /* Sorting **************************************************************/
3068 /***************************************************************************
3069 * Forward the DPA local callback to the treeview owner callback
3071 static INT WINAPI
3072 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3073 const TVSORTCB *pCallBackSort)
3075 /* Forward the call to the client-defined callback */
3076 return pCallBackSort->lpfnCompare(first->lParam,
3077 second->lParam,
3078 pCallBackSort->lParam);
3081 /***************************************************************************
3082 * Treeview native sort routine: sort on item text.
3084 static INT WINAPI
3085 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3086 const TREEVIEW_INFO *infoPtr)
3088 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3089 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3091 if(first->pszText && second->pszText)
3092 return lstrcmpiW(first->pszText, second->pszText);
3093 else if(first->pszText)
3094 return -1;
3095 else if(second->pszText)
3096 return 1;
3097 else
3098 return 0;
3101 /* Returns the number of physical children belonging to item. */
3102 static INT
3103 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3105 INT cChildren = 0;
3106 HTREEITEM hti;
3108 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3109 cChildren++;
3111 return cChildren;
3114 /* Returns a DPA containing a pointer to each physical child of item in
3115 * sibling order. If item has no children, an empty DPA is returned. */
3116 static HDPA
3117 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3119 HTREEITEM child;
3121 HDPA list = DPA_Create(8);
3122 if (list == 0) return NULL;
3124 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3126 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3128 DPA_Destroy(list);
3129 return NULL;
3133 return list;
3136 /***************************************************************************
3137 * Setup the treeview structure with regards of the sort method
3138 * and sort the children of the TV item specified in lParam
3139 * fRecurse: currently unused. Should be zero.
3140 * parent: if pSort!=NULL, should equal pSort->hParent.
3141 * otherwise, item which child items are to be sorted.
3142 * pSort: sort method info. if NULL, sort on item text.
3143 * if non-NULL, sort on item's lParam content, and let the
3144 * application decide what that means. See also TVM_SORTCHILDRENCB.
3147 static LRESULT
3148 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3149 LPTVSORTCB pSort)
3151 INT cChildren;
3152 PFNDPACOMPARE pfnCompare;
3153 LPARAM lpCompare;
3155 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3156 if (parent == TVI_ROOT || parent == NULL)
3157 parent = infoPtr->root;
3159 /* Check for a valid handle to the parent item */
3160 if (!TREEVIEW_ValidItem(infoPtr, parent))
3162 ERR("invalid item hParent=%p\n", parent);
3163 return FALSE;
3166 if (pSort)
3168 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3169 lpCompare = (LPARAM)pSort;
3171 else
3173 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3174 lpCompare = (LPARAM)infoPtr;
3177 cChildren = TREEVIEW_CountChildren(parent);
3179 /* Make sure there is something to sort */
3180 if (cChildren > 1)
3182 /* TREEVIEW_ITEM rechaining */
3183 INT count = 0;
3184 HTREEITEM item = 0;
3185 HTREEITEM nextItem = 0;
3186 HTREEITEM prevItem = 0;
3188 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3190 if (sortList == NULL)
3191 return FALSE;
3193 /* let DPA sort the list */
3194 DPA_Sort(sortList, pfnCompare, lpCompare);
3196 /* The order of DPA entries has been changed, so fixup the
3197 * nextSibling and prevSibling pointers. */
3199 item = DPA_GetPtr(sortList, count++);
3200 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3202 /* link the two current item together */
3203 item->nextSibling = nextItem;
3204 nextItem->prevSibling = item;
3206 if (prevItem == NULL)
3208 /* this is the first item, update the parent */
3209 parent->firstChild = item;
3210 item->prevSibling = NULL;
3212 else
3214 /* fix the back chaining */
3215 item->prevSibling = prevItem;
3218 /* get ready for the next one */
3219 prevItem = item;
3220 item = nextItem;
3223 /* the last item is pointed to by item and never has a sibling */
3224 item->nextSibling = NULL;
3225 parent->lastChild = item;
3227 DPA_Destroy(sortList);
3229 TREEVIEW_VerifyTree(infoPtr);
3231 if (parent->state & TVIS_EXPANDED)
3233 int visOrder = infoPtr->firstVisible->visibleOrder;
3235 if (parent == infoPtr->root)
3236 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3237 else
3238 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3240 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3242 TREEVIEW_ITEM *item;
3244 for (item = infoPtr->root->firstChild; item != NULL;
3245 item = TREEVIEW_GetNextListItem(infoPtr, item))
3247 if (item->visibleOrder == visOrder)
3248 break;
3251 if (!item) item = parent->firstChild;
3252 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3255 TREEVIEW_Invalidate(infoPtr, NULL);
3258 return TRUE;
3260 return FALSE;
3264 /***************************************************************************
3265 * Setup the treeview structure with regards of the sort method
3266 * and sort the children of the TV item specified in lParam
3268 static LRESULT
3269 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3271 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3275 /***************************************************************************
3276 * Sort the children of the TV item specified in lParam.
3278 static LRESULT
3279 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3281 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3285 /* Expansion/Collapse ***************************************************/
3287 static BOOL
3288 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3289 UINT action)
3291 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3292 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3293 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3294 0, item);
3297 static VOID
3298 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3299 UINT action)
3301 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3302 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3303 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3304 0, item);
3308 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3309 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3310 static BOOL
3311 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3312 BOOL bRemoveChildren, BOOL bUser)
3314 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3315 BOOL bSetSelection, bSetFirstVisible;
3316 RECT scrollRect;
3317 LONG scrollDist = 0;
3318 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3319 BOOL wasExpanded;
3321 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3323 if (!TREEVIEW_HasChildren(infoPtr, item))
3324 return FALSE;
3326 if (bUser)
3327 TREEVIEW_SendExpanding(infoPtr, item, action);
3329 if (item->firstChild == NULL)
3330 return FALSE;
3332 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3333 item->state &= ~TVIS_EXPANDED;
3335 if (wasExpanded && bUser)
3336 TREEVIEW_SendExpanded(infoPtr, item, action);
3338 bSetSelection = (infoPtr->selectedItem != NULL
3339 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3341 bSetFirstVisible = (infoPtr->firstVisible != NULL
3342 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3344 tmpItem = item;
3345 while (tmpItem)
3347 if (tmpItem->nextSibling)
3349 nextItem = tmpItem->nextSibling;
3350 break;
3352 tmpItem = tmpItem->parent;
3355 if (nextItem)
3356 scrollDist = nextItem->rect.top;
3358 if (bRemoveChildren)
3360 INT old_cChildren = item->cChildren;
3361 TRACE("TVE_COLLAPSERESET\n");
3362 item->state &= ~TVIS_EXPANDEDONCE;
3363 TREEVIEW_RemoveAllChildren(infoPtr, item);
3364 item->cChildren = old_cChildren;
3366 if (!wasExpanded)
3367 return FALSE;
3369 if (item->firstChild)
3371 TREEVIEW_ITEM *i, *sibling;
3373 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3375 for (i = item->firstChild; i != sibling;
3376 i = TREEVIEW_GetNextListItem(infoPtr, i))
3378 i->visibleOrder = -1;
3382 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3384 if (nextItem)
3385 scrollDist = -(scrollDist - nextItem->rect.top);
3387 if (bSetSelection)
3389 /* Don't call DoSelectItem, it sends notifications. */
3390 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3391 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3392 item->state |= TVIS_SELECTED;
3393 infoPtr->selectedItem = item;
3396 TREEVIEW_UpdateScrollBars(infoPtr);
3398 scrollRect.left = 0;
3399 scrollRect.right = infoPtr->clientWidth;
3400 scrollRect.bottom = infoPtr->clientHeight;
3402 if (nextItem)
3404 scrollRect.top = nextItem->rect.top;
3406 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3407 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3408 TREEVIEW_Invalidate(infoPtr, item);
3409 } else {
3410 scrollRect.top = item->rect.top;
3411 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3414 TREEVIEW_SetFirstVisible(infoPtr,
3415 bSetFirstVisible ? item : infoPtr->firstVisible,
3416 TRUE);
3418 return wasExpanded;
3421 static BOOL
3422 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3423 BOOL partial, BOOL user)
3425 LONG scrollDist;
3426 LONG orgNextTop = 0;
3427 RECT scrollRect;
3428 TREEVIEW_ITEM *nextItem, *tmpItem;
3429 BOOL sendsNotifications;
3431 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr, item, partial, user);
3433 if (!TREEVIEW_HasChildren(infoPtr, item))
3434 return FALSE;
3436 tmpItem = item; nextItem = NULL;
3437 while (tmpItem)
3439 if (tmpItem->nextSibling)
3441 nextItem = tmpItem->nextSibling;
3442 break;
3444 tmpItem = tmpItem->parent;
3447 if (nextItem)
3448 orgNextTop = nextItem->rect.top;
3450 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3452 sendsNotifications = user || ((item->cChildren != 0) &&
3453 !(item->state & TVIS_EXPANDEDONCE));
3454 if (sendsNotifications)
3456 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3458 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3459 return FALSE;
3462 if (!item->firstChild)
3463 return FALSE;
3465 item->state |= TVIS_EXPANDED;
3467 if (partial)
3468 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3470 if (ISVISIBLE(item))
3472 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3473 TREEVIEW_UpdateSubTree(infoPtr, item);
3474 TREEVIEW_UpdateScrollBars(infoPtr);
3476 scrollRect.left = 0;
3477 scrollRect.bottom = infoPtr->treeHeight;
3478 scrollRect.right = infoPtr->clientWidth;
3479 if (nextItem)
3481 scrollDist = nextItem->rect.top - orgNextTop;
3482 scrollRect.top = orgNextTop;
3484 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3485 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3486 TREEVIEW_Invalidate (infoPtr, item);
3487 } else {
3488 scrollRect.top = item->rect.top;
3489 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3492 /* Scroll up so that as many children as possible are visible.
3493 * This fails when expanding causes an HScroll bar to appear, but we
3494 * don't know that yet, so the last item is obscured. */
3495 if (item->firstChild != NULL)
3497 int nChildren = item->lastChild->visibleOrder
3498 - item->firstChild->visibleOrder + 1;
3500 int visible_pos = item->visibleOrder
3501 - infoPtr->firstVisible->visibleOrder;
3503 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3505 if (visible_pos > 0 && nChildren > rows_below)
3507 int scroll = nChildren - rows_below;
3509 if (scroll > visible_pos)
3510 scroll = visible_pos;
3512 if (scroll > 0)
3514 TREEVIEW_ITEM *newFirstVisible
3515 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3516 scroll);
3519 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3525 if (sendsNotifications) {
3526 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3527 item->state |= TVIS_EXPANDEDONCE;
3530 return TRUE;
3533 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3534 to mouse messages and TVM_SELECTITEM.
3536 selection - previously selected item, used to collapse a part of a tree
3537 item - new selected item
3539 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3540 HTREEITEM selection, HTREEITEM item)
3542 TREEVIEW_ITEM *SelItem;
3544 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3546 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3549 * Close the previous selection all the way to the root
3550 * as long as the new selection is not a child
3552 if(selection && (selection != item))
3554 BOOL closeit = TRUE;
3555 SelItem = item;
3557 /* determine if the hitItem is a child of the currently selected item */
3558 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3559 (SelItem->parent != infoPtr->root))
3561 closeit = (SelItem != selection);
3562 SelItem = SelItem->parent;
3565 if(closeit)
3567 if(TREEVIEW_ValidItem(infoPtr, selection))
3568 SelItem = selection;
3570 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3571 SelItem->parent != infoPtr->root)
3573 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3574 SelItem = SelItem->parent;
3580 * Expand the current item
3582 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3585 static BOOL
3586 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3588 TRACE("item=%p, user=%d\n", item, user);
3590 if (item->state & TVIS_EXPANDED)
3591 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3592 else
3593 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3596 static VOID
3597 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3599 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3601 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3603 if (TREEVIEW_HasChildren(infoPtr, item))
3604 TREEVIEW_ExpandAll(infoPtr, item);
3608 /* Note:If the specified item is the child of a collapsed parent item,
3609 the parent's list of child items is (recursively) expanded to reveal the
3610 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3611 know if it also applies here.
3614 static LRESULT
3615 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3617 if (!TREEVIEW_ValidItem(infoPtr, item))
3618 return 0;
3620 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3621 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3622 flag, item->state);
3624 switch (flag & TVE_TOGGLE)
3626 case TVE_COLLAPSE:
3627 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3628 FALSE);
3630 case TVE_EXPAND:
3631 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3632 FALSE);
3634 case TVE_TOGGLE:
3635 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3637 default:
3638 return 0;
3642 /* Hit-Testing **********************************************************/
3644 static TREEVIEW_ITEM *
3645 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3647 TREEVIEW_ITEM *item;
3648 LONG row;
3650 if (!infoPtr->firstVisible)
3651 return NULL;
3653 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3655 for (item = infoPtr->firstVisible; item != NULL;
3656 item = TREEVIEW_GetNextListItem(infoPtr, item))
3658 if (row >= item->visibleOrder
3659 && row < item->visibleOrder + item->iIntegral)
3660 break;
3663 return item;
3666 static LRESULT
3667 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3669 TREEVIEW_ITEM *item;
3670 RECT rect;
3671 UINT status;
3672 LONG x, y;
3674 lpht->hItem = 0;
3675 GetClientRect(infoPtr->hwnd, &rect);
3676 status = 0;
3677 x = lpht->pt.x;
3678 y = lpht->pt.y;
3680 if (x < rect.left)
3682 status |= TVHT_TOLEFT;
3684 else if (x > rect.right)
3686 status |= TVHT_TORIGHT;
3689 if (y < rect.top)
3691 status |= TVHT_ABOVE;
3693 else if (y > rect.bottom)
3695 status |= TVHT_BELOW;
3698 if (status)
3700 lpht->flags = status;
3701 return 0;
3704 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3705 if (!item)
3707 lpht->flags = TVHT_NOWHERE;
3708 return 0;
3711 if (x >= item->textOffset + item->textWidth)
3713 lpht->flags = TVHT_ONITEMRIGHT;
3715 else if (x >= item->textOffset)
3717 lpht->flags = TVHT_ONITEMLABEL;
3719 else if (x >= item->imageOffset)
3721 lpht->flags = TVHT_ONITEMICON;
3723 else if (x >= item->stateOffset)
3725 lpht->flags = TVHT_ONITEMSTATEICON;
3727 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3729 lpht->flags = TVHT_ONITEMBUTTON;
3731 else
3733 lpht->flags = TVHT_ONITEMINDENT;
3736 lpht->hItem = item;
3737 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3739 return (LRESULT)item;
3742 /* Item Label Editing ***************************************************/
3744 static LRESULT
3745 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3747 return (LRESULT)infoPtr->hwndEdit;
3750 static LRESULT CALLBACK
3751 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3753 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3754 BOOL bCancel = FALSE;
3755 LRESULT rc;
3757 switch (uMsg)
3759 case WM_PAINT:
3760 TRACE("WM_PAINT start\n");
3761 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3762 lParam);
3763 TRACE("WM_PAINT done\n");
3764 return rc;
3766 case WM_KILLFOCUS:
3767 if (infoPtr->bIgnoreEditKillFocus)
3768 return TRUE;
3769 break;
3771 case WM_DESTROY:
3773 WNDPROC editProc = infoPtr->wpEditOrig;
3774 infoPtr->wpEditOrig = 0;
3775 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3776 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3779 case WM_GETDLGCODE:
3780 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3782 case WM_KEYDOWN:
3783 if (wParam == VK_ESCAPE)
3785 bCancel = TRUE;
3786 break;
3788 else if (wParam == VK_RETURN)
3790 break;
3793 /* fall through */
3794 default:
3795 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3798 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3799 /* eg. Using a messagebox */
3801 infoPtr->bIgnoreEditKillFocus = TRUE;
3802 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3803 infoPtr->bIgnoreEditKillFocus = FALSE;
3805 return 0;
3809 /* should handle edit control messages here */
3811 static LRESULT
3812 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3814 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3816 switch (HIWORD(wParam))
3818 case EN_UPDATE:
3821 * Adjust the edit window size
3823 WCHAR buffer[1024];
3824 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3825 HDC hdc = GetDC(infoPtr->hwndEdit);
3826 SIZE sz;
3827 HFONT hFont, hOldFont = 0;
3829 TRACE("edit=%p\n", infoPtr->hwndEdit);
3831 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3833 infoPtr->bLabelChanged = TRUE;
3835 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3837 /* Select font to get the right dimension of the string */
3838 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3840 if (hFont != 0)
3842 hOldFont = SelectObject(hdc, hFont);
3845 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3847 TEXTMETRICW textMetric;
3849 /* Add Extra spacing for the next character */
3850 GetTextMetricsW(hdc, &textMetric);
3851 sz.cx += (textMetric.tmMaxCharWidth * 2);
3853 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3854 sz.cx = min(sz.cx,
3855 infoPtr->clientWidth - editItem->textOffset + 2);
3857 SetWindowPos(infoPtr->hwndEdit,
3858 HWND_TOP,
3861 sz.cx,
3862 editItem->rect.bottom - editItem->rect.top + 3,
3863 SWP_NOMOVE | SWP_DRAWFRAME);
3866 if (hFont != 0)
3868 SelectObject(hdc, hOldFont);
3871 ReleaseDC(infoPtr->hwnd, hdc);
3872 break;
3874 case EN_KILLFOCUS:
3875 /* apparently we should respect passed handle value */
3876 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3878 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3879 break;
3881 default:
3882 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3885 return 0;
3888 static HWND
3889 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3891 HWND hwnd = infoPtr->hwnd;
3892 HWND hwndEdit;
3893 SIZE sz;
3894 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3895 HDC hdc;
3896 HFONT hOldFont=0;
3897 TEXTMETRICW textMetric;
3899 TRACE("%p %p\n", hwnd, hItem);
3900 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3901 return NULL;
3903 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3904 return NULL;
3906 if (infoPtr->hwndEdit)
3907 return infoPtr->hwndEdit;
3909 infoPtr->bLabelChanged = FALSE;
3911 /* make edit item visible */
3912 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3914 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3916 hdc = GetDC(hwnd);
3917 /* Select the font to get appropriate metric dimensions */
3918 if (infoPtr->hFont != 0)
3920 hOldFont = SelectObject(hdc, infoPtr->hFont);
3923 /* Get string length in pixels */
3924 if (hItem->pszText)
3925 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3926 &sz);
3927 else
3928 GetTextExtentPoint32A(hdc, "", 0, &sz);
3930 /* Add Extra spacing for the next character */
3931 GetTextMetricsW(hdc, &textMetric);
3932 sz.cx += (textMetric.tmMaxCharWidth * 2);
3934 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3935 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3937 if (infoPtr->hFont != 0)
3939 SelectObject(hdc, hOldFont);
3942 ReleaseDC(hwnd, hdc);
3944 infoPtr->editItem = hItem;
3946 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3947 WC_EDITW,
3949 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3950 WS_CLIPSIBLINGS | ES_WANTRETURN |
3951 ES_LEFT, hItem->textOffset - 2,
3952 hItem->rect.top - 1, sz.cx + 3,
3953 hItem->rect.bottom -
3954 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3955 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3957 infoPtr->hwndEdit = hwndEdit;
3959 /* Get a 2D border. */
3960 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3961 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3962 SetWindowLongW(hwndEdit, GWL_STYLE,
3963 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3965 SendMessageW(hwndEdit, WM_SETFONT,
3966 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3968 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3969 (DWORD_PTR)
3970 TREEVIEW_Edit_SubclassProc);
3971 if (hItem->pszText)
3972 SetWindowTextW(hwndEdit, hItem->pszText);
3974 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3976 DestroyWindow(hwndEdit);
3977 infoPtr->hwndEdit = 0;
3978 infoPtr->editItem = NULL;
3979 return NULL;
3982 SetFocus(hwndEdit);
3983 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3984 ShowWindow(hwndEdit, SW_SHOW);
3986 return hwndEdit;
3990 static LRESULT
3991 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3993 HWND hwnd = infoPtr->hwnd;
3994 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3995 NMTVDISPINFOW tvdi;
3996 BOOL bCommit;
3997 WCHAR tmpText[1024] = { '\0' };
3998 WCHAR *newText = tmpText;
3999 int iLength = 0;
4001 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
4003 tvdi.hdr.hwndFrom = hwnd;
4004 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
4005 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
4006 tvdi.item.mask = 0;
4007 tvdi.item.hItem = editedItem;
4008 tvdi.item.state = editedItem->state;
4009 tvdi.item.lParam = editedItem->lParam;
4011 if (!bCancel)
4013 if (!infoPtr->bNtfUnicode)
4014 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
4015 else
4016 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
4018 if (iLength >= 1023)
4020 ERR("Insufficient space to retrieve new item label\n");
4023 tvdi.item.mask = TVIF_TEXT;
4024 tvdi.item.pszText = tmpText;
4025 tvdi.item.cchTextMax = iLength + 1;
4027 else
4029 tvdi.item.pszText = NULL;
4030 tvdi.item.cchTextMax = 0;
4033 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
4035 if (!bCancel && bCommit) /* Apply the changes */
4037 if (!infoPtr->bNtfUnicode)
4039 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
4040 newText = Alloc(len * sizeof(WCHAR));
4041 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
4042 iLength = len - 1;
4045 if (strcmpW(newText, editedItem->pszText) != 0)
4047 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
4048 if (ptr == NULL)
4050 ERR("OutOfMemory, cannot allocate space for label\n");
4051 if(newText != tmpText) Free(newText);
4052 DestroyWindow(infoPtr->hwndEdit);
4053 infoPtr->hwndEdit = 0;
4054 infoPtr->editItem = NULL;
4055 return FALSE;
4057 else
4059 editedItem->pszText = ptr;
4060 editedItem->cchTextMax = iLength + 1;
4061 strcpyW(editedItem->pszText, newText);
4062 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
4065 if(newText != tmpText) Free(newText);
4068 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4069 DestroyWindow(infoPtr->hwndEdit);
4070 infoPtr->hwndEdit = 0;
4071 infoPtr->editItem = NULL;
4072 return TRUE;
4075 static LRESULT
4076 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4078 if (wParam != TV_EDIT_TIMER)
4080 ERR("got unknown timer\n");
4081 return 1;
4084 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4085 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4087 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4089 return 0;
4093 /* Mouse Tracking/Drag **************************************************/
4095 /***************************************************************************
4096 * This is quite unusual piece of code, but that's how it's implemented in
4097 * Windows.
4099 static LRESULT
4100 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4102 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4103 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4104 RECT r;
4105 MSG msg;
4107 r.top = pt.y - cyDrag;
4108 r.left = pt.x - cxDrag;
4109 r.bottom = pt.y + cyDrag;
4110 r.right = pt.x + cxDrag;
4112 SetCapture(infoPtr->hwnd);
4114 while (1)
4116 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4118 if (msg.message == WM_MOUSEMOVE)
4120 pt.x = (short)LOWORD(msg.lParam);
4121 pt.y = (short)HIWORD(msg.lParam);
4122 if (PtInRect(&r, pt))
4123 continue;
4124 else
4126 ReleaseCapture();
4127 return 1;
4130 else if (msg.message >= WM_LBUTTONDOWN &&
4131 msg.message <= WM_RBUTTONDBLCLK)
4133 break;
4136 DispatchMessageW(&msg);
4139 if (GetCapture() != infoPtr->hwnd)
4140 return 0;
4143 ReleaseCapture();
4144 return 0;
4148 static LRESULT
4149 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4151 TREEVIEW_ITEM *item;
4152 TVHITTESTINFO hit;
4154 TRACE("\n");
4155 SetFocus(infoPtr->hwnd);
4157 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4159 /* If there is pending 'edit label' event - kill it now */
4160 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4163 hit.pt.x = (short)LOWORD(lParam);
4164 hit.pt.y = (short)HIWORD(lParam);
4166 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4167 if (!item)
4168 return 0;
4169 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4171 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4172 { /* FIXME! */
4173 switch (hit.flags)
4175 case TVHT_ONITEMRIGHT:
4176 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4177 break;
4179 case TVHT_ONITEMINDENT:
4180 if (!(infoPtr->dwStyle & TVS_HASLINES))
4182 break;
4184 else
4186 int level = hit.pt.x / infoPtr->uIndent;
4187 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4189 while (item->iLevel > level)
4191 item = item->parent;
4194 /* fall through */
4197 case TVHT_ONITEMLABEL:
4198 case TVHT_ONITEMICON:
4199 case TVHT_ONITEMBUTTON:
4200 TREEVIEW_Toggle(infoPtr, item, TRUE);
4201 break;
4203 case TVHT_ONITEMSTATEICON:
4204 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4205 TREEVIEW_ToggleItemState(infoPtr, item);
4206 else
4207 TREEVIEW_Toggle(infoPtr, item, TRUE);
4208 break;
4211 return TRUE;
4215 static LRESULT
4216 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4218 HWND hwnd = infoPtr->hwnd;
4219 TVHITTESTINFO ht;
4220 BOOL bTrack, bDoLabelEdit;
4222 /* If Edit control is active - kill it and return.
4223 * The best way to do it is to set focus to itself.
4224 * Edit control subclassed procedure will automatically call
4225 * EndEditLabelNow.
4227 if (infoPtr->hwndEdit)
4229 SetFocus(hwnd);
4230 return 0;
4233 ht.pt.x = (short)LOWORD(lParam);
4234 ht.pt.y = (short)HIWORD(lParam);
4236 TREEVIEW_HitTest(infoPtr, &ht);
4237 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4239 /* update focusedItem and redraw both items */
4240 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4242 infoPtr->focusedItem = ht.hItem;
4243 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4244 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4247 bTrack = (ht.flags & TVHT_ONITEM)
4248 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4251 * If the style allows editing and the node is already selected
4252 * and the click occurred on the item label...
4254 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4255 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4257 /* Send NM_CLICK right away */
4258 if (!bTrack)
4259 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4260 goto setfocus;
4262 if (ht.flags & TVHT_ONITEMBUTTON)
4264 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4265 goto setfocus;
4267 else if (bTrack)
4268 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4269 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4271 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4272 infoPtr->dropItem = ht.hItem;
4274 /* clean up focusedItem as we dragged and won't select this item */
4275 if(infoPtr->focusedItem)
4277 /* refresh the item that was focused */
4278 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4279 infoPtr->focusedItem = NULL;
4281 /* refresh the selected item to return the filled background */
4282 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4285 return 0;
4289 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4290 goto setfocus;
4292 if (bDoLabelEdit)
4294 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4295 KillTimer(hwnd, TV_EDIT_TIMER);
4297 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4298 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4300 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4302 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4304 /* Select the current item */
4305 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4306 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4308 else if (ht.flags & TVHT_ONITEMSTATEICON)
4310 /* TVS_CHECKBOXES requires us to toggle the current state */
4311 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4312 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4315 setfocus:
4316 SetFocus(hwnd);
4317 return 0;
4321 static LRESULT
4322 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4324 TVHITTESTINFO ht;
4326 if (infoPtr->hwndEdit)
4328 SetFocus(infoPtr->hwnd);
4329 return 0;
4332 ht.pt.x = (short)LOWORD(lParam);
4333 ht.pt.y = (short)HIWORD(lParam);
4335 TREEVIEW_HitTest(infoPtr, &ht);
4337 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4339 if (ht.hItem)
4341 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4342 infoPtr->dropItem = ht.hItem;
4345 else
4347 SetFocus(infoPtr->hwnd);
4348 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4350 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4351 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4352 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4356 return 0;
4359 static LRESULT
4360 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4362 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4363 INT cx, cy;
4364 HDC hdc, htopdc;
4365 HWND hwtop;
4366 HBITMAP hbmp, hOldbmp;
4367 SIZE size;
4368 RECT rc;
4369 HFONT hOldFont;
4371 TRACE("\n");
4373 if (!(infoPtr->himlNormal))
4374 return 0;
4376 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4377 return 0;
4379 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4381 hwtop = GetDesktopWindow();
4382 htopdc = GetDC(hwtop);
4383 hdc = CreateCompatibleDC(htopdc);
4385 hOldFont = SelectObject(hdc, infoPtr->hFont);
4387 if (dragItem->pszText)
4388 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4389 &size);
4390 else
4391 GetTextExtentPoint32A(hdc, "", 0, &size);
4393 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4394 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4395 hOldbmp = SelectObject(hdc, hbmp);
4397 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4398 size.cx += cx;
4399 if (cy > size.cy)
4400 size.cy = cy;
4402 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4403 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4404 ILD_NORMAL);
4407 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4408 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4411 /* draw item text */
4413 SetRect(&rc, cx, 0, size.cx, size.cy);
4415 if (dragItem->pszText)
4416 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4417 DT_LEFT);
4419 SelectObject(hdc, hOldFont);
4420 SelectObject(hdc, hOldbmp);
4422 ImageList_Add(infoPtr->dragList, hbmp, 0);
4424 DeleteDC(hdc);
4425 DeleteObject(hbmp);
4426 ReleaseDC(hwtop, htopdc);
4428 return (LRESULT)infoPtr->dragList;
4431 /* Selection ************************************************************/
4433 static LRESULT
4434 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4435 INT cause)
4437 TREEVIEW_ITEM *prevSelect;
4439 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4441 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4442 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4443 newSelect ? newSelect->state : 0);
4445 /* reset and redraw focusedItem if focusedItem was set so we don't */
4446 /* have to worry about the previously focused item when we set a new one */
4447 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4448 infoPtr->focusedItem = NULL;
4450 switch (action)
4452 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4453 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4454 /* Fall through */
4455 case TVGN_CARET:
4456 prevSelect = infoPtr->selectedItem;
4458 if (prevSelect == newSelect) {
4459 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4460 break;
4463 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4464 TVN_SELCHANGINGW,
4465 cause,
4466 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4467 prevSelect,
4468 newSelect))
4469 return FALSE;
4471 if (prevSelect)
4472 prevSelect->state &= ~TVIS_SELECTED;
4473 if (newSelect)
4474 newSelect->state |= TVIS_SELECTED;
4476 infoPtr->selectedItem = newSelect;
4478 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4480 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4481 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4483 TREEVIEW_SendTreeviewNotify(infoPtr,
4484 TVN_SELCHANGEDW,
4485 cause,
4486 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4487 prevSelect,
4488 newSelect);
4489 break;
4491 case TVGN_DROPHILITE:
4492 prevSelect = infoPtr->dropItem;
4494 if (prevSelect)
4495 prevSelect->state &= ~TVIS_DROPHILITED;
4497 infoPtr->dropItem = newSelect;
4499 if (newSelect)
4500 newSelect->state |= TVIS_DROPHILITED;
4502 TREEVIEW_Invalidate(infoPtr, prevSelect);
4503 TREEVIEW_Invalidate(infoPtr, newSelect);
4504 break;
4506 case TVGN_FIRSTVISIBLE:
4507 if (newSelect != NULL)
4509 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4510 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4511 TREEVIEW_Invalidate(infoPtr, NULL);
4513 break;
4516 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4517 return TRUE;
4520 /* FIXME: handle NM_KILLFOCUS etc */
4521 static LRESULT
4522 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4524 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4526 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4527 return FALSE;
4529 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4531 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4532 return FALSE;
4534 TREEVIEW_SingleExpand(infoPtr, selection, item);
4536 return TRUE;
4539 /*************************************************************************
4540 * TREEVIEW_ProcessLetterKeys
4542 * Processes keyboard messages generated by pressing the letter keys
4543 * on the keyboard.
4544 * What this does is perform a case insensitive search from the
4545 * current position with the following quirks:
4546 * - If two chars or more are pressed in quick succession we search
4547 * for the corresponding string (e.g. 'abc').
4548 * - If there is a delay we wipe away the current search string and
4549 * restart with just that char.
4550 * - If the user keeps pressing the same character, whether slowly or
4551 * fast, so that the search string is entirely composed of this
4552 * character ('aaaaa' for instance), then we search for first item
4553 * that starting with that character.
4554 * - If the user types the above character in quick succession, then
4555 * we must also search for the corresponding string ('aaaaa'), and
4556 * go to that string if there is a match.
4558 * RETURNS
4560 * Zero.
4562 * BUGS
4564 * - The current implementation has a list of characters it will
4565 * accept and it ignores everything else. In particular it will
4566 * ignore accentuated characters which seems to match what
4567 * Windows does. But I'm not sure it makes sense to follow
4568 * Windows there.
4569 * - We don't sound a beep when the search fails.
4570 * - The search should start from the focused item, not from the selected
4571 * item. One reason for this is to allow for multiple selections in trees.
4572 * But currently infoPtr->focusedItem does not seem very usable.
4574 * SEE ALSO
4576 * TREEVIEW_ProcessLetterKeys
4578 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4580 HTREEITEM nItem;
4581 HTREEITEM endidx,idx;
4582 TVITEMEXW item;
4583 WCHAR buffer[MAX_PATH];
4584 DWORD timestamp,elapsed;
4586 /* simple parameter checking */
4587 if (!charCode || !keyData) return 0;
4589 /* only allow the valid WM_CHARs through */
4590 if (!isalnum(charCode) &&
4591 charCode != '.' && charCode != '`' && charCode != '!' &&
4592 charCode != '@' && charCode != '#' && charCode != '$' &&
4593 charCode != '%' && charCode != '^' && charCode != '&' &&
4594 charCode != '*' && charCode != '(' && charCode != ')' &&
4595 charCode != '-' && charCode != '_' && charCode != '+' &&
4596 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4597 charCode != '}' && charCode != '[' && charCode != '{' &&
4598 charCode != '/' && charCode != '?' && charCode != '>' &&
4599 charCode != '<' && charCode != ',' && charCode != '~')
4600 return 0;
4602 /* compute how much time elapsed since last keypress */
4603 timestamp = GetTickCount();
4604 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4605 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4606 } else {
4607 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4610 /* update the search parameters */
4611 infoPtr->lastKeyPressTimestamp=timestamp;
4612 if (elapsed < KEY_DELAY) {
4613 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4614 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4616 if (infoPtr->charCode != charCode) {
4617 infoPtr->charCode=charCode=0;
4619 } else {
4620 infoPtr->charCode=charCode;
4621 infoPtr->szSearchParam[0]=charCode;
4622 infoPtr->nSearchParamLength=1;
4623 /* Redundant with the 1 char string */
4624 charCode=0;
4627 /* and search from the current position */
4628 nItem=NULL;
4629 if (infoPtr->selectedItem != NULL) {
4630 endidx=infoPtr->selectedItem;
4631 /* if looking for single character match,
4632 * then we must always move forward
4634 if (infoPtr->nSearchParamLength == 1)
4635 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4636 else
4637 idx=endidx;
4638 } else {
4639 endidx=NULL;
4640 idx=infoPtr->root->firstChild;
4642 do {
4643 /* At the end point, sort out wrapping */
4644 if (idx == NULL) {
4646 /* If endidx is null, stop at the last item (ie top to bottom) */
4647 if (endidx == NULL)
4648 break;
4650 /* Otherwise, start again at the very beginning */
4651 idx=infoPtr->root->firstChild;
4653 /* But if we are stopping on the first child, end now! */
4654 if (idx == endidx) break;
4657 /* get item */
4658 ZeroMemory(&item, sizeof(item));
4659 item.mask = TVIF_TEXT;
4660 item.hItem = idx;
4661 item.pszText = buffer;
4662 item.cchTextMax = sizeof(buffer);
4663 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4665 /* check for a match */
4666 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4667 nItem=idx;
4668 break;
4669 } else if ( (charCode != 0) && (nItem == NULL) &&
4670 (nItem != infoPtr->selectedItem) &&
4671 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4672 /* This would work but we must keep looking for a longer match */
4673 nItem=idx;
4675 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4676 } while (idx != endidx);
4678 if (nItem != NULL) {
4679 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4680 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4684 return 0;
4687 /* Scrolling ************************************************************/
4689 static LRESULT
4690 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4692 int viscount;
4693 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4694 HTREEITEM newFirstVisible = NULL;
4695 int visible_pos = -1;
4697 if (!TREEVIEW_ValidItem(infoPtr, item))
4698 return FALSE;
4700 if (!ISVISIBLE(item))
4702 /* Expand parents as necessary. */
4703 HTREEITEM parent;
4705 /* see if we are trying to ensure that root is visible */
4706 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4707 parent = item->parent;
4708 else
4709 parent = item; /* this item is the topmost item */
4711 while (parent != infoPtr->root)
4713 if (!(parent->state & TVIS_EXPANDED))
4714 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4716 parent = parent->parent;
4720 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4722 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4723 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4725 if (hasFirstVisible)
4726 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4728 if (visible_pos < 0)
4730 /* item is before the start of the list: put it at the top. */
4731 newFirstVisible = item;
4733 else if (visible_pos >= viscount
4734 /* Sometimes, before we are displayed, GVC is 0, causing us to
4735 * spuriously scroll up. */
4736 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4738 /* item is past the end of the list. */
4739 int scroll = visible_pos - viscount;
4741 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4742 scroll + 1);
4745 if (bHScroll)
4747 /* Scroll window so item's text is visible as much as possible */
4748 /* Calculation of amount of extra space is taken from EditLabel code */
4749 INT pos, x;
4750 TEXTMETRICW textMetric;
4751 HDC hdc = GetWindowDC(infoPtr->hwnd);
4753 x = item->textWidth;
4755 GetTextMetricsW(hdc, &textMetric);
4756 ReleaseDC(infoPtr->hwnd, hdc);
4758 x += (textMetric.tmMaxCharWidth * 2);
4759 x = max(x, textMetric.tmMaxCharWidth * 3);
4761 if (item->textOffset < 0)
4762 pos = item->textOffset;
4763 else if (item->textOffset + x > infoPtr->clientWidth)
4765 if (x > infoPtr->clientWidth)
4766 pos = item->textOffset;
4767 else
4768 pos = item->textOffset + x - infoPtr->clientWidth;
4770 else
4771 pos = 0;
4773 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4776 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4778 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4780 return TRUE;
4783 return FALSE;
4786 static VOID
4787 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4788 TREEVIEW_ITEM *newFirstVisible,
4789 BOOL bUpdateScrollPos)
4791 int gap_size;
4793 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4795 if (newFirstVisible != NULL)
4797 /* Prevent an empty gap from appearing at the bottom... */
4798 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4799 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4801 if (gap_size > 0)
4803 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4804 -gap_size);
4806 /* ... unless we just don't have enough items. */
4807 if (newFirstVisible == NULL)
4808 newFirstVisible = infoPtr->root->firstChild;
4812 if (infoPtr->firstVisible != newFirstVisible)
4814 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4816 infoPtr->firstVisible = newFirstVisible;
4817 TREEVIEW_Invalidate(infoPtr, NULL);
4819 else
4821 TREEVIEW_ITEM *item;
4822 int scroll = infoPtr->uItemHeight *
4823 (infoPtr->firstVisible->visibleOrder
4824 - newFirstVisible->visibleOrder);
4826 infoPtr->firstVisible = newFirstVisible;
4828 for (item = infoPtr->root->firstChild; item != NULL;
4829 item = TREEVIEW_GetNextListItem(infoPtr, item))
4831 item->rect.top += scroll;
4832 item->rect.bottom += scroll;
4835 if (bUpdateScrollPos)
4836 SetScrollPos(infoPtr->hwnd, SB_VERT,
4837 newFirstVisible->visibleOrder, TRUE);
4839 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4844 /************************************************************************
4845 * VScroll is always in units of visible items. i.e. we always have a
4846 * visible item aligned to the top of the control. (Unless we have no
4847 * items at all.)
4849 static LRESULT
4850 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4852 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4853 TREEVIEW_ITEM *newFirstVisible = NULL;
4855 int nScrollCode = LOWORD(wParam);
4857 TRACE("wp %lx\n", wParam);
4859 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4860 return 0;
4862 if (!oldFirstVisible)
4864 assert(infoPtr->root->firstChild == NULL);
4865 return 0;
4868 switch (nScrollCode)
4870 case SB_TOP:
4871 newFirstVisible = infoPtr->root->firstChild;
4872 break;
4874 case SB_BOTTOM:
4875 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4876 break;
4878 case SB_LINEUP:
4879 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4880 break;
4882 case SB_LINEDOWN:
4883 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4884 break;
4886 case SB_PAGEUP:
4887 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4888 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4889 break;
4891 case SB_PAGEDOWN:
4892 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4893 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4894 break;
4896 case SB_THUMBTRACK:
4897 case SB_THUMBPOSITION:
4898 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4899 infoPtr->root->firstChild,
4900 (LONG)(SHORT)HIWORD(wParam));
4901 break;
4903 case SB_ENDSCROLL:
4904 return 0;
4907 if (newFirstVisible != NULL)
4909 if (newFirstVisible != oldFirstVisible)
4910 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4911 nScrollCode != SB_THUMBTRACK);
4912 else if (nScrollCode == SB_THUMBPOSITION)
4913 SetScrollPos(infoPtr->hwnd, SB_VERT,
4914 newFirstVisible->visibleOrder, TRUE);
4917 return 0;
4920 static LRESULT
4921 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4923 int maxWidth;
4924 int scrollX = infoPtr->scrollX;
4925 int nScrollCode = LOWORD(wParam);
4927 TRACE("wp %lx\n", wParam);
4929 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4930 return FALSE;
4932 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4933 /* shall never occur */
4934 if (maxWidth <= 0)
4936 scrollX = 0;
4937 goto scroll;
4940 switch (nScrollCode)
4942 case SB_LINELEFT:
4943 scrollX -= infoPtr->uItemHeight;
4944 break;
4945 case SB_LINERIGHT:
4946 scrollX += infoPtr->uItemHeight;
4947 break;
4948 case SB_PAGELEFT:
4949 scrollX -= infoPtr->clientWidth;
4950 break;
4951 case SB_PAGERIGHT:
4952 scrollX += infoPtr->clientWidth;
4953 break;
4955 case SB_THUMBTRACK:
4956 case SB_THUMBPOSITION:
4957 scrollX = (int)(SHORT)HIWORD(wParam);
4958 break;
4960 case SB_ENDSCROLL:
4961 return 0;
4964 if (scrollX > maxWidth)
4965 scrollX = maxWidth;
4966 else if (scrollX < 0)
4967 scrollX = 0;
4969 scroll:
4970 if (scrollX != infoPtr->scrollX)
4972 TREEVIEW_ITEM *item;
4973 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4975 for (item = infoPtr->root->firstChild; item != NULL;
4976 item = TREEVIEW_GetNextListItem(infoPtr, item))
4978 item->linesOffset += scroll_pixels;
4979 item->stateOffset += scroll_pixels;
4980 item->imageOffset += scroll_pixels;
4981 item->textOffset += scroll_pixels;
4984 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4985 infoPtr->scrollX = scrollX;
4986 UpdateWindow(infoPtr->hwnd);
4989 if (nScrollCode != SB_THUMBTRACK)
4990 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4992 return 0;
4995 static LRESULT
4996 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4998 short wheelDelta;
4999 UINT pulScrollLines = 3;
5001 if (wParam & (MK_SHIFT | MK_CONTROL))
5002 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
5004 if (infoPtr->firstVisible == NULL)
5005 return TRUE;
5007 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
5009 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5010 /* if scrolling changes direction, ignore left overs */
5011 if ((wheelDelta < 0 && infoPtr->wheelRemainder < 0) ||
5012 (wheelDelta > 0 && infoPtr->wheelRemainder > 0))
5013 infoPtr->wheelRemainder += wheelDelta;
5014 else
5015 infoPtr->wheelRemainder = wheelDelta;
5017 if (infoPtr->wheelRemainder && pulScrollLines)
5019 int newDy;
5020 int maxDy;
5021 int lineScroll;
5023 lineScroll = pulScrollLines * (float)infoPtr->wheelRemainder / WHEEL_DELTA;
5024 infoPtr->wheelRemainder -= WHEEL_DELTA * lineScroll / (int)pulScrollLines;
5026 newDy = infoPtr->firstVisible->visibleOrder - lineScroll;
5027 maxDy = infoPtr->maxVisibleOrder;
5029 if (newDy > maxDy)
5030 newDy = maxDy;
5032 if (newDy < 0)
5033 newDy = 0;
5035 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
5037 return TRUE;
5040 /* Create/Destroy *******************************************************/
5042 static LRESULT
5043 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
5045 RECT rcClient;
5046 TREEVIEW_INFO *infoPtr;
5047 LOGFONTW lf;
5049 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5051 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
5053 if (infoPtr == NULL)
5055 ERR("could not allocate info memory!\n");
5056 return 0;
5059 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5061 infoPtr->hwnd = hwnd;
5062 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5063 infoPtr->Timer = 0;
5064 infoPtr->uNumItems = 0;
5065 infoPtr->cdmode = 0;
5066 infoPtr->uScrollTime = 300; /* milliseconds */
5067 infoPtr->bRedraw = TRUE;
5069 GetClientRect(hwnd, &rcClient);
5071 /* No scroll bars yet. */
5072 infoPtr->clientWidth = rcClient.right;
5073 infoPtr->clientHeight = rcClient.bottom;
5074 infoPtr->uInternalStatus = 0;
5076 infoPtr->treeWidth = 0;
5077 infoPtr->treeHeight = 0;
5079 infoPtr->uIndent = MINIMUM_INDENT;
5080 infoPtr->selectedItem = NULL;
5081 infoPtr->focusedItem = NULL;
5082 infoPtr->hotItem = NULL;
5083 infoPtr->editItem = NULL;
5084 infoPtr->firstVisible = NULL;
5085 infoPtr->maxVisibleOrder = 0;
5086 infoPtr->dropItem = NULL;
5087 infoPtr->insertMarkItem = NULL;
5088 infoPtr->insertBeforeorAfter = 0;
5089 /* dragList */
5091 infoPtr->scrollX = 0;
5092 infoPtr->wheelRemainder = 0;
5094 infoPtr->clrBk = CLR_NONE; /* use system color */
5095 infoPtr->clrText = CLR_NONE; /* use system color */
5096 infoPtr->clrLine = CLR_DEFAULT;
5097 infoPtr->clrInsertMark = CLR_DEFAULT;
5099 /* hwndToolTip */
5101 infoPtr->hwndEdit = NULL;
5102 infoPtr->wpEditOrig = NULL;
5103 infoPtr->bIgnoreEditKillFocus = FALSE;
5104 infoPtr->bLabelChanged = FALSE;
5106 infoPtr->himlNormal = NULL;
5107 infoPtr->himlState = NULL;
5108 infoPtr->normalImageWidth = 0;
5109 infoPtr->normalImageHeight = 0;
5110 infoPtr->stateImageWidth = 0;
5111 infoPtr->stateImageHeight = 0;
5113 infoPtr->items = DPA_Create(16);
5115 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5116 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5117 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5118 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5119 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
5120 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5122 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5124 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5125 infoPtr->root->state = TVIS_EXPANDED;
5126 infoPtr->root->iLevel = -1;
5127 infoPtr->root->visibleOrder = -1;
5129 infoPtr->hwndNotify = lpcs->hwndParent;
5130 infoPtr->hwndToolTip = 0;
5132 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5133 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5135 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5136 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5137 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5138 hwnd, 0, 0, 0);
5140 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5141 ShowScrollBar(hwnd, SB_VERT, FALSE);
5142 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5144 OpenThemeData (hwnd, themeClass);
5146 return 0;
5150 static LRESULT
5151 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5153 TRACE("\n");
5155 /* free item data */
5156 TREEVIEW_RemoveTree(infoPtr);
5157 /* root isn't freed with other items */
5158 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5159 DPA_Destroy(infoPtr->items);
5161 /* tool tip is automatically destroyed: we are its owner */
5163 /* Restore original wndproc */
5164 if (infoPtr->hwndEdit)
5165 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5166 (DWORD_PTR)infoPtr->wpEditOrig);
5168 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5170 /* Deassociate treeview from the window before doing anything drastic. */
5171 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5173 DeleteObject(infoPtr->hDefaultFont);
5174 DeleteObject(infoPtr->hBoldFont);
5175 DeleteObject(infoPtr->hUnderlineFont);
5176 DeleteObject(infoPtr->hBoldUnderlineFont);
5177 Free(infoPtr);
5179 return 0;
5182 /* Miscellaneous Messages ***********************************************/
5184 static LRESULT
5185 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5187 static const struct
5189 unsigned char code;
5191 scroll[] =
5193 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5194 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5195 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5196 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5197 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5198 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5199 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5200 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5201 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5202 #undef SCROLL_ENTRY
5205 if (key >= VK_PRIOR && key <= VK_DOWN)
5207 unsigned char code = scroll[key - VK_PRIOR].code;
5209 (((code & (1 << 7)) == (SB_HORZ << 7))
5210 ? TREEVIEW_HScroll
5211 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5214 return 0;
5217 /************************************************************************
5218 * TREEVIEW_KeyDown
5220 * VK_UP Move selection to the previous non-hidden item.
5221 * VK_DOWN Move selection to the next non-hidden item.
5222 * VK_HOME Move selection to the first item.
5223 * VK_END Move selection to the last item.
5224 * VK_LEFT If expanded then collapse, otherwise move to parent.
5225 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5226 * VK_ADD Expand.
5227 * VK_SUBTRACT Collapse.
5228 * VK_MULTIPLY Expand all.
5229 * VK_PRIOR Move up GetVisibleCount items.
5230 * VK_NEXT Move down GetVisibleCount items.
5231 * VK_BACK Move to parent.
5232 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5234 static LRESULT
5235 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5237 /* If it is non-NULL and different, it will be selected and visible. */
5238 TREEVIEW_ITEM *newSelection = NULL;
5240 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5242 TRACE("%lx\n", wParam);
5244 if (prevItem == NULL)
5245 return FALSE;
5247 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5248 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5250 switch (wParam)
5252 case VK_UP:
5253 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5254 if (!newSelection)
5255 newSelection = infoPtr->root->firstChild;
5256 break;
5258 case VK_DOWN:
5259 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5260 break;
5262 case VK_HOME:
5263 newSelection = infoPtr->root->firstChild;
5264 break;
5266 case VK_END:
5267 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5268 break;
5270 case VK_LEFT:
5271 if (prevItem->state & TVIS_EXPANDED)
5273 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5275 else if (prevItem->parent != infoPtr->root)
5277 newSelection = prevItem->parent;
5279 break;
5281 case VK_RIGHT:
5282 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5284 if (!(prevItem->state & TVIS_EXPANDED))
5285 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5286 else
5288 newSelection = prevItem->firstChild;
5292 break;
5294 case VK_MULTIPLY:
5295 TREEVIEW_ExpandAll(infoPtr, prevItem);
5296 break;
5298 case VK_ADD:
5299 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5300 break;
5302 case VK_SUBTRACT:
5303 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5304 break;
5306 case VK_PRIOR:
5307 newSelection
5308 = TREEVIEW_GetListItem(infoPtr, prevItem,
5309 -TREEVIEW_GetVisibleCount(infoPtr));
5310 break;
5312 case VK_NEXT:
5313 newSelection
5314 = TREEVIEW_GetListItem(infoPtr, prevItem,
5315 TREEVIEW_GetVisibleCount(infoPtr));
5316 break;
5318 case VK_BACK:
5319 newSelection = prevItem->parent;
5320 if (newSelection == infoPtr->root)
5321 newSelection = NULL;
5322 break;
5324 case VK_SPACE:
5325 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5326 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5327 break;
5330 if (newSelection && newSelection != prevItem)
5332 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5333 TVC_BYKEYBOARD))
5335 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5339 return FALSE;
5342 static LRESULT
5343 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5345 /* remove hot effect from item */
5346 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5347 infoPtr->hotItem = NULL;
5349 return 0;
5352 static LRESULT
5353 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5355 POINT pt;
5356 TRACKMOUSEEVENT trackinfo;
5357 TREEVIEW_ITEM * item;
5359 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5361 /* fill in the TRACKMOUSEEVENT struct */
5362 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5363 trackinfo.dwFlags = TME_QUERY;
5364 trackinfo.hwndTrack = infoPtr->hwnd;
5366 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5367 _TrackMouseEvent(&trackinfo);
5369 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5370 if(!(trackinfo.dwFlags & TME_LEAVE))
5372 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5373 trackinfo.hwndTrack = infoPtr->hwnd;
5374 /* do it as fast as possible, minimal systimer latency will be used */
5375 trackinfo.dwHoverTime = 1;
5377 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5378 /* and can properly deactivate the hot item */
5379 _TrackMouseEvent(&trackinfo);
5382 pt.x = (short)LOWORD(lParam);
5383 pt.y = (short)HIWORD(lParam);
5385 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5387 if (item != infoPtr->hotItem)
5389 /* redraw old hot item */
5390 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5391 infoPtr->hotItem = item;
5392 /* redraw new hot item */
5393 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5396 return 0;
5399 /* Draw themed border */
5400 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5402 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5403 HDC dc;
5404 RECT r;
5405 HRGN cliprgn;
5406 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5407 cyEdge = GetSystemMetrics (SM_CYEDGE);
5409 if (!theme)
5410 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5412 GetWindowRect(infoPtr->hwnd, &r);
5414 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5415 r.right - cxEdge, r.bottom - cyEdge);
5416 if (region != (HRGN)1)
5417 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5418 OffsetRect(&r, -r.left, -r.top);
5420 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5421 OffsetRect(&r, -r.left, -r.top);
5423 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5424 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5425 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5426 ReleaseDC(infoPtr->hwnd, dc);
5428 /* Call default proc to get the scrollbars etc. painted */
5429 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5431 return TRUE;
5434 static LRESULT
5435 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5437 LPNMHDR lpnmh = (LPNMHDR)lParam;
5439 if (lpnmh->code == PGN_CALCSIZE) {
5440 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5442 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5443 lppgc->iWidth = infoPtr->treeWidth;
5444 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5445 infoPtr->treeWidth, infoPtr->clientWidth);
5447 else {
5448 lppgc->iHeight = infoPtr->treeHeight;
5449 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5450 infoPtr->treeHeight, infoPtr->clientHeight);
5452 return 0;
5454 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5457 static LRESULT
5458 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5460 if (wParam == SIZE_RESTORED)
5462 infoPtr->clientWidth = (short)LOWORD(lParam);
5463 infoPtr->clientHeight = (short)HIWORD(lParam);
5465 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5466 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5467 TREEVIEW_UpdateScrollBars(infoPtr);
5469 else
5471 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5474 TREEVIEW_Invalidate(infoPtr, NULL);
5475 return 0;
5478 static LRESULT
5479 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5481 TRACE("(%lx %lx)\n", wParam, lParam);
5483 if (wParam == GWL_STYLE)
5485 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5487 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5489 if (dwNewStyle & TVS_CHECKBOXES)
5491 TREEVIEW_InitCheckboxes(infoPtr);
5492 TRACE("checkboxes enabled\n");
5494 /* set all items to state image index 1 */
5495 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5497 else
5499 FIXME("tried to disable checkboxes\n");
5503 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5505 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5507 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5508 TRACE("tooltips enabled\n");
5510 else
5512 DestroyWindow(infoPtr->hwndToolTip);
5513 infoPtr->hwndToolTip = 0;
5514 TRACE("tooltips disabled\n");
5518 infoPtr->dwStyle = dwNewStyle;
5521 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
5522 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5523 TREEVIEW_UpdateScrollBars(infoPtr);
5524 TREEVIEW_Invalidate(infoPtr, NULL);
5526 return 0;
5529 static LRESULT
5530 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5532 POINT pt;
5533 TREEVIEW_ITEM * item;
5534 NMMOUSE nmmouse;
5536 GetCursorPos(&pt);
5537 ScreenToClient(infoPtr->hwnd, &pt);
5539 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5541 memset(&nmmouse, 0, sizeof(nmmouse));
5542 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5543 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5544 nmmouse.hdr.code = NM_SETCURSOR;
5545 if (item)
5547 nmmouse.dwItemSpec = (DWORD_PTR)item;
5548 nmmouse.dwItemData = item->lParam;
5550 nmmouse.pt.x = 0;
5551 nmmouse.pt.y = 0;
5552 nmmouse.dwHitInfo = lParam;
5553 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, &nmmouse.hdr))
5554 return 0;
5556 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5558 SetCursor(infoPtr->hcurHand);
5559 return 0;
5561 else
5562 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5565 static LRESULT
5566 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5568 TRACE("\n");
5570 if (!infoPtr->selectedItem)
5572 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5573 TVC_UNKNOWN);
5576 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5577 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5578 return 0;
5581 static LRESULT
5582 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5584 TRACE("\n");
5586 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5587 UpdateWindow(infoPtr->hwnd);
5588 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5589 return 0;
5592 /* update theme after a WM_THEMECHANGED message */
5593 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5595 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5596 CloseThemeData (theme);
5597 OpenThemeData (infoPtr->hwnd, themeClass);
5598 return 0;
5602 static LRESULT WINAPI
5603 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5605 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5607 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5609 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5610 else
5612 if (uMsg == WM_CREATE)
5613 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5614 else
5615 goto def;
5618 switch (uMsg)
5620 case TVM_CREATEDRAGIMAGE:
5621 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5623 case TVM_DELETEITEM:
5624 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5626 case TVM_EDITLABELA:
5627 case TVM_EDITLABELW:
5628 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5630 case TVM_ENDEDITLABELNOW:
5631 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5633 case TVM_ENSUREVISIBLE:
5634 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5636 case TVM_EXPAND:
5637 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5639 case TVM_GETBKCOLOR:
5640 return TREEVIEW_GetBkColor(infoPtr);
5642 case TVM_GETCOUNT:
5643 return TREEVIEW_GetCount(infoPtr);
5645 case TVM_GETEDITCONTROL:
5646 return TREEVIEW_GetEditControl(infoPtr);
5648 case TVM_GETIMAGELIST:
5649 return TREEVIEW_GetImageList(infoPtr, wParam);
5651 case TVM_GETINDENT:
5652 return TREEVIEW_GetIndent(infoPtr);
5654 case TVM_GETINSERTMARKCOLOR:
5655 return TREEVIEW_GetInsertMarkColor(infoPtr);
5657 case TVM_GETISEARCHSTRINGA:
5658 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5659 return 0;
5661 case TVM_GETISEARCHSTRINGW:
5662 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5663 return 0;
5665 case TVM_GETITEMA:
5666 case TVM_GETITEMW:
5667 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5668 uMsg == TVM_GETITEMW);
5669 case TVM_GETITEMHEIGHT:
5670 return TREEVIEW_GetItemHeight(infoPtr);
5672 case TVM_GETITEMRECT:
5673 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5675 case TVM_GETITEMSTATE:
5676 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5678 case TVM_GETLINECOLOR:
5679 return TREEVIEW_GetLineColor(infoPtr);
5681 case TVM_GETNEXTITEM:
5682 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5684 case TVM_GETSCROLLTIME:
5685 return TREEVIEW_GetScrollTime(infoPtr);
5687 case TVM_GETTEXTCOLOR:
5688 return TREEVIEW_GetTextColor(infoPtr);
5690 case TVM_GETTOOLTIPS:
5691 return TREEVIEW_GetToolTips(infoPtr);
5693 case TVM_GETUNICODEFORMAT:
5694 return TREEVIEW_GetUnicodeFormat(infoPtr);
5696 case TVM_GETVISIBLECOUNT:
5697 return TREEVIEW_GetVisibleCount(infoPtr);
5699 case TVM_HITTEST:
5700 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5702 case TVM_INSERTITEMA:
5703 case TVM_INSERTITEMW:
5704 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5705 uMsg == TVM_INSERTITEMW);
5706 case TVM_SELECTITEM:
5707 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5709 case TVM_SETBKCOLOR:
5710 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5712 case TVM_SETIMAGELIST:
5713 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5715 case TVM_SETINDENT:
5716 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5718 case TVM_SETINSERTMARK:
5719 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5721 case TVM_SETINSERTMARKCOLOR:
5722 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5724 case TVM_SETITEMA:
5725 case TVM_SETITEMW:
5726 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5727 uMsg == TVM_SETITEMW);
5728 case TVM_SETLINECOLOR:
5729 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5731 case TVM_SETITEMHEIGHT:
5732 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5734 case TVM_SETSCROLLTIME:
5735 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5737 case TVM_SETTEXTCOLOR:
5738 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5740 case TVM_SETTOOLTIPS:
5741 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5743 case TVM_SETUNICODEFORMAT:
5744 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5746 case TVM_SORTCHILDREN:
5747 return TREEVIEW_SortChildren(infoPtr, lParam);
5749 case TVM_SORTCHILDRENCB:
5750 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5752 case WM_CHAR:
5753 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5755 case WM_COMMAND:
5756 return TREEVIEW_Command(infoPtr, wParam, lParam);
5758 case WM_DESTROY:
5759 return TREEVIEW_Destroy(infoPtr);
5761 /* WM_ENABLE */
5763 case WM_ERASEBKGND:
5764 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5766 case WM_GETDLGCODE:
5767 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5769 case WM_GETFONT:
5770 return TREEVIEW_GetFont(infoPtr);
5772 case WM_HSCROLL:
5773 return TREEVIEW_HScroll(infoPtr, wParam);
5775 case WM_KEYDOWN:
5776 return TREEVIEW_KeyDown(infoPtr, wParam);
5778 case WM_KILLFOCUS:
5779 return TREEVIEW_KillFocus(infoPtr);
5781 case WM_LBUTTONDBLCLK:
5782 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5784 case WM_LBUTTONDOWN:
5785 return TREEVIEW_LButtonDown(infoPtr, lParam);
5787 /* WM_MBUTTONDOWN */
5789 case WM_MOUSELEAVE:
5790 return TREEVIEW_MouseLeave(infoPtr);
5792 case WM_MOUSEMOVE:
5793 return TREEVIEW_MouseMove(infoPtr, lParam);
5795 case WM_NCLBUTTONDOWN:
5796 if (infoPtr->hwndEdit)
5797 SetFocus(infoPtr->hwnd);
5798 goto def;
5800 case WM_NCPAINT:
5801 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5803 case WM_NOTIFY:
5804 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5806 case WM_NOTIFYFORMAT:
5807 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5809 case WM_PRINTCLIENT:
5810 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5812 case WM_PAINT:
5813 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5815 case WM_RBUTTONDOWN:
5816 return TREEVIEW_RButtonDown(infoPtr, lParam);
5818 case WM_SETCURSOR:
5819 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5821 case WM_SETFOCUS:
5822 return TREEVIEW_SetFocus(infoPtr);
5824 case WM_SETFONT:
5825 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5827 case WM_SETREDRAW:
5828 return TREEVIEW_SetRedraw(infoPtr, wParam);
5830 case WM_SIZE:
5831 return TREEVIEW_Size(infoPtr, wParam, lParam);
5833 case WM_STYLECHANGED:
5834 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5836 case WM_SYSCOLORCHANGE:
5837 COMCTL32_RefreshSysColors();
5838 return 0;
5840 /* WM_SYSKEYDOWN */
5842 case WM_TIMER:
5843 return TREEVIEW_HandleTimer(infoPtr, wParam);
5845 case WM_THEMECHANGED:
5846 return TREEVIEW_ThemeChanged (infoPtr);
5848 case WM_VSCROLL:
5849 return TREEVIEW_VScroll(infoPtr, wParam);
5851 /* WM_WININICHANGE */
5853 case WM_MOUSEWHEEL:
5854 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5856 case WM_DRAWITEM:
5857 TRACE("drawItem\n");
5858 goto def;
5860 default:
5861 /* This mostly catches MFC and Delphi messages. :( */
5862 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5863 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5864 def:
5865 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5870 /* Class Registration ***************************************************/
5872 VOID
5873 TREEVIEW_Register(void)
5875 WNDCLASSW wndClass;
5877 TRACE("\n");
5879 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5880 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5881 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5882 wndClass.cbClsExtra = 0;
5883 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5885 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5886 wndClass.hbrBackground = 0;
5887 wndClass.lpszClassName = WC_TREEVIEWW;
5889 RegisterClassW(&wndClass);
5893 VOID
5894 TREEVIEW_Unregister(void)
5896 UnregisterClassW(WC_TREEVIEWW, NULL);
5900 /* Tree Verification ****************************************************/
5902 static inline void
5903 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5905 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5906 const TREEVIEW_ITEM *item)
5908 assert(infoPtr != NULL);
5909 assert(item != NULL);
5911 /* both NULL, or both non-null */
5912 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5914 assert(item->firstChild != item);
5915 assert(item->lastChild != item);
5917 if (item->firstChild)
5919 assert(item->firstChild->parent == item);
5920 assert(item->firstChild->prevSibling == NULL);
5923 if (item->lastChild)
5925 assert(item->lastChild->parent == item);
5926 assert(item->lastChild->nextSibling == NULL);
5929 assert(item->nextSibling != item);
5930 if (item->nextSibling)
5932 assert(item->nextSibling->parent == item->parent);
5933 assert(item->nextSibling->prevSibling == item);
5936 assert(item->prevSibling != item);
5937 if (item->prevSibling)
5939 assert(item->prevSibling->parent == item->parent);
5940 assert(item->prevSibling->nextSibling == item);
5944 static inline void
5945 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5947 assert(item != NULL);
5949 assert(item->parent != NULL);
5950 assert(item->parent != item);
5951 assert(item->iLevel == item->parent->iLevel + 1);
5953 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5955 TREEVIEW_VerifyItemCommon(infoPtr, item);
5957 TREEVIEW_VerifyChildren(infoPtr, item);
5960 static inline void
5961 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5963 const TREEVIEW_ITEM *child;
5964 assert(item != NULL);
5966 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5967 TREEVIEW_VerifyItem(infoPtr, child);
5970 static inline void
5971 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5973 TREEVIEW_ITEM *root = infoPtr->root;
5975 assert(root != NULL);
5976 assert(root->iLevel == -1);
5977 assert(root->parent == NULL);
5978 assert(root->prevSibling == NULL);
5980 TREEVIEW_VerifyItemCommon(infoPtr, root);
5982 TREEVIEW_VerifyChildren(infoPtr, root);
5985 static void
5986 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5988 if (!TRACE_ON(treeview)) return;
5990 assert(infoPtr != NULL);
5991 TREEVIEW_VerifyRoot(infoPtr);