kernel32: Use BOOL type where appropriate.
[wine.git] / dlls / comctl32 / treeview.c
blobc73fb54ab8d06f0c8e9e811c7340405d2fcaee3e
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 LRESULT
2947 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2949 HDC hdc;
2950 PAINTSTRUCT ps;
2951 RECT rc;
2953 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2955 if (hdc_ref)
2957 hdc = hdc_ref;
2958 GetClientRect(infoPtr->hwnd, &rc);
2959 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2961 else
2963 hdc = BeginPaint(infoPtr->hwnd, &ps);
2964 rc = ps.rcPaint;
2965 if(ps.fErase)
2966 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2969 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2970 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2972 if (!hdc_ref)
2973 EndPaint(infoPtr->hwnd, &ps);
2975 return 0;
2978 static LRESULT
2979 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2981 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2983 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2984 return 0;
2986 if (options & PRF_ERASEBKGND)
2987 TREEVIEW_EraseBackground(infoPtr, hdc);
2989 if (options & PRF_CLIENT)
2991 RECT rc;
2992 GetClientRect(infoPtr->hwnd, &rc);
2993 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2996 return 0;
2999 /* Sorting **************************************************************/
3001 /***************************************************************************
3002 * Forward the DPA local callback to the treeview owner callback
3004 static INT WINAPI
3005 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3006 const TVSORTCB *pCallBackSort)
3008 /* Forward the call to the client-defined callback */
3009 return pCallBackSort->lpfnCompare(first->lParam,
3010 second->lParam,
3011 pCallBackSort->lParam);
3014 /***************************************************************************
3015 * Treeview native sort routine: sort on item text.
3017 static INT WINAPI
3018 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3019 const TREEVIEW_INFO *infoPtr)
3021 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3022 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3024 if(first->pszText && second->pszText)
3025 return lstrcmpiW(first->pszText, second->pszText);
3026 else if(first->pszText)
3027 return -1;
3028 else if(second->pszText)
3029 return 1;
3030 else
3031 return 0;
3034 /* Returns the number of physical children belonging to item. */
3035 static INT
3036 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3038 INT cChildren = 0;
3039 HTREEITEM hti;
3041 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3042 cChildren++;
3044 return cChildren;
3047 /* Returns a DPA containing a pointer to each physical child of item in
3048 * sibling order. If item has no children, an empty DPA is returned. */
3049 static HDPA
3050 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3052 HTREEITEM child;
3054 HDPA list = DPA_Create(8);
3055 if (list == 0) return NULL;
3057 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3059 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3061 DPA_Destroy(list);
3062 return NULL;
3066 return list;
3069 /***************************************************************************
3070 * Setup the treeview structure with regards of the sort method
3071 * and sort the children of the TV item specified in lParam
3072 * fRecurse: currently unused. Should be zero.
3073 * parent: if pSort!=NULL, should equal pSort->hParent.
3074 * otherwise, item which child items are to be sorted.
3075 * pSort: sort method info. if NULL, sort on item text.
3076 * if non-NULL, sort on item's lParam content, and let the
3077 * application decide what that means. See also TVM_SORTCHILDRENCB.
3080 static LRESULT
3081 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3082 LPTVSORTCB pSort)
3084 INT cChildren;
3085 PFNDPACOMPARE pfnCompare;
3086 LPARAM lpCompare;
3088 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3089 if (parent == TVI_ROOT || parent == NULL)
3090 parent = infoPtr->root;
3092 /* Check for a valid handle to the parent item */
3093 if (!TREEVIEW_ValidItem(infoPtr, parent))
3095 ERR("invalid item hParent=%p\n", parent);
3096 return FALSE;
3099 if (pSort)
3101 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3102 lpCompare = (LPARAM)pSort;
3104 else
3106 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3107 lpCompare = (LPARAM)infoPtr;
3110 cChildren = TREEVIEW_CountChildren(parent);
3112 /* Make sure there is something to sort */
3113 if (cChildren > 1)
3115 /* TREEVIEW_ITEM rechaining */
3116 INT count = 0;
3117 HTREEITEM item = 0;
3118 HTREEITEM nextItem = 0;
3119 HTREEITEM prevItem = 0;
3121 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3123 if (sortList == NULL)
3124 return FALSE;
3126 /* let DPA sort the list */
3127 DPA_Sort(sortList, pfnCompare, lpCompare);
3129 /* The order of DPA entries has been changed, so fixup the
3130 * nextSibling and prevSibling pointers. */
3132 item = DPA_GetPtr(sortList, count++);
3133 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3135 /* link the two current item together */
3136 item->nextSibling = nextItem;
3137 nextItem->prevSibling = item;
3139 if (prevItem == NULL)
3141 /* this is the first item, update the parent */
3142 parent->firstChild = item;
3143 item->prevSibling = NULL;
3145 else
3147 /* fix the back chaining */
3148 item->prevSibling = prevItem;
3151 /* get ready for the next one */
3152 prevItem = item;
3153 item = nextItem;
3156 /* the last item is pointed to by item and never has a sibling */
3157 item->nextSibling = NULL;
3158 parent->lastChild = item;
3160 DPA_Destroy(sortList);
3162 TREEVIEW_VerifyTree(infoPtr);
3164 if (parent->state & TVIS_EXPANDED)
3166 int visOrder = infoPtr->firstVisible->visibleOrder;
3168 if (parent == infoPtr->root)
3169 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3170 else
3171 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3173 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3175 TREEVIEW_ITEM *item;
3177 for (item = infoPtr->root->firstChild; item != NULL;
3178 item = TREEVIEW_GetNextListItem(infoPtr, item))
3180 if (item->visibleOrder == visOrder)
3181 break;
3184 if (!item) item = parent->firstChild;
3185 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3188 TREEVIEW_Invalidate(infoPtr, NULL);
3191 return TRUE;
3193 return FALSE;
3197 /***************************************************************************
3198 * Setup the treeview structure with regards of the sort method
3199 * and sort the children of the TV item specified in lParam
3201 static LRESULT
3202 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3204 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3208 /***************************************************************************
3209 * Sort the children of the TV item specified in lParam.
3211 static LRESULT
3212 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3214 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3218 /* Expansion/Collapse ***************************************************/
3220 static BOOL
3221 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3222 UINT action)
3224 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3225 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3226 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3227 0, item);
3230 static VOID
3231 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3232 UINT action)
3234 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3235 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3236 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3237 0, item);
3241 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3242 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3243 static BOOL
3244 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3245 BOOL bRemoveChildren, BOOL bUser)
3247 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3248 BOOL bSetSelection, bSetFirstVisible;
3249 RECT scrollRect;
3250 LONG scrollDist = 0;
3251 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3252 BOOL wasExpanded;
3254 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3256 if (!TREEVIEW_HasChildren(infoPtr, item))
3257 return FALSE;
3259 if (bUser)
3260 TREEVIEW_SendExpanding(infoPtr, item, action);
3262 if (item->firstChild == NULL)
3263 return FALSE;
3265 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3266 item->state &= ~TVIS_EXPANDED;
3268 if (wasExpanded && bUser)
3269 TREEVIEW_SendExpanded(infoPtr, item, action);
3271 bSetSelection = (infoPtr->selectedItem != NULL
3272 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3274 bSetFirstVisible = (infoPtr->firstVisible != NULL
3275 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3277 tmpItem = item;
3278 while (tmpItem)
3280 if (tmpItem->nextSibling)
3282 nextItem = tmpItem->nextSibling;
3283 break;
3285 tmpItem = tmpItem->parent;
3288 if (nextItem)
3289 scrollDist = nextItem->rect.top;
3291 if (bRemoveChildren)
3293 INT old_cChildren = item->cChildren;
3294 TRACE("TVE_COLLAPSERESET\n");
3295 item->state &= ~TVIS_EXPANDEDONCE;
3296 TREEVIEW_RemoveAllChildren(infoPtr, item);
3297 item->cChildren = old_cChildren;
3299 if (!wasExpanded)
3300 return FALSE;
3302 if (item->firstChild)
3304 TREEVIEW_ITEM *i, *sibling;
3306 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3308 for (i = item->firstChild; i != sibling;
3309 i = TREEVIEW_GetNextListItem(infoPtr, i))
3311 i->visibleOrder = -1;
3315 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3317 if (nextItem)
3318 scrollDist = -(scrollDist - nextItem->rect.top);
3320 if (bSetSelection)
3322 /* Don't call DoSelectItem, it sends notifications. */
3323 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3324 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3325 item->state |= TVIS_SELECTED;
3326 infoPtr->selectedItem = item;
3329 TREEVIEW_UpdateScrollBars(infoPtr);
3331 scrollRect.left = 0;
3332 scrollRect.right = infoPtr->clientWidth;
3333 scrollRect.bottom = infoPtr->clientHeight;
3335 if (nextItem)
3337 scrollRect.top = nextItem->rect.top;
3339 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3340 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3341 TREEVIEW_Invalidate(infoPtr, item);
3342 } else {
3343 scrollRect.top = item->rect.top;
3344 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3347 TREEVIEW_SetFirstVisible(infoPtr,
3348 bSetFirstVisible ? item : infoPtr->firstVisible,
3349 TRUE);
3351 return wasExpanded;
3354 static BOOL
3355 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3356 BOOL partial, BOOL user)
3358 LONG scrollDist;
3359 LONG orgNextTop = 0;
3360 RECT scrollRect;
3361 TREEVIEW_ITEM *nextItem, *tmpItem;
3362 BOOL sendsNotifications;
3364 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, item, partial, user);
3366 if (!TREEVIEW_HasChildren(infoPtr, item))
3367 return FALSE;
3369 tmpItem = item; nextItem = NULL;
3370 while (tmpItem)
3372 if (tmpItem->nextSibling)
3374 nextItem = tmpItem->nextSibling;
3375 break;
3377 tmpItem = tmpItem->parent;
3380 if (nextItem)
3381 orgNextTop = nextItem->rect.top;
3383 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3385 sendsNotifications = user || ((item->cChildren != 0) &&
3386 !(item->state & TVIS_EXPANDEDONCE));
3387 if (sendsNotifications)
3389 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3391 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3392 return FALSE;
3395 if (!item->firstChild)
3396 return FALSE;
3398 item->state |= TVIS_EXPANDED;
3400 if (partial)
3401 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3403 if (ISVISIBLE(item))
3405 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3406 TREEVIEW_UpdateSubTree(infoPtr, item);
3407 TREEVIEW_UpdateScrollBars(infoPtr);
3409 scrollRect.left = 0;
3410 scrollRect.bottom = infoPtr->treeHeight;
3411 scrollRect.right = infoPtr->clientWidth;
3412 if (nextItem)
3414 scrollDist = nextItem->rect.top - orgNextTop;
3415 scrollRect.top = orgNextTop;
3417 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3418 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3419 TREEVIEW_Invalidate (infoPtr, item);
3420 } else {
3421 scrollRect.top = item->rect.top;
3422 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3425 /* Scroll up so that as many children as possible are visible.
3426 * This fails when expanding causes an HScroll bar to appear, but we
3427 * don't know that yet, so the last item is obscured. */
3428 if (item->firstChild != NULL)
3430 int nChildren = item->lastChild->visibleOrder
3431 - item->firstChild->visibleOrder + 1;
3433 int visible_pos = item->visibleOrder
3434 - infoPtr->firstVisible->visibleOrder;
3436 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3438 if (visible_pos > 0 && nChildren > rows_below)
3440 int scroll = nChildren - rows_below;
3442 if (scroll > visible_pos)
3443 scroll = visible_pos;
3445 if (scroll > 0)
3447 TREEVIEW_ITEM *newFirstVisible
3448 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3449 scroll);
3452 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3458 if (sendsNotifications) {
3459 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3460 item->state |= TVIS_EXPANDEDONCE;
3463 return TRUE;
3466 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3467 to mouse messages and TVM_SELECTITEM.
3469 selection - previously selected item, used to collapse a part of a tree
3470 item - new selected item
3472 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3473 HTREEITEM selection, HTREEITEM item)
3475 TREEVIEW_ITEM *SelItem;
3477 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3479 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3482 * Close the previous selection all the way to the root
3483 * as long as the new selection is not a child
3485 if(selection && (selection != item))
3487 BOOL closeit = TRUE;
3488 SelItem = item;
3490 /* determine if the hitItem is a child of the currently selected item */
3491 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3492 (SelItem->parent != infoPtr->root))
3494 closeit = (SelItem != selection);
3495 SelItem = SelItem->parent;
3498 if(closeit)
3500 if(TREEVIEW_ValidItem(infoPtr, selection))
3501 SelItem = selection;
3503 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3504 SelItem->parent != infoPtr->root)
3506 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3507 SelItem = SelItem->parent;
3513 * Expand the current item
3515 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3518 static BOOL
3519 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3521 TRACE("item=%p, user=%d\n", item, user);
3523 if (item->state & TVIS_EXPANDED)
3524 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3525 else
3526 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3529 static VOID
3530 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3532 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3534 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3536 if (TREEVIEW_HasChildren(infoPtr, item))
3537 TREEVIEW_ExpandAll(infoPtr, item);
3541 /* Note:If the specified item is the child of a collapsed parent item,
3542 the parent's list of child items is (recursively) expanded to reveal the
3543 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3544 know if it also applies here.
3547 static LRESULT
3548 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3550 if (!TREEVIEW_ValidItem(infoPtr, item))
3551 return 0;
3553 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3554 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3555 flag, item->state);
3557 switch (flag & TVE_TOGGLE)
3559 case TVE_COLLAPSE:
3560 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3561 FALSE);
3563 case TVE_EXPAND:
3564 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3565 FALSE);
3567 case TVE_TOGGLE:
3568 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3570 default:
3571 return 0;
3575 /* Hit-Testing **********************************************************/
3577 static TREEVIEW_ITEM *
3578 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3580 TREEVIEW_ITEM *item;
3581 LONG row;
3583 if (!infoPtr->firstVisible)
3584 return NULL;
3586 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3588 for (item = infoPtr->firstVisible; item != NULL;
3589 item = TREEVIEW_GetNextListItem(infoPtr, item))
3591 if (row >= item->visibleOrder
3592 && row < item->visibleOrder + item->iIntegral)
3593 break;
3596 return item;
3599 static LRESULT
3600 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3602 TREEVIEW_ITEM *item;
3603 RECT rect;
3604 UINT status;
3605 LONG x, y;
3607 lpht->hItem = 0;
3608 GetClientRect(infoPtr->hwnd, &rect);
3609 status = 0;
3610 x = lpht->pt.x;
3611 y = lpht->pt.y;
3613 if (x < rect.left)
3615 status |= TVHT_TOLEFT;
3617 else if (x > rect.right)
3619 status |= TVHT_TORIGHT;
3622 if (y < rect.top)
3624 status |= TVHT_ABOVE;
3626 else if (y > rect.bottom)
3628 status |= TVHT_BELOW;
3631 if (status)
3633 lpht->flags = status;
3634 return 0;
3637 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3638 if (!item)
3640 lpht->flags = TVHT_NOWHERE;
3641 return 0;
3644 if (x >= item->textOffset + item->textWidth)
3646 lpht->flags = TVHT_ONITEMRIGHT;
3648 else if (x >= item->textOffset)
3650 lpht->flags = TVHT_ONITEMLABEL;
3652 else if (x >= item->imageOffset)
3654 lpht->flags = TVHT_ONITEMICON;
3656 else if (x >= item->stateOffset)
3658 lpht->flags = TVHT_ONITEMSTATEICON;
3660 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3662 lpht->flags = TVHT_ONITEMBUTTON;
3664 else
3666 lpht->flags = TVHT_ONITEMINDENT;
3669 lpht->hItem = item;
3670 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3672 return (LRESULT)item;
3675 /* Item Label Editing ***************************************************/
3677 static LRESULT
3678 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3680 return (LRESULT)infoPtr->hwndEdit;
3683 static LRESULT CALLBACK
3684 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3686 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3687 BOOL bCancel = FALSE;
3688 LRESULT rc;
3690 switch (uMsg)
3692 case WM_PAINT:
3693 TRACE("WM_PAINT start\n");
3694 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3695 lParam);
3696 TRACE("WM_PAINT done\n");
3697 return rc;
3699 case WM_KILLFOCUS:
3700 if (infoPtr->bIgnoreEditKillFocus)
3701 return TRUE;
3702 break;
3704 case WM_DESTROY:
3706 WNDPROC editProc = infoPtr->wpEditOrig;
3707 infoPtr->wpEditOrig = 0;
3708 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3709 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3712 case WM_GETDLGCODE:
3713 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3715 case WM_KEYDOWN:
3716 if (wParam == VK_ESCAPE)
3718 bCancel = TRUE;
3719 break;
3721 else if (wParam == VK_RETURN)
3723 break;
3726 /* fall through */
3727 default:
3728 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3731 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3732 /* eg. Using a messagebox */
3734 infoPtr->bIgnoreEditKillFocus = TRUE;
3735 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3736 infoPtr->bIgnoreEditKillFocus = FALSE;
3738 return 0;
3742 /* should handle edit control messages here */
3744 static LRESULT
3745 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3747 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3749 switch (HIWORD(wParam))
3751 case EN_UPDATE:
3754 * Adjust the edit window size
3756 WCHAR buffer[1024];
3757 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3758 HDC hdc = GetDC(infoPtr->hwndEdit);
3759 SIZE sz;
3760 HFONT hFont, hOldFont = 0;
3762 TRACE("edit=%p\n", infoPtr->hwndEdit);
3764 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3766 infoPtr->bLabelChanged = TRUE;
3768 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3770 /* Select font to get the right dimension of the string */
3771 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3773 if (hFont != 0)
3775 hOldFont = SelectObject(hdc, hFont);
3778 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3780 TEXTMETRICW textMetric;
3782 /* Add Extra spacing for the next character */
3783 GetTextMetricsW(hdc, &textMetric);
3784 sz.cx += (textMetric.tmMaxCharWidth * 2);
3786 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3787 sz.cx = min(sz.cx,
3788 infoPtr->clientWidth - editItem->textOffset + 2);
3790 SetWindowPos(infoPtr->hwndEdit,
3791 HWND_TOP,
3794 sz.cx,
3795 editItem->rect.bottom - editItem->rect.top + 3,
3796 SWP_NOMOVE | SWP_DRAWFRAME);
3799 if (hFont != 0)
3801 SelectObject(hdc, hOldFont);
3804 ReleaseDC(infoPtr->hwnd, hdc);
3805 break;
3807 case EN_KILLFOCUS:
3808 /* apparently we should respect passed handle value */
3809 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3811 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3812 break;
3814 default:
3815 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3818 return 0;
3821 static HWND
3822 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3824 HWND hwnd = infoPtr->hwnd;
3825 HWND hwndEdit;
3826 SIZE sz;
3827 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3828 HDC hdc;
3829 HFONT hOldFont=0;
3830 TEXTMETRICW textMetric;
3832 TRACE("%p %p\n", hwnd, hItem);
3833 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3834 return NULL;
3836 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3837 return NULL;
3839 if (infoPtr->hwndEdit)
3840 return infoPtr->hwndEdit;
3842 infoPtr->bLabelChanged = FALSE;
3844 /* make edit item visible */
3845 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3847 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3849 hdc = GetDC(hwnd);
3850 /* Select the font to get appropriate metric dimensions */
3851 if (infoPtr->hFont != 0)
3853 hOldFont = SelectObject(hdc, infoPtr->hFont);
3856 /* Get string length in pixels */
3857 if (hItem->pszText)
3858 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3859 &sz);
3860 else
3861 GetTextExtentPoint32A(hdc, "", 0, &sz);
3863 /* Add Extra spacing for the next character */
3864 GetTextMetricsW(hdc, &textMetric);
3865 sz.cx += (textMetric.tmMaxCharWidth * 2);
3867 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3868 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3870 if (infoPtr->hFont != 0)
3872 SelectObject(hdc, hOldFont);
3875 ReleaseDC(hwnd, hdc);
3877 infoPtr->editItem = hItem;
3879 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3880 WC_EDITW,
3882 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3883 WS_CLIPSIBLINGS | ES_WANTRETURN |
3884 ES_LEFT, hItem->textOffset - 2,
3885 hItem->rect.top - 1, sz.cx + 3,
3886 hItem->rect.bottom -
3887 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3888 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3890 infoPtr->hwndEdit = hwndEdit;
3892 /* Get a 2D border. */
3893 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3894 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3895 SetWindowLongW(hwndEdit, GWL_STYLE,
3896 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3898 SendMessageW(hwndEdit, WM_SETFONT,
3899 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3901 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3902 (DWORD_PTR)
3903 TREEVIEW_Edit_SubclassProc);
3904 if (hItem->pszText)
3905 SetWindowTextW(hwndEdit, hItem->pszText);
3907 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3909 DestroyWindow(hwndEdit);
3910 infoPtr->hwndEdit = 0;
3911 infoPtr->editItem = NULL;
3912 return NULL;
3915 SetFocus(hwndEdit);
3916 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3917 ShowWindow(hwndEdit, SW_SHOW);
3919 return hwndEdit;
3923 static LRESULT
3924 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3926 HWND hwnd = infoPtr->hwnd;
3927 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3928 NMTVDISPINFOW tvdi;
3929 BOOL bCommit;
3930 WCHAR tmpText[1024] = { '\0' };
3931 WCHAR *newText = tmpText;
3932 int iLength = 0;
3934 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3936 tvdi.hdr.hwndFrom = hwnd;
3937 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3938 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3939 tvdi.item.mask = 0;
3940 tvdi.item.hItem = editedItem;
3941 tvdi.item.state = editedItem->state;
3942 tvdi.item.lParam = editedItem->lParam;
3944 if (!bCancel)
3946 if (!infoPtr->bNtfUnicode)
3947 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3948 else
3949 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3951 if (iLength >= 1023)
3953 ERR("Insufficient space to retrieve new item label\n");
3956 tvdi.item.mask = TVIF_TEXT;
3957 tvdi.item.pszText = tmpText;
3958 tvdi.item.cchTextMax = iLength + 1;
3960 else
3962 tvdi.item.pszText = NULL;
3963 tvdi.item.cchTextMax = 0;
3966 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
3968 if (!bCancel && bCommit) /* Apply the changes */
3970 if (!infoPtr->bNtfUnicode)
3972 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3973 newText = Alloc(len * sizeof(WCHAR));
3974 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3975 iLength = len - 1;
3978 if (strcmpW(newText, editedItem->pszText) != 0)
3980 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3981 if (ptr == NULL)
3983 ERR("OutOfMemory, cannot allocate space for label\n");
3984 if(newText != tmpText) Free(newText);
3985 DestroyWindow(infoPtr->hwndEdit);
3986 infoPtr->hwndEdit = 0;
3987 infoPtr->editItem = NULL;
3988 return FALSE;
3990 else
3992 editedItem->pszText = ptr;
3993 editedItem->cchTextMax = iLength + 1;
3994 strcpyW(editedItem->pszText, newText);
3995 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3998 if(newText != tmpText) Free(newText);
4001 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4002 DestroyWindow(infoPtr->hwndEdit);
4003 infoPtr->hwndEdit = 0;
4004 infoPtr->editItem = NULL;
4005 return TRUE;
4008 static LRESULT
4009 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4011 if (wParam != TV_EDIT_TIMER)
4013 ERR("got unknown timer\n");
4014 return 1;
4017 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4018 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4020 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4022 return 0;
4026 /* Mouse Tracking/Drag **************************************************/
4028 /***************************************************************************
4029 * This is quite unusual piece of code, but that's how it's implemented in
4030 * Windows.
4032 static LRESULT
4033 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4035 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4036 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4037 RECT r;
4038 MSG msg;
4040 r.top = pt.y - cyDrag;
4041 r.left = pt.x - cxDrag;
4042 r.bottom = pt.y + cyDrag;
4043 r.right = pt.x + cxDrag;
4045 SetCapture(infoPtr->hwnd);
4047 while (1)
4049 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4051 if (msg.message == WM_MOUSEMOVE)
4053 pt.x = (short)LOWORD(msg.lParam);
4054 pt.y = (short)HIWORD(msg.lParam);
4055 if (PtInRect(&r, pt))
4056 continue;
4057 else
4059 ReleaseCapture();
4060 return 1;
4063 else if (msg.message >= WM_LBUTTONDOWN &&
4064 msg.message <= WM_RBUTTONDBLCLK)
4066 break;
4069 DispatchMessageW(&msg);
4072 if (GetCapture() != infoPtr->hwnd)
4073 return 0;
4076 ReleaseCapture();
4077 return 0;
4081 static LRESULT
4082 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4084 TREEVIEW_ITEM *item;
4085 TVHITTESTINFO hit;
4087 TRACE("\n");
4088 SetFocus(infoPtr->hwnd);
4090 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4092 /* If there is pending 'edit label' event - kill it now */
4093 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4096 hit.pt.x = (short)LOWORD(lParam);
4097 hit.pt.y = (short)HIWORD(lParam);
4099 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4100 if (!item)
4101 return 0;
4102 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4104 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4105 { /* FIXME! */
4106 switch (hit.flags)
4108 case TVHT_ONITEMRIGHT:
4109 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4110 break;
4112 case TVHT_ONITEMINDENT:
4113 if (!(infoPtr->dwStyle & TVS_HASLINES))
4115 break;
4117 else
4119 int level = hit.pt.x / infoPtr->uIndent;
4120 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4122 while (item->iLevel > level)
4124 item = item->parent;
4127 /* fall through */
4130 case TVHT_ONITEMLABEL:
4131 case TVHT_ONITEMICON:
4132 case TVHT_ONITEMBUTTON:
4133 TREEVIEW_Toggle(infoPtr, item, TRUE);
4134 break;
4136 case TVHT_ONITEMSTATEICON:
4137 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4138 TREEVIEW_ToggleItemState(infoPtr, item);
4139 else
4140 TREEVIEW_Toggle(infoPtr, item, TRUE);
4141 break;
4144 return TRUE;
4148 static LRESULT
4149 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4151 HWND hwnd = infoPtr->hwnd;
4152 TVHITTESTINFO ht;
4153 BOOL bTrack, bDoLabelEdit;
4155 /* If Edit control is active - kill it and return.
4156 * The best way to do it is to set focus to itself.
4157 * Edit control subclassed procedure will automatically call
4158 * EndEditLabelNow.
4160 if (infoPtr->hwndEdit)
4162 SetFocus(hwnd);
4163 return 0;
4166 ht.pt.x = (short)LOWORD(lParam);
4167 ht.pt.y = (short)HIWORD(lParam);
4169 TREEVIEW_HitTest(infoPtr, &ht);
4170 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4172 /* update focusedItem and redraw both items */
4173 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4175 infoPtr->focusedItem = ht.hItem;
4176 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4177 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4180 bTrack = (ht.flags & TVHT_ONITEM)
4181 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4184 * If the style allows editing and the node is already selected
4185 * and the click occurred on the item label...
4187 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4188 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4190 /* Send NM_CLICK right away */
4191 if (!bTrack)
4192 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4193 goto setfocus;
4195 if (ht.flags & TVHT_ONITEMBUTTON)
4197 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4198 goto setfocus;
4200 else if (bTrack)
4201 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4202 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4204 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4205 infoPtr->dropItem = ht.hItem;
4207 /* clean up focusedItem as we dragged and won't select this item */
4208 if(infoPtr->focusedItem)
4210 /* refresh the item that was focused */
4211 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4212 infoPtr->focusedItem = NULL;
4214 /* refresh the selected item to return the filled background */
4215 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4218 return 0;
4222 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4223 goto setfocus;
4225 if (bDoLabelEdit)
4227 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4228 KillTimer(hwnd, TV_EDIT_TIMER);
4230 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4231 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4233 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4235 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4237 /* Select the current item */
4238 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4239 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4241 else if (ht.flags & TVHT_ONITEMSTATEICON)
4243 /* TVS_CHECKBOXES requires us to toggle the current state */
4244 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4245 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4248 setfocus:
4249 SetFocus(hwnd);
4250 return 0;
4254 static LRESULT
4255 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4257 TVHITTESTINFO ht;
4259 if (infoPtr->hwndEdit)
4261 SetFocus(infoPtr->hwnd);
4262 return 0;
4265 ht.pt.x = (short)LOWORD(lParam);
4266 ht.pt.y = (short)HIWORD(lParam);
4268 TREEVIEW_HitTest(infoPtr, &ht);
4270 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4272 if (ht.hItem)
4274 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4275 infoPtr->dropItem = ht.hItem;
4278 else
4280 SetFocus(infoPtr->hwnd);
4281 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4283 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4284 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4285 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4289 return 0;
4292 static LRESULT
4293 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4295 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4296 INT cx, cy;
4297 HDC hdc, htopdc;
4298 HWND hwtop;
4299 HBITMAP hbmp, hOldbmp;
4300 SIZE size;
4301 RECT rc;
4302 HFONT hOldFont;
4304 TRACE("\n");
4306 if (!(infoPtr->himlNormal))
4307 return 0;
4309 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4310 return 0;
4312 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4314 hwtop = GetDesktopWindow();
4315 htopdc = GetDC(hwtop);
4316 hdc = CreateCompatibleDC(htopdc);
4318 hOldFont = SelectObject(hdc, infoPtr->hFont);
4320 if (dragItem->pszText)
4321 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4322 &size);
4323 else
4324 GetTextExtentPoint32A(hdc, "", 0, &size);
4326 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4327 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4328 hOldbmp = SelectObject(hdc, hbmp);
4330 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4331 size.cx += cx;
4332 if (cy > size.cy)
4333 size.cy = cy;
4335 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4336 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4337 ILD_NORMAL);
4340 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4341 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4344 /* draw item text */
4346 SetRect(&rc, cx, 0, size.cx, size.cy);
4348 if (dragItem->pszText)
4349 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4350 DT_LEFT);
4352 SelectObject(hdc, hOldFont);
4353 SelectObject(hdc, hOldbmp);
4355 ImageList_Add(infoPtr->dragList, hbmp, 0);
4357 DeleteDC(hdc);
4358 DeleteObject(hbmp);
4359 ReleaseDC(hwtop, htopdc);
4361 return (LRESULT)infoPtr->dragList;
4364 /* Selection ************************************************************/
4366 static LRESULT
4367 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4368 INT cause)
4370 TREEVIEW_ITEM *prevSelect;
4372 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4374 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4375 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4376 newSelect ? newSelect->state : 0);
4378 /* reset and redraw focusedItem if focusedItem was set so we don't */
4379 /* have to worry about the previously focused item when we set a new one */
4380 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4381 infoPtr->focusedItem = NULL;
4383 switch (action)
4385 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4386 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4387 /* Fall through */
4388 case TVGN_CARET:
4389 prevSelect = infoPtr->selectedItem;
4391 if (prevSelect == newSelect) {
4392 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4393 break;
4396 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4397 TVN_SELCHANGINGW,
4398 cause,
4399 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4400 prevSelect,
4401 newSelect))
4402 return FALSE;
4404 if (prevSelect)
4405 prevSelect->state &= ~TVIS_SELECTED;
4406 if (newSelect)
4407 newSelect->state |= TVIS_SELECTED;
4409 infoPtr->selectedItem = newSelect;
4411 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4413 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4414 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4416 TREEVIEW_SendTreeviewNotify(infoPtr,
4417 TVN_SELCHANGEDW,
4418 cause,
4419 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4420 prevSelect,
4421 newSelect);
4422 break;
4424 case TVGN_DROPHILITE:
4425 prevSelect = infoPtr->dropItem;
4427 if (prevSelect)
4428 prevSelect->state &= ~TVIS_DROPHILITED;
4430 infoPtr->dropItem = newSelect;
4432 if (newSelect)
4433 newSelect->state |= TVIS_DROPHILITED;
4435 TREEVIEW_Invalidate(infoPtr, prevSelect);
4436 TREEVIEW_Invalidate(infoPtr, newSelect);
4437 break;
4439 case TVGN_FIRSTVISIBLE:
4440 if (newSelect != NULL)
4442 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4443 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4444 TREEVIEW_Invalidate(infoPtr, NULL);
4446 break;
4449 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4450 return TRUE;
4453 /* FIXME: handle NM_KILLFOCUS etc */
4454 static LRESULT
4455 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4457 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4459 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4460 return FALSE;
4462 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4464 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4465 return FALSE;
4467 TREEVIEW_SingleExpand(infoPtr, selection, item);
4469 return TRUE;
4472 /*************************************************************************
4473 * TREEVIEW_ProcessLetterKeys
4475 * Processes keyboard messages generated by pressing the letter keys
4476 * on the keyboard.
4477 * What this does is perform a case insensitive search from the
4478 * current position with the following quirks:
4479 * - If two chars or more are pressed in quick succession we search
4480 * for the corresponding string (e.g. 'abc').
4481 * - If there is a delay we wipe away the current search string and
4482 * restart with just that char.
4483 * - If the user keeps pressing the same character, whether slowly or
4484 * fast, so that the search string is entirely composed of this
4485 * character ('aaaaa' for instance), then we search for first item
4486 * that starting with that character.
4487 * - If the user types the above character in quick succession, then
4488 * we must also search for the corresponding string ('aaaaa'), and
4489 * go to that string if there is a match.
4491 * RETURNS
4493 * Zero.
4495 * BUGS
4497 * - The current implementation has a list of characters it will
4498 * accept and it ignores everything else. In particular it will
4499 * ignore accentuated characters which seems to match what
4500 * Windows does. But I'm not sure it makes sense to follow
4501 * Windows there.
4502 * - We don't sound a beep when the search fails.
4503 * - The search should start from the focused item, not from the selected
4504 * item. One reason for this is to allow for multiple selections in trees.
4505 * But currently infoPtr->focusedItem does not seem very usable.
4507 * SEE ALSO
4509 * TREEVIEW_ProcessLetterKeys
4511 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4513 HTREEITEM nItem;
4514 HTREEITEM endidx,idx;
4515 TVITEMEXW item;
4516 WCHAR buffer[MAX_PATH];
4517 DWORD timestamp,elapsed;
4519 /* simple parameter checking */
4520 if (!charCode || !keyData) return 0;
4522 /* only allow the valid WM_CHARs through */
4523 if (!isalnum(charCode) &&
4524 charCode != '.' && charCode != '`' && charCode != '!' &&
4525 charCode != '@' && charCode != '#' && charCode != '$' &&
4526 charCode != '%' && charCode != '^' && charCode != '&' &&
4527 charCode != '*' && charCode != '(' && charCode != ')' &&
4528 charCode != '-' && charCode != '_' && charCode != '+' &&
4529 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4530 charCode != '}' && charCode != '[' && charCode != '{' &&
4531 charCode != '/' && charCode != '?' && charCode != '>' &&
4532 charCode != '<' && charCode != ',' && charCode != '~')
4533 return 0;
4535 /* compute how much time elapsed since last keypress */
4536 timestamp = GetTickCount();
4537 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4538 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4539 } else {
4540 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4543 /* update the search parameters */
4544 infoPtr->lastKeyPressTimestamp=timestamp;
4545 if (elapsed < KEY_DELAY) {
4546 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4547 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4549 if (infoPtr->charCode != charCode) {
4550 infoPtr->charCode=charCode=0;
4552 } else {
4553 infoPtr->charCode=charCode;
4554 infoPtr->szSearchParam[0]=charCode;
4555 infoPtr->nSearchParamLength=1;
4556 /* Redundant with the 1 char string */
4557 charCode=0;
4560 /* and search from the current position */
4561 nItem=NULL;
4562 if (infoPtr->selectedItem != NULL) {
4563 endidx=infoPtr->selectedItem;
4564 /* if looking for single character match,
4565 * then we must always move forward
4567 if (infoPtr->nSearchParamLength == 1)
4568 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4569 else
4570 idx=endidx;
4571 } else {
4572 endidx=NULL;
4573 idx=infoPtr->root->firstChild;
4575 do {
4576 /* At the end point, sort out wrapping */
4577 if (idx == NULL) {
4579 /* If endidx is null, stop at the last item (ie top to bottom) */
4580 if (endidx == NULL)
4581 break;
4583 /* Otherwise, start again at the very beginning */
4584 idx=infoPtr->root->firstChild;
4586 /* But if we are stopping on the first child, end now! */
4587 if (idx == endidx) break;
4590 /* get item */
4591 ZeroMemory(&item, sizeof(item));
4592 item.mask = TVIF_TEXT;
4593 item.hItem = idx;
4594 item.pszText = buffer;
4595 item.cchTextMax = sizeof(buffer);
4596 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4598 /* check for a match */
4599 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4600 nItem=idx;
4601 break;
4602 } else if ( (charCode != 0) && (nItem == NULL) &&
4603 (nItem != infoPtr->selectedItem) &&
4604 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4605 /* This would work but we must keep looking for a longer match */
4606 nItem=idx;
4608 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4609 } while (idx != endidx);
4611 if (nItem != NULL) {
4612 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4613 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4617 return 0;
4620 /* Scrolling ************************************************************/
4622 static LRESULT
4623 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4625 int viscount;
4626 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4627 HTREEITEM newFirstVisible = NULL;
4628 int visible_pos = -1;
4630 if (!TREEVIEW_ValidItem(infoPtr, item))
4631 return FALSE;
4633 if (!ISVISIBLE(item))
4635 /* Expand parents as necessary. */
4636 HTREEITEM parent;
4638 /* see if we are trying to ensure that root is visible */
4639 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4640 parent = item->parent;
4641 else
4642 parent = item; /* this item is the topmost item */
4644 while (parent != infoPtr->root)
4646 if (!(parent->state & TVIS_EXPANDED))
4647 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4649 parent = parent->parent;
4653 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4655 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4656 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4658 if (hasFirstVisible)
4659 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4661 if (visible_pos < 0)
4663 /* item is before the start of the list: put it at the top. */
4664 newFirstVisible = item;
4666 else if (visible_pos >= viscount
4667 /* Sometimes, before we are displayed, GVC is 0, causing us to
4668 * spuriously scroll up. */
4669 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4671 /* item is past the end of the list. */
4672 int scroll = visible_pos - viscount;
4674 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4675 scroll + 1);
4678 if (bHScroll)
4680 /* Scroll window so item's text is visible as much as possible */
4681 /* Calculation of amount of extra space is taken from EditLabel code */
4682 INT pos, x;
4683 TEXTMETRICW textMetric;
4684 HDC hdc = GetWindowDC(infoPtr->hwnd);
4686 x = item->textWidth;
4688 GetTextMetricsW(hdc, &textMetric);
4689 ReleaseDC(infoPtr->hwnd, hdc);
4691 x += (textMetric.tmMaxCharWidth * 2);
4692 x = max(x, textMetric.tmMaxCharWidth * 3);
4694 if (item->textOffset < 0)
4695 pos = item->textOffset;
4696 else if (item->textOffset + x > infoPtr->clientWidth)
4698 if (x > infoPtr->clientWidth)
4699 pos = item->textOffset;
4700 else
4701 pos = item->textOffset + x - infoPtr->clientWidth;
4703 else
4704 pos = 0;
4706 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4709 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4711 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4713 return TRUE;
4716 return FALSE;
4719 static VOID
4720 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4721 TREEVIEW_ITEM *newFirstVisible,
4722 BOOL bUpdateScrollPos)
4724 int gap_size;
4726 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4728 if (newFirstVisible != NULL)
4730 /* Prevent an empty gap from appearing at the bottom... */
4731 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4732 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4734 if (gap_size > 0)
4736 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4737 -gap_size);
4739 /* ... unless we just don't have enough items. */
4740 if (newFirstVisible == NULL)
4741 newFirstVisible = infoPtr->root->firstChild;
4745 if (infoPtr->firstVisible != newFirstVisible)
4747 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4749 infoPtr->firstVisible = newFirstVisible;
4750 TREEVIEW_Invalidate(infoPtr, NULL);
4752 else
4754 TREEVIEW_ITEM *item;
4755 int scroll = infoPtr->uItemHeight *
4756 (infoPtr->firstVisible->visibleOrder
4757 - newFirstVisible->visibleOrder);
4759 infoPtr->firstVisible = newFirstVisible;
4761 for (item = infoPtr->root->firstChild; item != NULL;
4762 item = TREEVIEW_GetNextListItem(infoPtr, item))
4764 item->rect.top += scroll;
4765 item->rect.bottom += scroll;
4768 if (bUpdateScrollPos)
4769 SetScrollPos(infoPtr->hwnd, SB_VERT,
4770 newFirstVisible->visibleOrder, TRUE);
4772 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4777 /************************************************************************
4778 * VScroll is always in units of visible items. i.e. we always have a
4779 * visible item aligned to the top of the control. (Unless we have no
4780 * items at all.)
4782 static LRESULT
4783 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4785 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4786 TREEVIEW_ITEM *newFirstVisible = NULL;
4788 int nScrollCode = LOWORD(wParam);
4790 TRACE("wp %lx\n", wParam);
4792 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4793 return 0;
4795 if (!oldFirstVisible)
4797 assert(infoPtr->root->firstChild == NULL);
4798 return 0;
4801 switch (nScrollCode)
4803 case SB_TOP:
4804 newFirstVisible = infoPtr->root->firstChild;
4805 break;
4807 case SB_BOTTOM:
4808 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4809 break;
4811 case SB_LINEUP:
4812 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4813 break;
4815 case SB_LINEDOWN:
4816 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4817 break;
4819 case SB_PAGEUP:
4820 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4821 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4822 break;
4824 case SB_PAGEDOWN:
4825 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4826 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4827 break;
4829 case SB_THUMBTRACK:
4830 case SB_THUMBPOSITION:
4831 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4832 infoPtr->root->firstChild,
4833 (LONG)(SHORT)HIWORD(wParam));
4834 break;
4836 case SB_ENDSCROLL:
4837 return 0;
4840 if (newFirstVisible != NULL)
4842 if (newFirstVisible != oldFirstVisible)
4843 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4844 nScrollCode != SB_THUMBTRACK);
4845 else if (nScrollCode == SB_THUMBPOSITION)
4846 SetScrollPos(infoPtr->hwnd, SB_VERT,
4847 newFirstVisible->visibleOrder, TRUE);
4850 return 0;
4853 static LRESULT
4854 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4856 int maxWidth;
4857 int scrollX = infoPtr->scrollX;
4858 int nScrollCode = LOWORD(wParam);
4860 TRACE("wp %lx\n", wParam);
4862 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4863 return FALSE;
4865 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4866 /* shall never occur */
4867 if (maxWidth <= 0)
4869 scrollX = 0;
4870 goto scroll;
4873 switch (nScrollCode)
4875 case SB_LINELEFT:
4876 scrollX -= infoPtr->uItemHeight;
4877 break;
4878 case SB_LINERIGHT:
4879 scrollX += infoPtr->uItemHeight;
4880 break;
4881 case SB_PAGELEFT:
4882 scrollX -= infoPtr->clientWidth;
4883 break;
4884 case SB_PAGERIGHT:
4885 scrollX += infoPtr->clientWidth;
4886 break;
4888 case SB_THUMBTRACK:
4889 case SB_THUMBPOSITION:
4890 scrollX = (int)(SHORT)HIWORD(wParam);
4891 break;
4893 case SB_ENDSCROLL:
4894 return 0;
4897 if (scrollX > maxWidth)
4898 scrollX = maxWidth;
4899 else if (scrollX < 0)
4900 scrollX = 0;
4902 scroll:
4903 if (scrollX != infoPtr->scrollX)
4905 TREEVIEW_ITEM *item;
4906 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4908 for (item = infoPtr->root->firstChild; item != NULL;
4909 item = TREEVIEW_GetNextListItem(infoPtr, item))
4911 item->linesOffset += scroll_pixels;
4912 item->stateOffset += scroll_pixels;
4913 item->imageOffset += scroll_pixels;
4914 item->textOffset += scroll_pixels;
4917 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4918 infoPtr->scrollX = scrollX;
4919 UpdateWindow(infoPtr->hwnd);
4922 if (nScrollCode != SB_THUMBTRACK)
4923 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4925 return 0;
4928 static LRESULT
4929 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4931 short wheelDelta;
4932 UINT pulScrollLines = 3;
4934 if (wParam & (MK_SHIFT | MK_CONTROL))
4935 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4937 if (infoPtr->firstVisible == NULL)
4938 return TRUE;
4940 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4942 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4943 /* if scrolling changes direction, ignore left overs */
4944 if ((wheelDelta < 0 && infoPtr->wheelRemainder < 0) ||
4945 (wheelDelta > 0 && infoPtr->wheelRemainder > 0))
4946 infoPtr->wheelRemainder += wheelDelta;
4947 else
4948 infoPtr->wheelRemainder = wheelDelta;
4950 if (infoPtr->wheelRemainder && pulScrollLines)
4952 int newDy;
4953 int maxDy;
4954 int lineScroll;
4956 lineScroll = pulScrollLines * (float)infoPtr->wheelRemainder / WHEEL_DELTA;
4957 infoPtr->wheelRemainder -= WHEEL_DELTA * lineScroll / (int)pulScrollLines;
4959 newDy = infoPtr->firstVisible->visibleOrder - lineScroll;
4960 maxDy = infoPtr->maxVisibleOrder;
4962 if (newDy > maxDy)
4963 newDy = maxDy;
4965 if (newDy < 0)
4966 newDy = 0;
4968 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4970 return TRUE;
4973 /* Create/Destroy *******************************************************/
4975 static void
4976 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
4978 RECT rc;
4979 HBITMAP hbm, hbmOld;
4980 HDC hdc, hdcScreen;
4981 int nIndex;
4983 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4985 hdcScreen = GetDC(0);
4987 hdc = CreateCompatibleDC(hdcScreen);
4988 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4989 hbmOld = SelectObject(hdc, hbm);
4991 SetRect(&rc, 0, 0, 48, 16);
4992 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4994 SetRect(&rc, 18, 2, 30, 14);
4995 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4996 DFCS_BUTTONCHECK|DFCS_FLAT);
4998 SetRect(&rc, 34, 2, 46, 14);
4999 DrawFrameControl(hdc, &rc, DFC_BUTTON,
5000 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
5002 SelectObject(hdc, hbmOld);
5003 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
5004 comctl32_color.clrWindow);
5005 TRACE("checkbox index %d\n", nIndex);
5007 DeleteObject(hbm);
5008 DeleteDC(hdc);
5009 ReleaseDC(0, hdcScreen);
5011 infoPtr->stateImageWidth = 16;
5012 infoPtr->stateImageHeight = 16;
5015 static LRESULT
5016 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
5018 RECT rcClient;
5019 TREEVIEW_INFO *infoPtr;
5020 LOGFONTW lf;
5022 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5024 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
5026 if (infoPtr == NULL)
5028 ERR("could not allocate info memory!\n");
5029 return 0;
5032 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5034 infoPtr->hwnd = hwnd;
5035 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5036 infoPtr->Timer = 0;
5037 infoPtr->uNumItems = 0;
5038 infoPtr->cdmode = 0;
5039 infoPtr->uScrollTime = 300; /* milliseconds */
5040 infoPtr->bRedraw = TRUE;
5042 GetClientRect(hwnd, &rcClient);
5044 /* No scroll bars yet. */
5045 infoPtr->clientWidth = rcClient.right;
5046 infoPtr->clientHeight = rcClient.bottom;
5047 infoPtr->uInternalStatus = 0;
5049 infoPtr->treeWidth = 0;
5050 infoPtr->treeHeight = 0;
5052 infoPtr->uIndent = MINIMUM_INDENT;
5053 infoPtr->selectedItem = NULL;
5054 infoPtr->focusedItem = NULL;
5055 infoPtr->hotItem = NULL;
5056 infoPtr->editItem = NULL;
5057 infoPtr->firstVisible = NULL;
5058 infoPtr->maxVisibleOrder = 0;
5059 infoPtr->dropItem = NULL;
5060 infoPtr->insertMarkItem = NULL;
5061 infoPtr->insertBeforeorAfter = 0;
5062 /* dragList */
5064 infoPtr->scrollX = 0;
5065 infoPtr->wheelRemainder = 0;
5067 infoPtr->clrBk = CLR_NONE; /* use system color */
5068 infoPtr->clrText = CLR_NONE; /* use system color */
5069 infoPtr->clrLine = CLR_DEFAULT;
5070 infoPtr->clrInsertMark = CLR_DEFAULT;
5072 /* hwndToolTip */
5074 infoPtr->hwndEdit = NULL;
5075 infoPtr->wpEditOrig = NULL;
5076 infoPtr->bIgnoreEditKillFocus = FALSE;
5077 infoPtr->bLabelChanged = FALSE;
5079 infoPtr->himlNormal = NULL;
5080 infoPtr->himlState = NULL;
5081 infoPtr->normalImageWidth = 0;
5082 infoPtr->normalImageHeight = 0;
5083 infoPtr->stateImageWidth = 0;
5084 infoPtr->stateImageHeight = 0;
5086 infoPtr->items = DPA_Create(16);
5088 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5089 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5090 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5091 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5092 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
5093 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5095 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5097 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5098 infoPtr->root->state = TVIS_EXPANDED;
5099 infoPtr->root->iLevel = -1;
5100 infoPtr->root->visibleOrder = -1;
5102 infoPtr->hwndNotify = lpcs->hwndParent;
5103 infoPtr->hwndToolTip = 0;
5105 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5106 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5108 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5109 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5110 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5111 hwnd, 0, 0, 0);
5113 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5114 TREEVIEW_InitCheckboxes(infoPtr);
5116 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5117 ShowScrollBar(hwnd, SB_VERT, FALSE);
5118 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5120 OpenThemeData (hwnd, themeClass);
5122 return 0;
5126 static LRESULT
5127 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5129 TRACE("\n");
5131 /* free item data */
5132 TREEVIEW_RemoveTree(infoPtr);
5133 /* root isn't freed with other items */
5134 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5135 DPA_Destroy(infoPtr->items);
5137 /* tool tip is automatically destroyed: we are its owner */
5139 /* Restore original wndproc */
5140 if (infoPtr->hwndEdit)
5141 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5142 (DWORD_PTR)infoPtr->wpEditOrig);
5144 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5146 /* Deassociate treeview from the window before doing anything drastic. */
5147 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5149 DeleteObject(infoPtr->hDefaultFont);
5150 DeleteObject(infoPtr->hBoldFont);
5151 DeleteObject(infoPtr->hUnderlineFont);
5152 DeleteObject(infoPtr->hBoldUnderlineFont);
5153 Free(infoPtr);
5155 return 0;
5158 /* Miscellaneous Messages ***********************************************/
5160 static LRESULT
5161 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5163 static const struct
5165 unsigned char code;
5167 scroll[] =
5169 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5170 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5171 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5172 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5173 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5174 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5175 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5176 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5177 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5178 #undef SCROLL_ENTRY
5181 if (key >= VK_PRIOR && key <= VK_DOWN)
5183 unsigned char code = scroll[key - VK_PRIOR].code;
5185 (((code & (1 << 7)) == (SB_HORZ << 7))
5186 ? TREEVIEW_HScroll
5187 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5190 return 0;
5193 /************************************************************************
5194 * TREEVIEW_KeyDown
5196 * VK_UP Move selection to the previous non-hidden item.
5197 * VK_DOWN Move selection to the next non-hidden item.
5198 * VK_HOME Move selection to the first item.
5199 * VK_END Move selection to the last item.
5200 * VK_LEFT If expanded then collapse, otherwise move to parent.
5201 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5202 * VK_ADD Expand.
5203 * VK_SUBTRACT Collapse.
5204 * VK_MULTIPLY Expand all.
5205 * VK_PRIOR Move up GetVisibleCount items.
5206 * VK_NEXT Move down GetVisibleCount items.
5207 * VK_BACK Move to parent.
5208 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5210 static LRESULT
5211 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5213 /* If it is non-NULL and different, it will be selected and visible. */
5214 TREEVIEW_ITEM *newSelection = NULL;
5216 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5218 TRACE("%lx\n", wParam);
5220 if (prevItem == NULL)
5221 return FALSE;
5223 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5224 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5226 switch (wParam)
5228 case VK_UP:
5229 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5230 if (!newSelection)
5231 newSelection = infoPtr->root->firstChild;
5232 break;
5234 case VK_DOWN:
5235 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5236 break;
5238 case VK_HOME:
5239 newSelection = infoPtr->root->firstChild;
5240 break;
5242 case VK_END:
5243 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5244 break;
5246 case VK_LEFT:
5247 if (prevItem->state & TVIS_EXPANDED)
5249 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5251 else if (prevItem->parent != infoPtr->root)
5253 newSelection = prevItem->parent;
5255 break;
5257 case VK_RIGHT:
5258 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5260 if (!(prevItem->state & TVIS_EXPANDED))
5261 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5262 else
5264 newSelection = prevItem->firstChild;
5268 break;
5270 case VK_MULTIPLY:
5271 TREEVIEW_ExpandAll(infoPtr, prevItem);
5272 break;
5274 case VK_ADD:
5275 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5276 break;
5278 case VK_SUBTRACT:
5279 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5280 break;
5282 case VK_PRIOR:
5283 newSelection
5284 = TREEVIEW_GetListItem(infoPtr, prevItem,
5285 -TREEVIEW_GetVisibleCount(infoPtr));
5286 break;
5288 case VK_NEXT:
5289 newSelection
5290 = TREEVIEW_GetListItem(infoPtr, prevItem,
5291 TREEVIEW_GetVisibleCount(infoPtr));
5292 break;
5294 case VK_BACK:
5295 newSelection = prevItem->parent;
5296 if (newSelection == infoPtr->root)
5297 newSelection = NULL;
5298 break;
5300 case VK_SPACE:
5301 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5302 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5303 break;
5306 if (newSelection && newSelection != prevItem)
5308 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5309 TVC_BYKEYBOARD))
5311 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5315 return FALSE;
5318 static LRESULT
5319 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5321 /* remove hot effect from item */
5322 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5323 infoPtr->hotItem = NULL;
5325 return 0;
5328 static LRESULT
5329 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5331 POINT pt;
5332 TRACKMOUSEEVENT trackinfo;
5333 TREEVIEW_ITEM * item;
5335 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5337 /* fill in the TRACKMOUSEEVENT struct */
5338 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5339 trackinfo.dwFlags = TME_QUERY;
5340 trackinfo.hwndTrack = infoPtr->hwnd;
5342 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5343 _TrackMouseEvent(&trackinfo);
5345 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5346 if(!(trackinfo.dwFlags & TME_LEAVE))
5348 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5349 trackinfo.hwndTrack = infoPtr->hwnd;
5350 /* do it as fast as possible, minimal systimer latency will be used */
5351 trackinfo.dwHoverTime = 1;
5353 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5354 /* and can properly deactivate the hot item */
5355 _TrackMouseEvent(&trackinfo);
5358 pt.x = (short)LOWORD(lParam);
5359 pt.y = (short)HIWORD(lParam);
5361 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5363 if (item != infoPtr->hotItem)
5365 /* redraw old hot item */
5366 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5367 infoPtr->hotItem = item;
5368 /* redraw new hot item */
5369 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5372 return 0;
5375 /* Draw themed border */
5376 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5378 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5379 HDC dc;
5380 RECT r;
5381 HRGN cliprgn;
5382 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5383 cyEdge = GetSystemMetrics (SM_CYEDGE);
5385 if (!theme)
5386 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5388 GetWindowRect(infoPtr->hwnd, &r);
5390 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5391 r.right - cxEdge, r.bottom - cyEdge);
5392 if (region != (HRGN)1)
5393 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5394 OffsetRect(&r, -r.left, -r.top);
5396 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5397 OffsetRect(&r, -r.left, -r.top);
5399 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5400 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5401 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5402 ReleaseDC(infoPtr->hwnd, dc);
5404 /* Call default proc to get the scrollbars etc. painted */
5405 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5407 return TRUE;
5410 static LRESULT
5411 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5413 LPNMHDR lpnmh = (LPNMHDR)lParam;
5415 if (lpnmh->code == PGN_CALCSIZE) {
5416 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5418 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5419 lppgc->iWidth = infoPtr->treeWidth;
5420 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5421 infoPtr->treeWidth, infoPtr->clientWidth);
5423 else {
5424 lppgc->iHeight = infoPtr->treeHeight;
5425 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5426 infoPtr->treeHeight, infoPtr->clientHeight);
5428 return 0;
5430 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5433 static LRESULT
5434 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5436 if (wParam == SIZE_RESTORED)
5438 infoPtr->clientWidth = (short)LOWORD(lParam);
5439 infoPtr->clientHeight = (short)HIWORD(lParam);
5441 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5442 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5443 TREEVIEW_UpdateScrollBars(infoPtr);
5445 else
5447 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5450 TREEVIEW_Invalidate(infoPtr, NULL);
5451 return 0;
5454 static void TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5456 TREEVIEW_ITEM *child = item->firstChild;
5458 item->state &= ~TVIS_STATEIMAGEMASK;
5459 item->state |= INDEXTOSTATEIMAGEMASK(1);
5461 while (child)
5463 TREEVIEW_ITEM *next = child->nextSibling;
5464 TREEVIEW_ResetImageStateIndex(infoPtr, child);
5465 child = next;
5469 static LRESULT
5470 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5472 TRACE("(%lx %lx)\n", wParam, lParam);
5474 if (wParam == GWL_STYLE)
5476 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5478 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5480 if (dwNewStyle & TVS_CHECKBOXES)
5482 TREEVIEW_InitCheckboxes(infoPtr);
5483 TRACE("checkboxes enabled\n");
5485 /* set all items to state image index 1 */
5486 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5488 else
5490 FIXME("tried to disable checkboxes\n");
5494 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5496 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5498 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5499 TRACE("tooltips enabled\n");
5501 else
5503 DestroyWindow(infoPtr->hwndToolTip);
5504 infoPtr->hwndToolTip = 0;
5505 TRACE("tooltips disabled\n");
5509 infoPtr->dwStyle = dwNewStyle;
5512 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5513 TREEVIEW_UpdateScrollBars(infoPtr);
5514 TREEVIEW_Invalidate(infoPtr, NULL);
5516 return 0;
5519 static LRESULT
5520 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5522 POINT pt;
5523 TREEVIEW_ITEM * item;
5524 NMMOUSE nmmouse;
5526 GetCursorPos(&pt);
5527 ScreenToClient(infoPtr->hwnd, &pt);
5529 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5531 memset(&nmmouse, 0, sizeof(nmmouse));
5532 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5533 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5534 nmmouse.hdr.code = NM_SETCURSOR;
5535 if (item)
5537 nmmouse.dwItemSpec = (DWORD_PTR)item;
5538 nmmouse.dwItemData = item->lParam;
5540 nmmouse.pt.x = 0;
5541 nmmouse.pt.y = 0;
5542 nmmouse.dwHitInfo = lParam;
5543 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, &nmmouse.hdr))
5544 return 0;
5546 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5548 SetCursor(infoPtr->hcurHand);
5549 return 0;
5551 else
5552 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5555 static LRESULT
5556 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5558 TRACE("\n");
5560 if (!infoPtr->selectedItem)
5562 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5563 TVC_UNKNOWN);
5566 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5567 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5568 return 0;
5571 static LRESULT
5572 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5574 TRACE("\n");
5576 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5577 UpdateWindow(infoPtr->hwnd);
5578 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5579 return 0;
5582 /* update theme after a WM_THEMECHANGED message */
5583 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5585 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5586 CloseThemeData (theme);
5587 OpenThemeData (infoPtr->hwnd, themeClass);
5588 return 0;
5592 static LRESULT WINAPI
5593 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5595 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5597 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5599 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5600 else
5602 if (uMsg == WM_CREATE)
5603 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5604 else
5605 goto def;
5608 switch (uMsg)
5610 case TVM_CREATEDRAGIMAGE:
5611 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5613 case TVM_DELETEITEM:
5614 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5616 case TVM_EDITLABELA:
5617 case TVM_EDITLABELW:
5618 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5620 case TVM_ENDEDITLABELNOW:
5621 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5623 case TVM_ENSUREVISIBLE:
5624 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5626 case TVM_EXPAND:
5627 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5629 case TVM_GETBKCOLOR:
5630 return TREEVIEW_GetBkColor(infoPtr);
5632 case TVM_GETCOUNT:
5633 return TREEVIEW_GetCount(infoPtr);
5635 case TVM_GETEDITCONTROL:
5636 return TREEVIEW_GetEditControl(infoPtr);
5638 case TVM_GETIMAGELIST:
5639 return TREEVIEW_GetImageList(infoPtr, wParam);
5641 case TVM_GETINDENT:
5642 return TREEVIEW_GetIndent(infoPtr);
5644 case TVM_GETINSERTMARKCOLOR:
5645 return TREEVIEW_GetInsertMarkColor(infoPtr);
5647 case TVM_GETISEARCHSTRINGA:
5648 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5649 return 0;
5651 case TVM_GETISEARCHSTRINGW:
5652 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5653 return 0;
5655 case TVM_GETITEMA:
5656 case TVM_GETITEMW:
5657 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5658 uMsg == TVM_GETITEMW);
5659 case TVM_GETITEMHEIGHT:
5660 return TREEVIEW_GetItemHeight(infoPtr);
5662 case TVM_GETITEMRECT:
5663 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5665 case TVM_GETITEMSTATE:
5666 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5668 case TVM_GETLINECOLOR:
5669 return TREEVIEW_GetLineColor(infoPtr);
5671 case TVM_GETNEXTITEM:
5672 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5674 case TVM_GETSCROLLTIME:
5675 return TREEVIEW_GetScrollTime(infoPtr);
5677 case TVM_GETTEXTCOLOR:
5678 return TREEVIEW_GetTextColor(infoPtr);
5680 case TVM_GETTOOLTIPS:
5681 return TREEVIEW_GetToolTips(infoPtr);
5683 case TVM_GETUNICODEFORMAT:
5684 return TREEVIEW_GetUnicodeFormat(infoPtr);
5686 case TVM_GETVISIBLECOUNT:
5687 return TREEVIEW_GetVisibleCount(infoPtr);
5689 case TVM_HITTEST:
5690 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5692 case TVM_INSERTITEMA:
5693 case TVM_INSERTITEMW:
5694 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5695 uMsg == TVM_INSERTITEMW);
5696 case TVM_SELECTITEM:
5697 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5699 case TVM_SETBKCOLOR:
5700 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5702 case TVM_SETIMAGELIST:
5703 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5705 case TVM_SETINDENT:
5706 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5708 case TVM_SETINSERTMARK:
5709 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5711 case TVM_SETINSERTMARKCOLOR:
5712 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5714 case TVM_SETITEMA:
5715 case TVM_SETITEMW:
5716 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5717 uMsg == TVM_SETITEMW);
5718 case TVM_SETLINECOLOR:
5719 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5721 case TVM_SETITEMHEIGHT:
5722 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5724 case TVM_SETSCROLLTIME:
5725 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5727 case TVM_SETTEXTCOLOR:
5728 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5730 case TVM_SETTOOLTIPS:
5731 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5733 case TVM_SETUNICODEFORMAT:
5734 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5736 case TVM_SORTCHILDREN:
5737 return TREEVIEW_SortChildren(infoPtr, lParam);
5739 case TVM_SORTCHILDRENCB:
5740 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5742 case WM_CHAR:
5743 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5745 case WM_COMMAND:
5746 return TREEVIEW_Command(infoPtr, wParam, lParam);
5748 case WM_DESTROY:
5749 return TREEVIEW_Destroy(infoPtr);
5751 /* WM_ENABLE */
5753 case WM_ERASEBKGND:
5754 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5756 case WM_GETDLGCODE:
5757 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5759 case WM_GETFONT:
5760 return TREEVIEW_GetFont(infoPtr);
5762 case WM_HSCROLL:
5763 return TREEVIEW_HScroll(infoPtr, wParam);
5765 case WM_KEYDOWN:
5766 return TREEVIEW_KeyDown(infoPtr, wParam);
5768 case WM_KILLFOCUS:
5769 return TREEVIEW_KillFocus(infoPtr);
5771 case WM_LBUTTONDBLCLK:
5772 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5774 case WM_LBUTTONDOWN:
5775 return TREEVIEW_LButtonDown(infoPtr, lParam);
5777 /* WM_MBUTTONDOWN */
5779 case WM_MOUSELEAVE:
5780 return TREEVIEW_MouseLeave(infoPtr);
5782 case WM_MOUSEMOVE:
5783 return TREEVIEW_MouseMove(infoPtr, lParam);
5785 case WM_NCLBUTTONDOWN:
5786 if (infoPtr->hwndEdit)
5787 SetFocus(infoPtr->hwnd);
5788 goto def;
5790 case WM_NCPAINT:
5791 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5793 case WM_NOTIFY:
5794 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5796 case WM_NOTIFYFORMAT:
5797 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5799 case WM_PRINTCLIENT:
5800 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5802 case WM_PAINT:
5803 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5805 case WM_RBUTTONDOWN:
5806 return TREEVIEW_RButtonDown(infoPtr, lParam);
5808 case WM_SETCURSOR:
5809 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5811 case WM_SETFOCUS:
5812 return TREEVIEW_SetFocus(infoPtr);
5814 case WM_SETFONT:
5815 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5817 case WM_SETREDRAW:
5818 return TREEVIEW_SetRedraw(infoPtr, wParam);
5820 case WM_SIZE:
5821 return TREEVIEW_Size(infoPtr, wParam, lParam);
5823 case WM_STYLECHANGED:
5824 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5826 case WM_SYSCOLORCHANGE:
5827 COMCTL32_RefreshSysColors();
5828 return 0;
5830 /* WM_SYSKEYDOWN */
5832 case WM_TIMER:
5833 return TREEVIEW_HandleTimer(infoPtr, wParam);
5835 case WM_THEMECHANGED:
5836 return TREEVIEW_ThemeChanged (infoPtr);
5838 case WM_VSCROLL:
5839 return TREEVIEW_VScroll(infoPtr, wParam);
5841 /* WM_WININICHANGE */
5843 case WM_MOUSEWHEEL:
5844 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5846 case WM_DRAWITEM:
5847 TRACE("drawItem\n");
5848 goto def;
5850 default:
5851 /* This mostly catches MFC and Delphi messages. :( */
5852 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5853 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5854 def:
5855 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5860 /* Class Registration ***************************************************/
5862 VOID
5863 TREEVIEW_Register(void)
5865 WNDCLASSW wndClass;
5867 TRACE("\n");
5869 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5870 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5871 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5872 wndClass.cbClsExtra = 0;
5873 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5875 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5876 wndClass.hbrBackground = 0;
5877 wndClass.lpszClassName = WC_TREEVIEWW;
5879 RegisterClassW(&wndClass);
5883 VOID
5884 TREEVIEW_Unregister(void)
5886 UnregisterClassW(WC_TREEVIEWW, NULL);
5890 /* Tree Verification ****************************************************/
5892 static inline void
5893 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5895 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5896 const TREEVIEW_ITEM *item)
5898 assert(infoPtr != NULL);
5899 assert(item != NULL);
5901 /* both NULL, or both non-null */
5902 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5904 assert(item->firstChild != item);
5905 assert(item->lastChild != item);
5907 if (item->firstChild)
5909 assert(item->firstChild->parent == item);
5910 assert(item->firstChild->prevSibling == NULL);
5913 if (item->lastChild)
5915 assert(item->lastChild->parent == item);
5916 assert(item->lastChild->nextSibling == NULL);
5919 assert(item->nextSibling != item);
5920 if (item->nextSibling)
5922 assert(item->nextSibling->parent == item->parent);
5923 assert(item->nextSibling->prevSibling == item);
5926 assert(item->prevSibling != item);
5927 if (item->prevSibling)
5929 assert(item->prevSibling->parent == item->parent);
5930 assert(item->prevSibling->nextSibling == item);
5934 static inline void
5935 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5937 assert(item != NULL);
5939 assert(item->parent != NULL);
5940 assert(item->parent != item);
5941 assert(item->iLevel == item->parent->iLevel + 1);
5943 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5945 TREEVIEW_VerifyItemCommon(infoPtr, item);
5947 TREEVIEW_VerifyChildren(infoPtr, item);
5950 static inline void
5951 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5953 const TREEVIEW_ITEM *child;
5954 assert(item != NULL);
5956 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5957 TREEVIEW_VerifyItem(infoPtr, child);
5960 static inline void
5961 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5963 TREEVIEW_ITEM *root = infoPtr->root;
5965 assert(root != NULL);
5966 assert(root->iLevel == -1);
5967 assert(root->parent == NULL);
5968 assert(root->prevSibling == NULL);
5970 TREEVIEW_VerifyItemCommon(infoPtr, root);
5972 TREEVIEW_VerifyChildren(infoPtr, root);
5975 static void
5976 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5978 if (!TRACE_ON(treeview)) return;
5980 assert(infoPtr != NULL);
5981 TREEVIEW_VerifyRoot(infoPtr);