winepulse v18: Latency and compilation improvements
[wine/multimedia.git] / dlls / comctl32 / treeview.c
blobd8fe6358bf3ca76979d22f4f0f926240bd87c231
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 COLORREF clrBk;
106 COLORREF clrText;
107 COLORREF clrLine;
108 COLORREF clrInsertMark;
109 HFONT hFont;
110 HFONT hDefaultFont;
111 HFONT hBoldFont;
112 HFONT hUnderlineFont;
113 HCURSOR hcurHand;
114 HWND hwndToolTip;
116 HWND hwndEdit;
117 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
118 BOOL bIgnoreEditKillFocus;
119 BOOL bLabelChanged;
121 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
122 HIMAGELIST himlNormal;
123 int normalImageHeight;
124 int normalImageWidth;
125 HIMAGELIST himlState;
126 int stateImageHeight;
127 int stateImageWidth;
128 HDPA items;
130 DWORD lastKeyPressTimestamp;
131 WPARAM charCode;
132 INT nSearchParamLength;
133 WCHAR szSearchParam[ MAX_PATH ];
134 } TREEVIEW_INFO;
136 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
138 HTREEITEM parent; /* handle to parent or 0 if at root */
139 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
140 HTREEITEM firstChild; /* handle to first child or 0 if no child */
142 UINT callbackMask;
143 UINT state;
144 UINT stateMask;
145 LPWSTR pszText;
146 int cchTextMax;
147 int iImage;
148 int iSelectedImage;
149 int iExpandedImage;
150 int cChildren;
151 LPARAM lParam;
152 int iIntegral; /* item height multiplier (1 is normal) */
153 int iLevel; /* indentation level:0=root level */
154 HTREEITEM lastChild;
155 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
156 RECT rect;
157 LONG linesOffset;
158 LONG stateOffset;
159 LONG imageOffset;
160 LONG textOffset;
161 LONG textWidth; /* horizontal text extent for pszText */
162 LONG visibleOrder; /* visible ordering, 0 is first visible item */
163 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
164 } TREEVIEW_ITEM;
166 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
167 #define KEY_DELAY 450
169 /* bitflags for infoPtr->uInternalStatus */
171 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
172 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
173 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
174 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
175 #define TV_RDRAG 0x10 /* ditto Rbutton */
176 #define TV_RDRAGGING 0x20
178 /* bitflags for infoPtr->timer */
180 #define TV_EDIT_TIMER 2
181 #define TV_EDIT_TIMER_SET 2
183 #define TEXT_CALLBACK_SIZE 260
185 #define TREEVIEW_LEFT_MARGIN 8
187 #define MINIMUM_INDENT 19
189 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
191 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
192 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
193 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
195 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
196 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
197 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
198 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
200 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
203 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
206 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
208 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
209 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
210 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
211 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
212 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
213 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
215 /* Random Utilities *****************************************************/
216 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
218 /* Returns the treeview private data if hwnd is a treeview.
219 * Otherwise returns an undefined value. */
220 static inline TREEVIEW_INFO *
221 TREEVIEW_GetInfoPtr(HWND hwnd)
223 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
226 /* Don't call this. Nothing wants an item index. */
227 static inline int
228 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
230 return DPA_GetPtrIndex(infoPtr->items, handle);
233 /* Checks if item has changed and needs to be redrawn */
234 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
235 const TVITEMEXW *tvChange)
237 /* Number of children has changed */
238 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
239 return TRUE;
241 /* Image has changed and it's not a callback */
242 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
243 tiNew->iImage != I_IMAGECALLBACK)
244 return TRUE;
246 /* Selected image has changed and it's not a callback */
247 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
248 tiNew->iSelectedImage != I_IMAGECALLBACK)
249 return TRUE;
251 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
252 tiNew->iExpandedImage != I_IMAGECALLBACK)
253 return TRUE;
255 /* Text has changed and it's not a callback */
256 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
257 tiNew->pszText != LPSTR_TEXTCALLBACKW)
258 return TRUE;
260 /* Indent has changed */
261 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
262 return TRUE;
264 /* Item state has changed */
265 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
266 return TRUE;
268 return FALSE;
271 /***************************************************************************
272 * This method checks that handle is an item for this tree.
274 static BOOL
275 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
277 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
279 TRACE("invalid item %p\n", handle);
280 return FALSE;
282 else
283 return TRUE;
286 static HFONT
287 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
289 LOGFONTW font;
291 GetObjectW(hOrigFont, sizeof(font), &font);
292 font.lfWeight = FW_BOLD;
293 return CreateFontIndirectW(&font);
296 static HFONT
297 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
299 LOGFONTW font;
301 GetObjectW(hOrigFont, sizeof(font), &font);
302 font.lfUnderline = TRUE;
303 return CreateFontIndirectW(&font);
306 static inline HFONT
307 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
309 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
310 return infoPtr->hUnderlineFont;
311 if (item->state & TVIS_BOLD)
312 return infoPtr->hBoldFont;
313 return infoPtr->hFont;
316 /* for trace/debugging purposes only */
317 static const char *
318 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
320 if (item == NULL) return "<null item>";
321 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
322 if (item->pszText == NULL) return "<null>";
323 return debugstr_w(item->pszText);
326 /* An item is not a child of itself. */
327 static BOOL
328 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
332 child = child->parent;
333 if (child == parent) return TRUE;
334 } while (child != NULL);
336 return FALSE;
340 /* Tree Traversal *******************************************************/
342 /***************************************************************************
343 * This method returns the last expanded sibling or child child item
344 * of a tree node
346 static TREEVIEW_ITEM *
347 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
349 if (!item) return NULL;
351 while (item->lastChild)
353 if (item->state & TVIS_EXPANDED)
354 item = item->lastChild;
355 else
356 break;
359 if (item == infoPtr->root)
360 return NULL;
362 return item;
365 /***************************************************************************
366 * This method returns the previous non-hidden item in the list not
367 * considering the tree hierarchy.
369 static TREEVIEW_ITEM *
370 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
372 if (tvItem->prevSibling)
374 /* This item has a prevSibling, get the last item in the sibling's tree. */
375 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
377 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
378 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
379 else
380 return upItem;
382 else
384 /* this item does not have a prevSibling, get the parent */
385 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
390 /***************************************************************************
391 * This method returns the next physical item in the treeview not
392 * considering the tree hierarchy.
394 static TREEVIEW_ITEM *
395 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
398 * If this item has children and is expanded, return the first child
400 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
402 return tvItem->firstChild;
407 * try to get the sibling
409 if (tvItem->nextSibling)
410 return tvItem->nextSibling;
413 * Otherwise, get the parent's sibling.
415 while (tvItem->parent)
417 tvItem = tvItem->parent;
419 if (tvItem->nextSibling)
420 return tvItem->nextSibling;
423 return NULL;
426 /***************************************************************************
427 * This method returns the nth item starting at the given item. It returns
428 * the last item (or first) we we run out of items.
430 * Will scroll backward if count is <0.
431 * forward if count is >0.
433 static TREEVIEW_ITEM *
434 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
435 LONG count)
437 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
438 TREEVIEW_ITEM *previousItem;
440 assert(item != NULL);
442 if (count > 0)
444 next_item = TREEVIEW_GetNextListItem;
446 else if (count < 0)
448 count = -count;
449 next_item = TREEVIEW_GetPrevListItem;
451 else
452 return item;
456 previousItem = item;
457 item = next_item(infoPtr, item);
459 } while (--count && item != NULL);
462 return item ? item : previousItem;
465 /* Notifications ************************************************************/
467 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
469 if (!infoPtr->bNtfUnicode) {
470 switch (code) {
471 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
472 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
473 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
474 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
475 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
476 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
477 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
478 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
479 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
480 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
481 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
482 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
485 return code;
488 static inline BOOL
489 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPNMHDR pnmh)
491 TRACE("wParam=%ld, lParam=%p\n", wParam, pnmh);
492 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, (LPARAM)pnmh);
495 static BOOL
496 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
498 NMHDR nmhdr;
499 HWND hwnd = infoPtr->hwnd;
501 TRACE("%d\n", code);
502 nmhdr.hwndFrom = hwnd;
503 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
504 nmhdr.code = get_notifycode(infoPtr, code);
506 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, &nmhdr);
509 static VOID
510 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
512 tvItem->mask = mask;
513 tvItem->hItem = item;
514 tvItem->state = item->state;
515 tvItem->stateMask = 0;
516 tvItem->iImage = item->iImage;
517 tvItem->iSelectedImage = item->iSelectedImage;
518 tvItem->cChildren = item->cChildren;
519 tvItem->lParam = item->lParam;
521 if(mask & TVIF_TEXT)
523 if (!infoPtr->bNtfUnicode)
525 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
526 tvItem->pszText = Alloc (tvItem->cchTextMax);
527 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
529 else
531 tvItem->cchTextMax = item->cchTextMax;
532 tvItem->pszText = item->pszText;
535 else
537 tvItem->cchTextMax = 0;
538 tvItem->pszText = NULL;
542 static BOOL
543 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
544 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
546 HWND hwnd = infoPtr->hwnd;
547 NMTREEVIEWW nmhdr;
548 BOOL ret;
550 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
551 code, action, oldItem, newItem);
553 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
555 nmhdr.hdr.hwndFrom = hwnd;
556 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
557 nmhdr.hdr.code = get_notifycode(infoPtr, code);
558 nmhdr.action = action;
560 if (oldItem)
561 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
563 if (newItem)
564 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
566 nmhdr.ptDrag.x = 0;
567 nmhdr.ptDrag.y = 0;
569 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
570 if (!infoPtr->bNtfUnicode)
572 Free(nmhdr.itemOld.pszText);
573 Free(nmhdr.itemNew.pszText);
575 return ret;
578 static BOOL
579 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
580 HTREEITEM dragItem, POINT pt)
582 HWND hwnd = infoPtr->hwnd;
583 NMTREEVIEWW nmhdr;
585 TRACE("code:%d dragitem:%p\n", code, dragItem);
587 nmhdr.hdr.hwndFrom = hwnd;
588 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
589 nmhdr.hdr.code = get_notifycode(infoPtr, code);
590 nmhdr.action = 0;
591 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
592 nmhdr.itemNew.hItem = dragItem;
593 nmhdr.itemNew.state = dragItem->state;
594 nmhdr.itemNew.lParam = dragItem->lParam;
596 nmhdr.ptDrag.x = pt.x;
597 nmhdr.ptDrag.y = pt.y;
599 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
603 static BOOL
604 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
605 HDC hdc, RECT rc)
607 HWND hwnd = infoPtr->hwnd;
608 NMTVCUSTOMDRAW nmcdhdr;
609 LPNMCUSTOMDRAW nmcd;
611 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
613 nmcd = &nmcdhdr.nmcd;
614 nmcd->hdr.hwndFrom = hwnd;
615 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
616 nmcd->hdr.code = NM_CUSTOMDRAW;
617 nmcd->dwDrawStage = dwDrawStage;
618 nmcd->hdc = hdc;
619 nmcd->rc = rc;
620 nmcd->dwItemSpec = 0;
621 nmcd->uItemState = 0;
622 nmcd->lItemlParam = 0;
623 nmcdhdr.clrText = infoPtr->clrText;
624 nmcdhdr.clrTextBk = infoPtr->clrBk;
625 nmcdhdr.iLevel = 0;
627 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr.nmcd.hdr);
632 /* FIXME: need to find out when the flags in uItemState need to be set */
634 static BOOL
635 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
636 TREEVIEW_ITEM *item, UINT uItemDrawState,
637 NMTVCUSTOMDRAW *nmcdhdr)
639 HWND hwnd = infoPtr->hwnd;
640 LPNMCUSTOMDRAW nmcd;
641 DWORD dwDrawStage;
642 DWORD_PTR dwItemSpec;
643 UINT uItemState;
645 dwDrawStage = CDDS_ITEM | uItemDrawState;
646 dwItemSpec = (DWORD_PTR)item;
647 uItemState = 0;
648 if (item->state & TVIS_SELECTED)
649 uItemState |= CDIS_SELECTED;
650 if (item == infoPtr->selectedItem)
651 uItemState |= CDIS_FOCUS;
652 if (item == infoPtr->hotItem)
653 uItemState |= CDIS_HOT;
655 nmcd = &nmcdhdr->nmcd;
656 nmcd->hdr.hwndFrom = hwnd;
657 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
658 nmcd->hdr.code = NM_CUSTOMDRAW;
659 nmcd->dwDrawStage = dwDrawStage;
660 nmcd->hdc = hdc;
661 nmcd->rc = item->rect;
662 nmcd->dwItemSpec = dwItemSpec;
663 nmcd->uItemState = uItemState;
664 nmcd->lItemlParam = item->lParam;
665 nmcdhdr->iLevel = item->iLevel;
667 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
668 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
669 nmcd->uItemState, nmcd->lItemlParam);
671 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr->nmcd.hdr);
674 static BOOL
675 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
677 HWND hwnd = infoPtr->hwnd;
678 NMTVDISPINFOW tvdi;
679 BOOL ret;
681 tvdi.hdr.hwndFrom = hwnd;
682 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
683 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
685 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
686 &tvdi.item, editItem);
688 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
690 if (!infoPtr->bNtfUnicode)
691 Free(tvdi.item.pszText);
693 return ret;
696 static void
697 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
698 UINT mask)
700 NMTVDISPINFOEXW callback;
701 HWND hwnd = infoPtr->hwnd;
703 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
704 mask &= item->callbackMask;
706 if (mask == 0) return;
708 callback.hdr.hwndFrom = hwnd;
709 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
710 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
712 /* 'state' always contains valid value, as well as 'lParam'.
713 * All other parameters are uninitialized.
715 callback.item.pszText = item->pszText;
716 callback.item.cchTextMax = item->cchTextMax;
717 callback.item.mask = mask;
718 callback.item.hItem = item;
719 callback.item.state = item->state;
720 callback.item.lParam = item->lParam;
722 /* If text is changed we need to recalculate textWidth */
723 if (mask & TVIF_TEXT)
724 item->textWidth = 0;
726 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, &callback.hdr);
727 TRACE("resulting code 0x%08x\n", callback.hdr.code);
729 /* It may have changed due to a call to SetItem. */
730 mask &= item->callbackMask;
732 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
734 /* Instead of copying text into our buffer user specified his own */
735 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
736 LPWSTR newText;
737 int buflen;
738 int len = MultiByteToWideChar( CP_ACP, 0,
739 (LPSTR)callback.item.pszText, -1,
740 NULL, 0);
741 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
742 newText = ReAlloc(item->pszText, buflen);
744 TRACE("returned str %s, len=%d, buflen=%d\n",
745 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
747 if (newText)
749 item->pszText = newText;
750 MultiByteToWideChar( CP_ACP, 0,
751 (LPSTR)callback.item.pszText, -1,
752 item->pszText, buflen/sizeof(WCHAR));
753 item->cchTextMax = buflen/sizeof(WCHAR);
755 /* If ReAlloc fails we have nothing to do, but keep original text */
757 else {
758 int len = max(lstrlenW(callback.item.pszText) + 1,
759 TEXT_CALLBACK_SIZE);
760 LPWSTR newText = ReAlloc(item->pszText, len);
762 TRACE("returned wstr %s, len=%d\n",
763 debugstr_w(callback.item.pszText), len);
765 if (newText)
767 item->pszText = newText;
768 strcpyW(item->pszText, callback.item.pszText);
769 item->cchTextMax = len;
771 /* If ReAlloc fails we have nothing to do, but keep original text */
774 else if (mask & TVIF_TEXT) {
775 /* User put text into our buffer, that is ok unless A string */
776 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
777 LPWSTR newText;
778 int buflen;
779 int len = MultiByteToWideChar( CP_ACP, 0,
780 (LPSTR)callback.item.pszText, -1,
781 NULL, 0);
782 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
783 newText = Alloc(buflen);
785 TRACE("same buffer str %s, len=%d, buflen=%d\n",
786 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
788 if (newText)
790 LPWSTR oldText = item->pszText;
791 item->pszText = newText;
792 MultiByteToWideChar( CP_ACP, 0,
793 (LPSTR)callback.item.pszText, -1,
794 item->pszText, buflen/sizeof(WCHAR));
795 item->cchTextMax = buflen/sizeof(WCHAR);
796 Free(oldText);
801 if (mask & TVIF_IMAGE)
802 item->iImage = callback.item.iImage;
804 if (mask & TVIF_SELECTEDIMAGE)
805 item->iSelectedImage = callback.item.iSelectedImage;
807 if (mask & TVIF_EXPANDEDIMAGE)
808 item->iExpandedImage = callback.item.iExpandedImage;
810 if (mask & TVIF_CHILDREN)
811 item->cChildren = callback.item.cChildren;
813 if (callback.item.mask & TVIF_STATE)
815 item->state &= ~callback.item.stateMask;
816 item->state |= (callback.item.state & callback.item.stateMask);
819 /* These members are now permanently set. */
820 if (callback.item.mask & TVIF_DI_SETITEM)
821 item->callbackMask &= ~callback.item.mask;
824 /***************************************************************************
825 * This function uses cChildren field to decide whether the item has
826 * children or not.
827 * Note: if this returns TRUE, the child items may not actually exist,
828 * they could be virtual.
830 * Just use item->firstChild to check for physical children.
832 static BOOL
833 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
835 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
837 return item->cChildren > 0;
840 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
842 INT format;
844 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
846 if (nCommand != NF_REQUERY) return 0;
848 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
849 TRACE("format=%d\n", format);
851 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
853 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
855 return format;
858 /* Item Position ********************************************************/
860 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
861 static VOID
862 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
864 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
865 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
866 > TVS_LINESATROOT);
868 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
869 - infoPtr->scrollX;
870 item->stateOffset = item->linesOffset + infoPtr->uIndent;
871 item->imageOffset = item->stateOffset
872 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
873 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
876 static VOID
877 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
879 HDC hdc;
880 HFONT hOldFont=0;
881 SIZE sz;
883 /* DRAW's OM docker creates items like this */
884 if (item->pszText == NULL)
886 item->textWidth = 0;
887 return;
890 if (hDC != 0)
892 hdc = hDC;
894 else
896 hdc = GetDC(infoPtr->hwnd);
897 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
900 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
901 item->textWidth = sz.cx;
903 if (hDC == 0)
905 SelectObject(hdc, hOldFont);
906 ReleaseDC(0, hdc);
910 static VOID
911 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
913 item->rect.top = infoPtr->uItemHeight *
914 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
916 item->rect.bottom = item->rect.top
917 + infoPtr->uItemHeight * item->iIntegral - 1;
919 item->rect.left = 0;
920 item->rect.right = infoPtr->clientWidth;
923 /* We know that only items after start need their order updated. */
924 static void
925 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
927 TREEVIEW_ITEM *item;
928 int order;
930 if (!start)
932 start = infoPtr->root->firstChild;
933 order = 0;
935 else
936 order = start->visibleOrder;
938 for (item = start; item != NULL;
939 item = TREEVIEW_GetNextListItem(infoPtr, item))
941 if (!ISVISIBLE(item) && order > 0)
942 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
943 item->visibleOrder = order;
944 order += item->iIntegral;
947 infoPtr->maxVisibleOrder = order;
949 for (item = start; item != NULL;
950 item = TREEVIEW_GetNextListItem(infoPtr, item))
952 TREEVIEW_ComputeItemRect(infoPtr, item);
957 /* Update metrics of all items in selected subtree.
958 * root must be expanded
960 static VOID
961 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
963 TREEVIEW_ITEM *sibling;
964 HDC hdc;
965 HFONT hOldFont;
967 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
968 return;
970 root->state &= ~TVIS_EXPANDED;
971 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
972 root->state |= TVIS_EXPANDED;
974 hdc = GetDC(infoPtr->hwnd);
975 hOldFont = SelectObject(hdc, infoPtr->hFont);
977 for (; root != sibling;
978 root = TREEVIEW_GetNextListItem(infoPtr, root))
980 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
982 if (root->callbackMask & TVIF_TEXT)
983 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
985 if (root->textWidth == 0)
987 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
988 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
992 SelectObject(hdc, hOldFont);
993 ReleaseDC(infoPtr->hwnd, hdc);
996 /* Item Allocation **********************************************************/
998 static TREEVIEW_ITEM *
999 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1001 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1003 if (!newItem)
1004 return NULL;
1006 /* I_IMAGENONE would make more sense but this is neither what is
1007 * documented (MSDN doesn't specify) nor what Windows actually does
1008 * (it sets it to zero)... and I can so imagine an application using
1009 * inc/dec to toggle the images. */
1010 newItem->iImage = 0;
1011 newItem->iSelectedImage = 0;
1012 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1013 newItem->infoPtr = infoPtr;
1015 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1017 Free(newItem);
1018 return NULL;
1021 return newItem;
1024 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1025 * free item->pszText. */
1026 static void
1027 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1029 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1030 if (infoPtr->selectedItem == item)
1031 infoPtr->selectedItem = NULL;
1032 if (infoPtr->hotItem == item)
1033 infoPtr->hotItem = NULL;
1034 if (infoPtr->focusedItem == item)
1035 infoPtr->focusedItem = NULL;
1036 if (infoPtr->firstVisible == item)
1037 infoPtr->firstVisible = NULL;
1038 if (infoPtr->dropItem == item)
1039 infoPtr->dropItem = NULL;
1040 if (infoPtr->insertMarkItem == item)
1041 infoPtr->insertMarkItem = NULL;
1042 Free(item);
1046 /* Item Insertion *******************************************************/
1048 /***************************************************************************
1049 * This method inserts newItem before sibling as a child of parent.
1050 * sibling can be NULL, but only if parent has no children.
1052 static void
1053 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1054 TREEVIEW_ITEM *parent)
1056 assert(parent != NULL);
1058 if (sibling != NULL)
1060 assert(sibling->parent == parent);
1062 if (sibling->prevSibling != NULL)
1063 sibling->prevSibling->nextSibling = newItem;
1065 newItem->prevSibling = sibling->prevSibling;
1066 sibling->prevSibling = newItem;
1068 else
1069 newItem->prevSibling = NULL;
1071 newItem->nextSibling = sibling;
1073 if (parent->firstChild == sibling)
1074 parent->firstChild = newItem;
1076 if (parent->lastChild == NULL)
1077 parent->lastChild = newItem;
1080 /***************************************************************************
1081 * This method inserts newItem after sibling as a child of parent.
1082 * sibling can be NULL, but only if parent has no children.
1084 static void
1085 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1086 TREEVIEW_ITEM *parent)
1088 assert(parent != NULL);
1090 if (sibling != NULL)
1092 assert(sibling->parent == parent);
1094 if (sibling->nextSibling != NULL)
1095 sibling->nextSibling->prevSibling = newItem;
1097 newItem->nextSibling = sibling->nextSibling;
1098 sibling->nextSibling = newItem;
1100 else
1101 newItem->nextSibling = NULL;
1103 newItem->prevSibling = sibling;
1105 if (parent->lastChild == sibling)
1106 parent->lastChild = newItem;
1108 if (parent->firstChild == NULL)
1109 parent->firstChild = newItem;
1112 static BOOL
1113 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1114 const TVITEMEXW *tvItem, BOOL isW)
1116 UINT callbackClear = 0;
1117 UINT callbackSet = 0;
1119 TRACE("item %p\n", item);
1120 /* Do this first in case it fails. */
1121 if (tvItem->mask & TVIF_TEXT)
1123 item->textWidth = 0; /* force width recalculation */
1124 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1126 int len;
1127 LPWSTR newText;
1128 if (isW)
1129 len = lstrlenW(tvItem->pszText) + 1;
1130 else
1131 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1133 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1135 if (newText == NULL) return FALSE;
1137 callbackClear |= TVIF_TEXT;
1139 item->pszText = newText;
1140 item->cchTextMax = len;
1141 if (isW)
1142 lstrcpynW(item->pszText, tvItem->pszText, len);
1143 else
1144 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1145 item->pszText, len);
1147 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1149 else
1151 callbackSet |= TVIF_TEXT;
1153 item->pszText = ReAlloc(item->pszText,
1154 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1155 item->cchTextMax = TEXT_CALLBACK_SIZE;
1156 TRACE("setting callback, item %p\n", item);
1160 if (tvItem->mask & TVIF_CHILDREN)
1162 item->cChildren = tvItem->cChildren;
1164 if (item->cChildren == I_CHILDRENCALLBACK)
1165 callbackSet |= TVIF_CHILDREN;
1166 else
1167 callbackClear |= TVIF_CHILDREN;
1170 if (tvItem->mask & TVIF_IMAGE)
1172 item->iImage = tvItem->iImage;
1174 if (item->iImage == I_IMAGECALLBACK)
1175 callbackSet |= TVIF_IMAGE;
1176 else
1177 callbackClear |= TVIF_IMAGE;
1180 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1182 item->iSelectedImage = tvItem->iSelectedImage;
1184 if (item->iSelectedImage == I_IMAGECALLBACK)
1185 callbackSet |= TVIF_SELECTEDIMAGE;
1186 else
1187 callbackClear |= TVIF_SELECTEDIMAGE;
1190 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1192 item->iExpandedImage = tvItem->iExpandedImage;
1194 if (item->iExpandedImage == I_IMAGECALLBACK)
1195 callbackSet |= TVIF_EXPANDEDIMAGE;
1196 else
1197 callbackClear |= TVIF_EXPANDEDIMAGE;
1200 if (tvItem->mask & TVIF_PARAM)
1201 item->lParam = tvItem->lParam;
1203 /* If the application sets TVIF_INTEGRAL without
1204 * supplying a TVITEMEX structure, it's toast. */
1205 if (tvItem->mask & TVIF_INTEGRAL)
1206 item->iIntegral = tvItem->iIntegral;
1208 if (tvItem->mask & TVIF_STATE)
1210 TRACE("prevstate,state,mask:%x,%x,%x\n", item->state, tvItem->state,
1211 tvItem->stateMask);
1212 item->state &= ~tvItem->stateMask;
1213 item->state |= (tvItem->state & tvItem->stateMask);
1216 if (tvItem->mask & TVIF_STATEEX)
1218 FIXME("New extended state: %x\n", tvItem->uStateEx);
1221 item->callbackMask |= callbackSet;
1222 item->callbackMask &= ~callbackClear;
1224 return TRUE;
1227 /* Note that the new item is pre-zeroed. */
1228 static LRESULT
1229 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1231 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1232 HTREEITEM insertAfter;
1233 TREEVIEW_ITEM *newItem, *parentItem;
1234 BOOL bTextUpdated = FALSE;
1236 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1238 parentItem = infoPtr->root;
1240 else
1242 parentItem = ptdi->hParent;
1244 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1246 WARN("invalid parent %p\n", parentItem);
1247 return 0;
1251 insertAfter = ptdi->hInsertAfter;
1253 /* Validate this now for convenience. */
1254 switch ((DWORD_PTR)insertAfter)
1256 case (DWORD_PTR)TVI_FIRST:
1257 case (DWORD_PTR)TVI_LAST:
1258 case (DWORD_PTR)TVI_SORT:
1259 break;
1261 default:
1262 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1263 insertAfter->parent != parentItem)
1265 WARN("invalid insert after %p\n", insertAfter);
1266 insertAfter = TVI_LAST;
1270 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1271 (tvItem->mask & TVIF_TEXT)
1272 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1273 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1274 : "<no label>");
1276 newItem = TREEVIEW_AllocateItem(infoPtr);
1277 if (newItem == NULL)
1278 return 0;
1280 newItem->parent = parentItem;
1281 newItem->iIntegral = 1;
1282 newItem->visibleOrder = -1;
1284 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1285 return 0;
1287 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1289 infoPtr->uNumItems++;
1291 switch ((DWORD_PTR)insertAfter)
1293 case (DWORD_PTR)TVI_FIRST:
1295 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1296 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1297 if (infoPtr->firstVisible == originalFirst)
1298 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1300 break;
1302 case (DWORD_PTR)TVI_LAST:
1303 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1304 break;
1306 /* hInsertAfter names a specific item we want to insert after */
1307 default:
1308 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1309 break;
1311 case (DWORD_PTR)TVI_SORT:
1313 TREEVIEW_ITEM *aChild;
1314 TREEVIEW_ITEM *previousChild = NULL;
1315 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1316 BOOL bItemInserted = FALSE;
1318 aChild = parentItem->firstChild;
1320 bTextUpdated = TRUE;
1321 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1323 /* Iterate the parent children to see where we fit in */
1324 while (aChild != NULL)
1326 INT comp;
1328 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1329 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1331 if (comp < 0) /* we are smaller than the current one */
1333 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1334 if (infoPtr->firstVisible == originalFirst &&
1335 aChild == originalFirst)
1336 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1337 bItemInserted = TRUE;
1338 break;
1340 else if (comp > 0) /* we are bigger than the current one */
1342 previousChild = aChild;
1344 /* This will help us to exit if there is no more sibling */
1345 aChild = (aChild->nextSibling == 0)
1346 ? NULL
1347 : aChild->nextSibling;
1349 /* Look at the next item */
1350 continue;
1352 else if (comp == 0)
1355 * An item with this name is already existing, therefore,
1356 * we add after the one we found
1358 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1359 bItemInserted = TRUE;
1360 break;
1365 * we reach the end of the child list and the item has not
1366 * yet been inserted, therefore, insert it after the last child.
1368 if ((!bItemInserted) && (aChild == NULL))
1369 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1371 break;
1376 TRACE("new item %p; parent %p, mask %x\n", newItem,
1377 newItem->parent, tvItem->mask);
1379 newItem->iLevel = newItem->parent->iLevel + 1;
1381 if (newItem->parent->cChildren == 0)
1382 newItem->parent->cChildren = 1;
1384 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1386 if (STATEIMAGEINDEX(newItem->state) == 0)
1387 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1390 if (infoPtr->firstVisible == NULL)
1391 infoPtr->firstVisible = newItem;
1393 TREEVIEW_VerifyTree(infoPtr);
1395 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1397 if (parentItem == infoPtr->root ||
1398 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1400 TREEVIEW_ITEM *item;
1401 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1403 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1404 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1406 if (!bTextUpdated)
1407 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1409 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1410 TREEVIEW_UpdateScrollBars(infoPtr);
1412 * if the item was inserted in a visible part of the tree,
1413 * invalidate it, as well as those after it
1415 for (item = newItem;
1416 item != NULL;
1417 item = TREEVIEW_GetNextListItem(infoPtr, item))
1418 TREEVIEW_Invalidate(infoPtr, item);
1420 else
1422 /* refresh treeview if newItem is the first item inserted under parentItem */
1423 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1425 /* parent got '+' - update it */
1426 TREEVIEW_Invalidate(infoPtr, parentItem);
1430 return (LRESULT)newItem;
1433 /* Item Deletion ************************************************************/
1434 static void
1435 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1437 static void
1438 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1440 TREEVIEW_ITEM *kill = parentItem->firstChild;
1442 while (kill != NULL)
1444 TREEVIEW_ITEM *next = kill->nextSibling;
1446 TREEVIEW_RemoveItem(infoPtr, kill);
1448 kill = next;
1451 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1452 assert(parentItem->firstChild == NULL);
1453 assert(parentItem->lastChild == NULL);
1456 static void
1457 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1459 TREEVIEW_ITEM *parentItem = item->parent;
1461 assert(item != NULL);
1462 assert(item->parent != NULL); /* i.e. it must not be the root */
1464 if (parentItem->firstChild == item)
1465 parentItem->firstChild = item->nextSibling;
1467 if (parentItem->lastChild == item)
1468 parentItem->lastChild = item->prevSibling;
1470 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1471 && parentItem->cChildren > 0)
1472 parentItem->cChildren = 0;
1474 if (item->prevSibling)
1475 item->prevSibling->nextSibling = item->nextSibling;
1477 if (item->nextSibling)
1478 item->nextSibling->prevSibling = item->prevSibling;
1481 static void
1482 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1484 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1486 if (item->firstChild)
1487 TREEVIEW_RemoveAllChildren(infoPtr, item);
1489 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1490 TVIF_HANDLE | TVIF_PARAM, item, 0);
1492 TREEVIEW_UnlinkItem(item);
1494 infoPtr->uNumItems--;
1496 if (item->pszText != LPSTR_TEXTCALLBACKW)
1497 Free(item->pszText);
1499 TREEVIEW_FreeItem(infoPtr, item);
1503 /* Empty out the tree. */
1504 static void
1505 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1507 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1509 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1512 static LRESULT
1513 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1515 TREEVIEW_ITEM *newSelection = NULL;
1516 TREEVIEW_ITEM *newFirstVisible = NULL;
1517 TREEVIEW_ITEM *parent, *prev = NULL;
1518 BOOL visible = FALSE;
1520 if (item == TVI_ROOT || !item)
1522 TRACE("TVI_ROOT\n");
1523 parent = infoPtr->root;
1524 newSelection = NULL;
1525 visible = TRUE;
1526 TREEVIEW_RemoveTree(infoPtr);
1528 else
1530 if (!TREEVIEW_ValidItem(infoPtr, item))
1531 return FALSE;
1533 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1534 parent = item->parent;
1536 if (ISVISIBLE(item))
1538 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1539 visible = TRUE;
1542 if (infoPtr->selectedItem != NULL
1543 && (item == infoPtr->selectedItem
1544 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1546 if (item->nextSibling)
1547 newSelection = item->nextSibling;
1548 else if (item->parent != infoPtr->root)
1549 newSelection = item->parent;
1550 else
1551 newSelection = item->prevSibling;
1552 TRACE("newSelection = %p\n", newSelection);
1555 if (infoPtr->firstVisible == item)
1557 if (item->nextSibling)
1558 newFirstVisible = item->nextSibling;
1559 else if (item->prevSibling)
1560 newFirstVisible = item->prevSibling;
1561 else if (item->parent != infoPtr->root)
1562 newFirstVisible = item->parent;
1563 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1565 else
1566 newFirstVisible = infoPtr->firstVisible;
1568 TREEVIEW_RemoveItem(infoPtr, item);
1571 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1572 if (!infoPtr->selectedItem && newSelection)
1574 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1575 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1578 /* Validate insertMark dropItem.
1579 * hotItem ??? - used for comparison only.
1581 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1582 infoPtr->insertMarkItem = 0;
1584 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1585 infoPtr->dropItem = 0;
1587 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1588 newFirstVisible = infoPtr->root->firstChild;
1590 TREEVIEW_VerifyTree(infoPtr);
1592 if (!infoPtr->bRedraw) return TRUE;
1594 if (visible)
1596 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1597 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1598 TREEVIEW_UpdateScrollBars(infoPtr);
1599 TREEVIEW_Invalidate(infoPtr, NULL);
1601 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1603 /* parent lost '+/-' - update it */
1604 TREEVIEW_Invalidate(infoPtr, parent);
1607 return TRUE;
1611 /* Get/Set Messages *********************************************************/
1612 static LRESULT
1613 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1615 infoPtr->bRedraw = wParam != 0;
1617 if (infoPtr->bRedraw)
1619 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1620 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1621 TREEVIEW_UpdateScrollBars(infoPtr);
1622 TREEVIEW_Invalidate(infoPtr, NULL);
1624 return 0;
1627 static LRESULT
1628 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1630 TRACE("\n");
1631 return infoPtr->uIndent;
1634 static LRESULT
1635 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1637 TRACE("\n");
1639 if (newIndent < MINIMUM_INDENT)
1640 newIndent = MINIMUM_INDENT;
1642 if (infoPtr->uIndent != newIndent)
1644 infoPtr->uIndent = newIndent;
1645 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1646 TREEVIEW_UpdateScrollBars(infoPtr);
1647 TREEVIEW_Invalidate(infoPtr, NULL);
1650 return 0;
1654 static LRESULT
1655 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1657 TRACE("\n");
1658 return (LRESULT)infoPtr->hwndToolTip;
1661 static LRESULT
1662 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1664 HWND prevToolTip;
1666 TRACE("\n");
1667 prevToolTip = infoPtr->hwndToolTip;
1668 infoPtr->hwndToolTip = hwndTT;
1670 return (LRESULT)prevToolTip;
1673 static LRESULT
1674 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1676 BOOL rc = infoPtr->bNtfUnicode;
1677 infoPtr->bNtfUnicode = fUnicode;
1678 return rc;
1681 static LRESULT
1682 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1684 return infoPtr->bNtfUnicode;
1687 static LRESULT
1688 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1690 return infoPtr->uScrollTime;
1693 static LRESULT
1694 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1696 UINT uOldScrollTime = infoPtr->uScrollTime;
1698 infoPtr->uScrollTime = min(uScrollTime, 100);
1700 return uOldScrollTime;
1704 static LRESULT
1705 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1707 TRACE("\n");
1709 switch (wParam)
1711 case TVSIL_NORMAL:
1712 return (LRESULT)infoPtr->himlNormal;
1714 case TVSIL_STATE:
1715 return (LRESULT)infoPtr->himlState;
1717 default:
1718 return 0;
1722 #define TVHEIGHT_MIN 16
1723 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1725 /* Compute the natural height for items. */
1726 static UINT
1727 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1729 TEXTMETRICW tm;
1730 HDC hdc = GetDC(0);
1731 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1732 UINT height;
1734 /* Height is the maximum of:
1735 * 16 (a hack because our fonts are tiny), and
1736 * The text height + border & margin, and
1737 * The size of the normal image list
1739 GetTextMetricsW(hdc, &tm);
1740 SelectObject(hdc, hOldFont);
1741 ReleaseDC(0, hdc);
1743 height = TVHEIGHT_MIN;
1744 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1745 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1746 if (height < infoPtr->normalImageHeight)
1747 height = infoPtr->normalImageHeight;
1749 /* Round down, unless we support odd ("non even") heights. */
1750 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1751 height &= ~1;
1753 return height;
1756 static LRESULT
1757 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1759 HIMAGELIST himlOld = 0;
1760 int oldWidth = infoPtr->normalImageWidth;
1761 int oldHeight = infoPtr->normalImageHeight;
1763 TRACE("%u,%p\n", type, himlNew);
1765 switch (type)
1767 case TVSIL_NORMAL:
1768 himlOld = infoPtr->himlNormal;
1769 infoPtr->himlNormal = himlNew;
1771 if (himlNew)
1772 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1773 &infoPtr->normalImageHeight);
1774 else
1776 infoPtr->normalImageWidth = 0;
1777 infoPtr->normalImageHeight = 0;
1780 break;
1782 case TVSIL_STATE:
1783 himlOld = infoPtr->himlState;
1784 infoPtr->himlState = himlNew;
1786 if (himlNew)
1787 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1788 &infoPtr->stateImageHeight);
1789 else
1791 infoPtr->stateImageWidth = 0;
1792 infoPtr->stateImageHeight = 0;
1795 break;
1797 default:
1798 ERR("unknown imagelist type %u\n", type);
1801 if (oldWidth != infoPtr->normalImageWidth ||
1802 oldHeight != infoPtr->normalImageHeight)
1804 BOOL bRecalcVisible = FALSE;
1806 if (oldHeight != infoPtr->normalImageHeight &&
1807 !infoPtr->bHeightSet)
1809 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1810 bRecalcVisible = TRUE;
1813 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1814 infoPtr->normalImageWidth != infoPtr->uIndent)
1816 infoPtr->uIndent = infoPtr->normalImageWidth;
1817 bRecalcVisible = TRUE;
1820 if (bRecalcVisible)
1821 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1823 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1824 TREEVIEW_UpdateScrollBars(infoPtr);
1827 TREEVIEW_Invalidate(infoPtr, NULL);
1829 return (LRESULT)himlOld;
1832 static LRESULT
1833 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1835 INT prevHeight = infoPtr->uItemHeight;
1837 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1838 if (newHeight == -1)
1840 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1841 infoPtr->bHeightSet = FALSE;
1843 else
1845 if (newHeight == 0) newHeight = 1;
1846 infoPtr->uItemHeight = newHeight;
1847 infoPtr->bHeightSet = TRUE;
1850 /* Round down, unless we support odd ("non even") heights. */
1851 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1853 infoPtr->uItemHeight &= ~1;
1854 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1857 if (infoPtr->uItemHeight != prevHeight)
1859 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1860 TREEVIEW_UpdateScrollBars(infoPtr);
1861 TREEVIEW_Invalidate(infoPtr, NULL);
1864 return prevHeight;
1867 static LRESULT
1868 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1870 TRACE("\n");
1871 return infoPtr->uItemHeight;
1875 static LRESULT
1876 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1878 TRACE("%p\n", infoPtr->hFont);
1879 return (LRESULT)infoPtr->hFont;
1883 static INT CALLBACK
1884 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1886 (void)unused;
1888 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1890 return 1;
1893 static LRESULT
1894 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1896 UINT uHeight = infoPtr->uItemHeight;
1898 TRACE("%p %i\n", hFont, bRedraw);
1900 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1902 DeleteObject(infoPtr->hBoldFont);
1903 DeleteObject(infoPtr->hUnderlineFont);
1904 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1905 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1907 if (!infoPtr->bHeightSet)
1908 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1910 if (uHeight != infoPtr->uItemHeight)
1911 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1913 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1915 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1916 TREEVIEW_UpdateScrollBars(infoPtr);
1918 if (bRedraw)
1919 TREEVIEW_Invalidate(infoPtr, NULL);
1921 return 0;
1925 static LRESULT
1926 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1928 TRACE("\n");
1929 return (LRESULT)infoPtr->clrLine;
1932 static LRESULT
1933 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1935 COLORREF prevColor = infoPtr->clrLine;
1937 TRACE("\n");
1938 infoPtr->clrLine = color;
1939 return (LRESULT)prevColor;
1943 static LRESULT
1944 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1946 TRACE("\n");
1947 return (LRESULT)infoPtr->clrText;
1950 static LRESULT
1951 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1953 COLORREF prevColor = infoPtr->clrText;
1955 TRACE("\n");
1956 infoPtr->clrText = color;
1958 if (infoPtr->clrText != prevColor)
1959 TREEVIEW_Invalidate(infoPtr, NULL);
1961 return (LRESULT)prevColor;
1965 static LRESULT
1966 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1968 TRACE("\n");
1969 return (LRESULT)infoPtr->clrBk;
1972 static LRESULT
1973 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1975 COLORREF prevColor = infoPtr->clrBk;
1977 TRACE("\n");
1978 infoPtr->clrBk = newColor;
1980 if (newColor != prevColor)
1981 TREEVIEW_Invalidate(infoPtr, NULL);
1983 return (LRESULT)prevColor;
1987 static LRESULT
1988 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1990 TRACE("\n");
1991 return (LRESULT)infoPtr->clrInsertMark;
1994 static LRESULT
1995 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1997 COLORREF prevColor = infoPtr->clrInsertMark;
1999 TRACE("%x\n", color);
2000 infoPtr->clrInsertMark = color;
2002 return (LRESULT)prevColor;
2006 static LRESULT
2007 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2009 TRACE("%d %p\n", wParam, item);
2011 if (!TREEVIEW_ValidItem(infoPtr, item))
2012 return 0;
2014 infoPtr->insertBeforeorAfter = wParam;
2015 infoPtr->insertMarkItem = item;
2017 TREEVIEW_Invalidate(infoPtr, NULL);
2019 return 1;
2023 /************************************************************************
2024 * Some serious braindamage here. lParam is a pointer to both the
2025 * input HTREEITEM and the output RECT.
2027 static LRESULT
2028 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2030 TREEVIEW_ITEM *item;
2031 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2033 TRACE("\n");
2035 if (pItem == NULL)
2036 return FALSE;
2038 item = *pItem;
2039 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2040 return FALSE;
2043 * If wParam is TRUE return the text size otherwise return
2044 * the whole item size
2046 if (fTextRect)
2048 /* Windows does not send TVN_GETDISPINFO here. */
2050 lpRect->top = item->rect.top;
2051 lpRect->bottom = item->rect.bottom;
2053 lpRect->left = item->textOffset;
2054 if (!item->textWidth)
2055 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2057 lpRect->right = item->textOffset + item->textWidth + 4;
2059 else
2061 *lpRect = item->rect;
2064 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2066 return TRUE;
2069 static inline LRESULT
2070 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2072 /* Surprise! This does not take integral height into account. */
2073 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2074 return infoPtr->clientHeight / infoPtr->uItemHeight;
2078 static LRESULT
2079 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2081 TREEVIEW_ITEM *item = tvItem->hItem;
2083 if (!TREEVIEW_ValidItem(infoPtr, item))
2085 if (!item) return FALSE;
2087 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2088 infoPtr = item->infoPtr;
2089 if (!TREEVIEW_ValidItem(infoPtr, item)) return FALSE;
2092 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2094 if (tvItem->mask & TVIF_CHILDREN)
2096 if (item->cChildren==I_CHILDRENCALLBACK)
2097 FIXME("I_CHILDRENCALLBACK not supported\n");
2098 tvItem->cChildren = item->cChildren;
2101 if (tvItem->mask & TVIF_HANDLE)
2102 tvItem->hItem = item;
2104 if (tvItem->mask & TVIF_IMAGE)
2105 tvItem->iImage = item->iImage;
2107 if (tvItem->mask & TVIF_INTEGRAL)
2108 tvItem->iIntegral = item->iIntegral;
2110 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2111 tvItem->lParam = item->lParam;
2113 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2114 tvItem->iSelectedImage = item->iSelectedImage;
2116 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2117 tvItem->iExpandedImage = item->iExpandedImage;
2119 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2120 tvItem->state = item->state;
2122 if (tvItem->mask & TVIF_TEXT)
2124 if (item->pszText == NULL)
2126 if (tvItem->cchTextMax > 0)
2127 tvItem->pszText[0] = '\0';
2129 else if (isW)
2131 if (item->pszText == LPSTR_TEXTCALLBACKW)
2133 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2134 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2136 else
2138 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2141 else
2143 if (item->pszText == LPSTR_TEXTCALLBACKW)
2145 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2146 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2148 else
2150 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2151 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2156 if (tvItem->mask & TVIF_STATEEX)
2158 FIXME("Extended item state not supported, returning 0.\n");
2159 tvItem->uStateEx = 0;
2162 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2163 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2165 return TRUE;
2168 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2169 * which is wrong. */
2170 static LRESULT
2171 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2173 TREEVIEW_ITEM *item;
2174 TREEVIEW_ITEM originalItem;
2176 item = tvItem->hItem;
2178 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2179 tvItem->mask);
2181 if (!TREEVIEW_ValidItem(infoPtr, item))
2182 return FALSE;
2184 /* store the original item values */
2185 originalItem = *item;
2187 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2188 return FALSE;
2190 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2191 if ((tvItem->mask & TVIF_TEXT
2192 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2193 && ISVISIBLE(item))
2195 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2196 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2199 if (tvItem->mask != 0 && ISVISIBLE(item))
2201 /* The refresh updates everything, but we can't wait until then. */
2202 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2204 /* if any of the item's values changed and it's not a callback, redraw the item */
2205 if (item_changed(&originalItem, item, tvItem))
2207 if (tvItem->mask & TVIF_INTEGRAL)
2209 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2210 TREEVIEW_UpdateScrollBars(infoPtr);
2212 TREEVIEW_Invalidate(infoPtr, NULL);
2214 else
2216 TREEVIEW_UpdateScrollBars(infoPtr);
2217 TREEVIEW_Invalidate(infoPtr, item);
2222 return TRUE;
2225 static LRESULT
2226 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2228 TRACE("\n");
2230 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2231 return 0;
2233 return (item->state & mask);
2236 static LRESULT
2237 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2239 TREEVIEW_ITEM *retval;
2241 retval = 0;
2243 /* handle all the global data here */
2244 switch (which)
2246 case TVGN_CHILD: /* Special case: child of 0 is root */
2247 if (item)
2248 break;
2249 /* fall through */
2250 case TVGN_ROOT:
2251 retval = infoPtr->root->firstChild;
2252 break;
2254 case TVGN_CARET:
2255 retval = infoPtr->selectedItem;
2256 break;
2258 case TVGN_FIRSTVISIBLE:
2259 retval = infoPtr->firstVisible;
2260 break;
2262 case TVGN_DROPHILITE:
2263 retval = infoPtr->dropItem;
2264 break;
2266 case TVGN_LASTVISIBLE:
2267 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2268 break;
2271 if (retval)
2273 TRACE("flags:%x, returns %p\n", which, retval);
2274 return (LRESULT)retval;
2277 if (item == TVI_ROOT) item = infoPtr->root;
2279 if (!TREEVIEW_ValidItem(infoPtr, item))
2280 return FALSE;
2282 switch (which)
2284 case TVGN_NEXT:
2285 retval = item->nextSibling;
2286 break;
2287 case TVGN_PREVIOUS:
2288 retval = item->prevSibling;
2289 break;
2290 case TVGN_PARENT:
2291 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2292 break;
2293 case TVGN_CHILD:
2294 retval = item->firstChild;
2295 break;
2296 case TVGN_NEXTVISIBLE:
2297 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2298 break;
2299 case TVGN_PREVIOUSVISIBLE:
2300 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2301 break;
2302 default:
2303 TRACE("Unknown msg %x,item %p\n", which, item);
2304 break;
2307 TRACE("flags:%x, item %p;returns %p\n", which, item, retval);
2308 return (LRESULT)retval;
2312 static LRESULT
2313 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2315 TRACE(" %d\n", infoPtr->uNumItems);
2316 return (LRESULT)infoPtr->uNumItems;
2319 static VOID
2320 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2322 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2324 static const unsigned int state_table[] = { 0, 2, 1 };
2326 unsigned int state;
2328 state = STATEIMAGEINDEX(item->state);
2329 TRACE("state:%x\n", state);
2330 item->state &= ~TVIS_STATEIMAGEMASK;
2332 if (state < 3)
2333 state = state_table[state];
2335 item->state |= INDEXTOSTATEIMAGEMASK(state);
2337 TRACE("state:%x\n", state);
2338 TREEVIEW_Invalidate(infoPtr, item);
2343 /* Painting *************************************************************/
2345 /* Draw the lines and expand button for an item. Also draws one section
2346 * of the line from item's parent to item's parent's next sibling. */
2347 static void
2348 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2350 LONG centerx, centery;
2351 BOOL lar = ((infoPtr->dwStyle
2352 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2353 > TVS_LINESATROOT);
2354 HBRUSH hbr, hbrOld;
2355 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2357 if (!lar && item->iLevel == 0)
2358 return;
2360 hbr = CreateSolidBrush(clrBk);
2361 hbrOld = SelectObject(hdc, hbr);
2363 centerx = (item->linesOffset + item->stateOffset) / 2;
2364 centery = (item->rect.top + item->rect.bottom) / 2;
2366 if (infoPtr->dwStyle & TVS_HASLINES)
2368 HPEN hOldPen, hNewPen;
2369 HTREEITEM parent;
2370 LOGBRUSH lb;
2372 /* Get a dotted grey pen */
2373 lb.lbStyle = BS_SOLID;
2374 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2375 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2376 hOldPen = SelectObject(hdc, hNewPen);
2378 /* Make sure the center is on a dot (using +2 instead
2379 * of +1 gives us pixel-by-pixel compat with native) */
2380 centery = (centery + 2) & ~1;
2382 MoveToEx(hdc, item->stateOffset, centery, NULL);
2383 LineTo(hdc, centerx - 1, centery);
2385 if (item->prevSibling || item->parent != infoPtr->root)
2387 MoveToEx(hdc, centerx, item->rect.top, NULL);
2388 LineTo(hdc, centerx, centery);
2391 if (item->nextSibling)
2393 MoveToEx(hdc, centerx, centery, NULL);
2394 LineTo(hdc, centerx, item->rect.bottom + 1);
2397 /* Draw the line from our parent to its next sibling. */
2398 parent = item->parent;
2399 while (parent != infoPtr->root)
2401 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2403 if (parent->nextSibling
2404 /* skip top-levels unless TVS_LINESATROOT */
2405 && parent->stateOffset > parent->linesOffset)
2407 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2408 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2411 parent = parent->parent;
2414 SelectObject(hdc, hOldPen);
2415 DeleteObject(hNewPen);
2419 * Display the (+/-) signs
2422 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2424 if (item->cChildren)
2426 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2427 if (theme)
2429 RECT glyphRect = item->rect;
2430 glyphRect.left = item->linesOffset;
2431 glyphRect.right = item->stateOffset;
2432 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2433 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2434 &glyphRect, NULL);
2436 else
2438 LONG height = item->rect.bottom - item->rect.top;
2439 LONG width = item->stateOffset - item->linesOffset;
2440 LONG rectsize = min(height, width) / 4;
2441 /* plussize = ceil(rectsize * 3/4) */
2442 LONG plussize = (rectsize + 1) * 3 / 4;
2444 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2445 HPEN old_pen = SelectObject(hdc, new_pen);
2447 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2448 centerx + rectsize + 2, centery + rectsize + 2);
2450 SelectObject(hdc, old_pen);
2451 DeleteObject(new_pen);
2453 /* draw +/- signs with current text color */
2454 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2455 old_pen = SelectObject(hdc, new_pen);
2457 if (height < 18 || width < 18)
2459 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2460 LineTo(hdc, centerx + plussize, centery);
2462 if (!(item->state & TVIS_EXPANDED) ||
2463 (item->state & TVIS_EXPANDPARTIAL))
2465 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2466 LineTo(hdc, centerx, centery + plussize);
2469 else
2471 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2472 centerx + plussize, centery + 2);
2474 if (!(item->state & TVIS_EXPANDED) ||
2475 (item->state & TVIS_EXPANDPARTIAL))
2477 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2478 centerx + 2, centery + plussize);
2479 SetPixel(hdc, centerx - 1, centery, clrBk);
2480 SetPixel(hdc, centerx + 1, centery, clrBk);
2484 SelectObject(hdc, old_pen);
2485 DeleteObject(new_pen);
2489 SelectObject(hdc, hbrOld);
2490 DeleteObject(hbr);
2493 static void
2494 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2496 INT cditem;
2497 HFONT hOldFont;
2498 COLORREF oldTextColor, oldTextBkColor;
2499 int centery;
2500 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2501 NMTVCUSTOMDRAW nmcdhdr;
2503 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2505 /* - If item is drop target or it is selected and window is in focus -
2506 * use blue background (COLOR_HIGHLIGHT).
2507 * - If item is selected, window is not in focus, but it has style
2508 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2509 * - Otherwise - use background color
2511 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2512 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2513 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2515 if ((item->state & TVIS_DROPHILITED) || inFocus)
2517 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2518 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2520 else
2522 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2523 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2526 else
2528 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2529 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2530 nmcdhdr.clrText = comctl32_color.clrHighlight;
2531 else
2532 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2535 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2537 /* The custom draw handler can query the text rectangle,
2538 * so get ready. */
2539 /* should already be known, set to 0 when changed */
2540 if (!item->textWidth)
2541 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2543 cditem = 0;
2545 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2547 cditem = TREEVIEW_SendCustomDrawItemNotify
2548 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2549 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2551 if (cditem & CDRF_SKIPDEFAULT)
2553 SelectObject(hdc, hOldFont);
2554 return;
2558 if (cditem & CDRF_NEWFONT)
2559 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2561 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2563 /* Set colors. Custom draw handler can change these so we do this after it. */
2564 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2565 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2567 centery = (item->rect.top + item->rect.bottom) / 2;
2570 * Display the images associated with this item
2573 INT imageIndex;
2575 /* State images are displayed to the left of the Normal image
2576 * image number is in state; zero should be `display no image'.
2578 imageIndex = STATEIMAGEINDEX(item->state);
2580 if (infoPtr->himlState && imageIndex)
2582 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2583 item->stateOffset,
2584 centery - infoPtr->stateImageHeight / 2,
2585 ILD_NORMAL);
2588 /* Now, draw the normal image; can be either selected,
2589 * non-selected or expanded image.
2592 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2594 /* The item is currently selected */
2595 imageIndex = item->iSelectedImage;
2597 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2599 /* The item is currently not selected but expanded */
2600 imageIndex = item->iExpandedImage;
2602 else
2604 /* The item is not selected and not expanded */
2605 imageIndex = item->iImage;
2608 if (infoPtr->himlNormal)
2610 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2612 style |= item->state & TVIS_OVERLAYMASK;
2614 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2615 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2616 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2617 style);
2623 * Display the text associated with this item
2626 /* Don't paint item's text if it's being edited */
2627 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2629 if (item->pszText)
2631 RECT rcText;
2632 UINT align;
2633 SIZE sz;
2635 rcText.top = item->rect.top;
2636 rcText.bottom = item->rect.bottom;
2637 rcText.left = item->textOffset;
2638 rcText.right = rcText.left + item->textWidth + 4;
2640 TRACE("drawing text %s at (%s)\n",
2641 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2643 /* Draw it */
2644 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2646 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2647 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2648 ETO_CLIPPED | ETO_OPAQUE,
2649 &rcText,
2650 item->pszText,
2651 lstrlenW(item->pszText),
2652 NULL);
2653 SetTextAlign(hdc, align);
2655 /* Draw focus box around the selected item */
2656 if ((item == infoPtr->selectedItem) && inFocus)
2658 DrawFocusRect(hdc,&rcText);
2663 /* Draw insertion mark if necessary */
2665 if (infoPtr->insertMarkItem)
2666 TRACE("item:%d,mark:%p\n",
2667 TREEVIEW_GetItemIndex(infoPtr, item),
2668 infoPtr->insertMarkItem);
2670 if (item == infoPtr->insertMarkItem)
2672 HPEN hNewPen, hOldPen;
2673 int offset;
2674 int left, right;
2676 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2677 hOldPen = SelectObject(hdc, hNewPen);
2679 if (infoPtr->insertBeforeorAfter)
2680 offset = item->rect.bottom - 1;
2681 else
2682 offset = item->rect.top + 1;
2684 left = item->textOffset - 2;
2685 right = item->textOffset + item->textWidth + 2;
2687 MoveToEx(hdc, left, offset - 3, NULL);
2688 LineTo(hdc, left, offset + 4);
2690 MoveToEx(hdc, left, offset, NULL);
2691 LineTo(hdc, right + 1, offset);
2693 MoveToEx(hdc, right, offset + 3, NULL);
2694 LineTo(hdc, right, offset - 4);
2696 SelectObject(hdc, hOldPen);
2697 DeleteObject(hNewPen);
2700 if (cditem & CDRF_NOTIFYPOSTPAINT)
2702 cditem = TREEVIEW_SendCustomDrawItemNotify
2703 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2704 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2707 /* Restore the hdc state */
2708 SetTextColor(hdc, oldTextColor);
2709 SetBkColor(hdc, oldTextBkColor);
2710 SelectObject(hdc, hOldFont);
2713 /* Computes treeHeight and treeWidth and updates the scroll bars.
2715 static void
2716 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2718 TREEVIEW_ITEM *item;
2719 HWND hwnd = infoPtr->hwnd;
2720 BOOL vert = FALSE;
2721 BOOL horz = FALSE;
2722 SCROLLINFO si;
2723 LONG scrollX = infoPtr->scrollX;
2725 infoPtr->treeWidth = 0;
2726 infoPtr->treeHeight = 0;
2728 /* We iterate through all visible items in order to get the tree height
2729 * and width */
2730 item = infoPtr->root->firstChild;
2732 while (item != NULL)
2734 if (ISVISIBLE(item))
2736 /* actually we draw text at textOffset + 2 */
2737 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2738 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2740 /* This is scroll-adjusted, but we fix this below. */
2741 infoPtr->treeHeight = item->rect.bottom;
2744 item = TREEVIEW_GetNextListItem(infoPtr, item);
2747 /* Fix the scroll adjusted treeHeight and treeWidth. */
2748 if (infoPtr->root->firstChild)
2749 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2751 infoPtr->treeWidth += infoPtr->scrollX;
2753 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2755 /* Adding one scroll bar may take up enough space that it forces us
2756 * to add the other as well. */
2757 if (infoPtr->treeHeight > infoPtr->clientHeight)
2759 vert = TRUE;
2761 if (infoPtr->treeWidth
2762 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2763 horz = TRUE;
2765 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2766 horz = TRUE;
2768 if (!vert && horz && infoPtr->treeHeight
2769 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2770 vert = TRUE;
2772 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2774 si.cbSize = sizeof(SCROLLINFO);
2775 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2776 si.nMin = 0;
2778 if (vert)
2780 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2781 if ( si.nPage && NULL != infoPtr->firstVisible)
2783 si.nPos = infoPtr->firstVisible->visibleOrder;
2784 si.nMax = infoPtr->maxVisibleOrder - 1;
2786 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2788 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2789 ShowScrollBar(hwnd, SB_VERT, TRUE);
2790 infoPtr->uInternalStatus |= TV_VSCROLL;
2792 else
2794 if (infoPtr->uInternalStatus & TV_VSCROLL)
2795 ShowScrollBar(hwnd, SB_VERT, FALSE);
2796 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2799 else
2801 if (infoPtr->uInternalStatus & TV_VSCROLL)
2802 ShowScrollBar(hwnd, SB_VERT, FALSE);
2803 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2806 if (horz)
2808 si.nPage = infoPtr->clientWidth;
2809 si.nPos = infoPtr->scrollX;
2810 si.nMax = infoPtr->treeWidth - 1;
2812 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2814 si.nPos = si.nMax - max( si.nPage-1, 0 );
2815 scrollX = si.nPos;
2818 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2819 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2820 infoPtr->uInternalStatus |= TV_HSCROLL;
2822 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2823 TREEVIEW_HScroll(infoPtr,
2824 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2826 else
2828 if (infoPtr->uInternalStatus & TV_HSCROLL)
2829 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2830 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2832 scrollX = 0;
2833 if (infoPtr->scrollX != 0)
2835 TREEVIEW_HScroll(infoPtr,
2836 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2840 if (!horz)
2841 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2844 static void
2845 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2847 HBRUSH hBrush;
2848 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2850 hBrush = CreateSolidBrush(clrBk);
2851 FillRect(hdc, rc, hBrush);
2852 DeleteObject(hBrush);
2855 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2856 static LRESULT
2857 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2859 RECT rect;
2861 TRACE("%p\n", infoPtr);
2863 GetClientRect(infoPtr->hwnd, &rect);
2864 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2866 return 1;
2869 static void
2870 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2872 HWND hwnd = infoPtr->hwnd;
2873 RECT rect = *rc;
2874 TREEVIEW_ITEM *item;
2876 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2878 TRACE("empty window\n");
2879 return;
2882 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2883 hdc, rect);
2885 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2887 ReleaseDC(hwnd, hdc);
2888 return;
2891 for (item = infoPtr->root->firstChild;
2892 item != NULL;
2893 item = TREEVIEW_GetNextListItem(infoPtr, item))
2895 if (ISVISIBLE(item))
2897 /* Avoid unneeded calculations */
2898 if (item->rect.top > rect.bottom)
2899 break;
2900 if (item->rect.bottom < rect.top)
2901 continue;
2903 TREEVIEW_DrawItem(infoPtr, hdc, item);
2907 TREEVIEW_UpdateScrollBars(infoPtr);
2909 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2910 infoPtr->cdmode =
2911 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2914 static inline void
2915 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2917 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2920 static void
2921 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2923 if (item)
2924 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2925 else
2926 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2929 static LRESULT
2930 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2932 HDC hdc;
2933 PAINTSTRUCT ps;
2934 RECT rc;
2936 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2938 if (hdc_ref)
2940 hdc = hdc_ref;
2941 GetClientRect(infoPtr->hwnd, &rc);
2942 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2944 else
2946 hdc = BeginPaint(infoPtr->hwnd, &ps);
2947 rc = ps.rcPaint;
2948 if(ps.fErase)
2949 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2952 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2953 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2955 if (!hdc_ref)
2956 EndPaint(infoPtr->hwnd, &ps);
2958 return 0;
2961 static LRESULT
2962 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2964 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2966 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2967 return 0;
2969 if (options & PRF_ERASEBKGND)
2970 TREEVIEW_EraseBackground(infoPtr, hdc);
2972 if (options & PRF_CLIENT)
2974 RECT rc;
2975 GetClientRect(infoPtr->hwnd, &rc);
2976 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2979 return 0;
2982 /* Sorting **************************************************************/
2984 /***************************************************************************
2985 * Forward the DPA local callback to the treeview owner callback
2987 static INT WINAPI
2988 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2989 const TVSORTCB *pCallBackSort)
2991 /* Forward the call to the client-defined callback */
2992 return pCallBackSort->lpfnCompare(first->lParam,
2993 second->lParam,
2994 pCallBackSort->lParam);
2997 /***************************************************************************
2998 * Treeview native sort routine: sort on item text.
3000 static INT WINAPI
3001 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3002 const TREEVIEW_INFO *infoPtr)
3004 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3005 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3007 if(first->pszText && second->pszText)
3008 return lstrcmpiW(first->pszText, second->pszText);
3009 else if(first->pszText)
3010 return -1;
3011 else if(second->pszText)
3012 return 1;
3013 else
3014 return 0;
3017 /* Returns the number of physical children belonging to item. */
3018 static INT
3019 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3021 INT cChildren = 0;
3022 HTREEITEM hti;
3024 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3025 cChildren++;
3027 return cChildren;
3030 /* Returns a DPA containing a pointer to each physical child of item in
3031 * sibling order. If item has no children, an empty DPA is returned. */
3032 static HDPA
3033 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3035 HTREEITEM child;
3037 HDPA list = DPA_Create(8);
3038 if (list == 0) return NULL;
3040 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3042 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3044 DPA_Destroy(list);
3045 return NULL;
3049 return list;
3052 /***************************************************************************
3053 * Setup the treeview structure with regards of the sort method
3054 * and sort the children of the TV item specified in lParam
3055 * fRecurse: currently unused. Should be zero.
3056 * parent: if pSort!=NULL, should equal pSort->hParent.
3057 * otherwise, item which child items are to be sorted.
3058 * pSort: sort method info. if NULL, sort on item text.
3059 * if non-NULL, sort on item's lParam content, and let the
3060 * application decide what that means. See also TVM_SORTCHILDRENCB.
3063 static LRESULT
3064 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3065 LPTVSORTCB pSort)
3067 INT cChildren;
3068 PFNDPACOMPARE pfnCompare;
3069 LPARAM lpCompare;
3071 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3072 if (parent == TVI_ROOT || parent == NULL)
3073 parent = infoPtr->root;
3075 /* Check for a valid handle to the parent item */
3076 if (!TREEVIEW_ValidItem(infoPtr, parent))
3078 ERR("invalid item hParent=%p\n", parent);
3079 return FALSE;
3082 if (pSort)
3084 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3085 lpCompare = (LPARAM)pSort;
3087 else
3089 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3090 lpCompare = (LPARAM)infoPtr;
3093 cChildren = TREEVIEW_CountChildren(parent);
3095 /* Make sure there is something to sort */
3096 if (cChildren > 1)
3098 /* TREEVIEW_ITEM rechaining */
3099 INT count = 0;
3100 HTREEITEM item = 0;
3101 HTREEITEM nextItem = 0;
3102 HTREEITEM prevItem = 0;
3104 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3106 if (sortList == NULL)
3107 return FALSE;
3109 /* let DPA sort the list */
3110 DPA_Sort(sortList, pfnCompare, lpCompare);
3112 /* The order of DPA entries has been changed, so fixup the
3113 * nextSibling and prevSibling pointers. */
3115 item = DPA_GetPtr(sortList, count++);
3116 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3118 /* link the two current item together */
3119 item->nextSibling = nextItem;
3120 nextItem->prevSibling = item;
3122 if (prevItem == NULL)
3124 /* this is the first item, update the parent */
3125 parent->firstChild = item;
3126 item->prevSibling = NULL;
3128 else
3130 /* fix the back chaining */
3131 item->prevSibling = prevItem;
3134 /* get ready for the next one */
3135 prevItem = item;
3136 item = nextItem;
3139 /* the last item is pointed to by item and never has a sibling */
3140 item->nextSibling = NULL;
3141 parent->lastChild = item;
3143 DPA_Destroy(sortList);
3145 TREEVIEW_VerifyTree(infoPtr);
3147 if (parent->state & TVIS_EXPANDED)
3149 int visOrder = infoPtr->firstVisible->visibleOrder;
3151 if (parent == infoPtr->root)
3152 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3153 else
3154 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3156 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3158 TREEVIEW_ITEM *item;
3160 for (item = infoPtr->root->firstChild; item != NULL;
3161 item = TREEVIEW_GetNextListItem(infoPtr, item))
3163 if (item->visibleOrder == visOrder)
3164 break;
3167 if (!item) item = parent->firstChild;
3168 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3171 TREEVIEW_Invalidate(infoPtr, NULL);
3174 return TRUE;
3176 return FALSE;
3180 /***************************************************************************
3181 * Setup the treeview structure with regards of the sort method
3182 * and sort the children of the TV item specified in lParam
3184 static LRESULT
3185 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3187 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3191 /***************************************************************************
3192 * Sort the children of the TV item specified in lParam.
3194 static LRESULT
3195 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3197 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3201 /* Expansion/Collapse ***************************************************/
3203 static BOOL
3204 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3205 UINT action)
3207 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3208 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3209 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3210 0, item);
3213 static VOID
3214 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3215 UINT action)
3217 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3218 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3219 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3220 0, item);
3224 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3225 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3226 static BOOL
3227 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3228 BOOL bRemoveChildren, BOOL bUser)
3230 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3231 BOOL bSetSelection, bSetFirstVisible;
3232 RECT scrollRect;
3233 LONG scrollDist = 0;
3234 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3236 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3238 if (!(item->state & TVIS_EXPANDED))
3239 return FALSE;
3241 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3242 TREEVIEW_SendExpanding(infoPtr, item, action);
3244 if (item->firstChild == NULL)
3245 return FALSE;
3247 item->state &= ~TVIS_EXPANDED;
3249 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3250 TREEVIEW_SendExpanded(infoPtr, item, action);
3252 bSetSelection = (infoPtr->selectedItem != NULL
3253 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3255 bSetFirstVisible = (infoPtr->firstVisible != NULL
3256 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3258 tmpItem = item;
3259 while (tmpItem)
3261 if (tmpItem->nextSibling)
3263 nextItem = tmpItem->nextSibling;
3264 break;
3266 tmpItem = tmpItem->parent;
3269 if (nextItem)
3270 scrollDist = nextItem->rect.top;
3272 if (bRemoveChildren)
3274 INT old_cChildren = item->cChildren;
3275 TRACE("TVE_COLLAPSERESET\n");
3276 item->state &= ~TVIS_EXPANDEDONCE;
3277 TREEVIEW_RemoveAllChildren(infoPtr, item);
3278 item->cChildren = old_cChildren;
3281 if (item->firstChild)
3283 TREEVIEW_ITEM *i, *sibling;
3285 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3287 for (i = item->firstChild; i != sibling;
3288 i = TREEVIEW_GetNextListItem(infoPtr, i))
3290 i->visibleOrder = -1;
3294 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3296 if (nextItem)
3297 scrollDist = -(scrollDist - nextItem->rect.top);
3299 if (bSetSelection)
3301 /* Don't call DoSelectItem, it sends notifications. */
3302 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3303 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3304 item->state |= TVIS_SELECTED;
3305 infoPtr->selectedItem = item;
3308 TREEVIEW_UpdateScrollBars(infoPtr);
3310 scrollRect.left = 0;
3311 scrollRect.right = infoPtr->clientWidth;
3312 scrollRect.bottom = infoPtr->clientHeight;
3314 if (nextItem)
3316 scrollRect.top = nextItem->rect.top;
3318 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3319 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3320 TREEVIEW_Invalidate(infoPtr, item);
3321 } else {
3322 scrollRect.top = item->rect.top;
3323 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3326 TREEVIEW_SetFirstVisible(infoPtr,
3327 bSetFirstVisible ? item : infoPtr->firstVisible,
3328 TRUE);
3330 return TRUE;
3333 static BOOL
3334 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3335 BOOL partial, BOOL user)
3337 LONG scrollDist;
3338 LONG orgNextTop = 0;
3339 RECT scrollRect;
3340 TREEVIEW_ITEM *nextItem, *tmpItem;
3341 BOOL sendsNotifications;
3343 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, item, partial, user);
3345 if (item->state & TVIS_EXPANDED)
3346 return TRUE;
3348 tmpItem = item; nextItem = NULL;
3349 while (tmpItem)
3351 if (tmpItem->nextSibling)
3353 nextItem = tmpItem->nextSibling;
3354 break;
3356 tmpItem = tmpItem->parent;
3359 if (nextItem)
3360 orgNextTop = nextItem->rect.top;
3362 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3364 sendsNotifications = user || ((item->cChildren != 0) &&
3365 !(item->state & TVIS_EXPANDEDONCE));
3366 if (sendsNotifications)
3368 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3370 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3371 return FALSE;
3374 if (!item->firstChild)
3375 return FALSE;
3377 item->state |= TVIS_EXPANDED;
3379 if (partial)
3380 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3382 if (ISVISIBLE(item))
3384 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3385 TREEVIEW_UpdateSubTree(infoPtr, item);
3386 TREEVIEW_UpdateScrollBars(infoPtr);
3388 scrollRect.left = 0;
3389 scrollRect.bottom = infoPtr->treeHeight;
3390 scrollRect.right = infoPtr->clientWidth;
3391 if (nextItem)
3393 scrollDist = nextItem->rect.top - orgNextTop;
3394 scrollRect.top = orgNextTop;
3396 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3397 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3398 TREEVIEW_Invalidate (infoPtr, item);
3399 } else {
3400 scrollRect.top = item->rect.top;
3401 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3404 /* Scroll up so that as many children as possible are visible.
3405 * This fails when expanding causes an HScroll bar to appear, but we
3406 * don't know that yet, so the last item is obscured. */
3407 if (item->firstChild != NULL)
3409 int nChildren = item->lastChild->visibleOrder
3410 - item->firstChild->visibleOrder + 1;
3412 int visible_pos = item->visibleOrder
3413 - infoPtr->firstVisible->visibleOrder;
3415 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3417 if (visible_pos > 0 && nChildren > rows_below)
3419 int scroll = nChildren - rows_below;
3421 if (scroll > visible_pos)
3422 scroll = visible_pos;
3424 if (scroll > 0)
3426 TREEVIEW_ITEM *newFirstVisible
3427 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3428 scroll);
3431 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3437 if (sendsNotifications) {
3438 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3439 item->state |= TVIS_EXPANDEDONCE;
3442 return TRUE;
3445 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3446 to mouse messages and TVM_SELECTITEM.
3448 selection - previously selected item, used to collapse a part of a tree
3449 item - new selected item
3451 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3452 HTREEITEM selection, HTREEITEM item)
3454 TREEVIEW_ITEM *SelItem;
3456 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3458 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3461 * Close the previous selection all the way to the root
3462 * as long as the new selection is not a child
3464 if(selection && (selection != item))
3466 BOOL closeit = TRUE;
3467 SelItem = item;
3469 /* determine if the hitItem is a child of the currently selected item */
3470 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3471 (SelItem->parent != infoPtr->root))
3473 closeit = (SelItem != selection);
3474 SelItem = SelItem->parent;
3477 if(closeit)
3479 if(TREEVIEW_ValidItem(infoPtr, selection))
3480 SelItem = selection;
3482 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3483 SelItem->parent != infoPtr->root)
3485 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3486 SelItem = SelItem->parent;
3492 * Expand the current item
3494 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3497 static BOOL
3498 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3500 TRACE("item=%p, user=%d\n", item, user);
3502 if (item->state & TVIS_EXPANDED)
3503 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3504 else
3505 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3508 static VOID
3509 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3511 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3513 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3515 if (TREEVIEW_HasChildren(infoPtr, item))
3516 TREEVIEW_ExpandAll(infoPtr, item);
3520 /* Note:If the specified item is the child of a collapsed parent item,
3521 the parent's list of child items is (recursively) expanded to reveal the
3522 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3523 know if it also applies here.
3526 static LRESULT
3527 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3529 if (!TREEVIEW_ValidItem(infoPtr, item))
3530 return 0;
3532 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3533 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3534 flag, item->state);
3536 switch (flag & TVE_TOGGLE)
3538 case TVE_COLLAPSE:
3539 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3540 FALSE);
3542 case TVE_EXPAND:
3543 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3544 FALSE);
3546 case TVE_TOGGLE:
3547 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3549 default:
3550 return 0;
3554 /* Hit-Testing **********************************************************/
3556 static TREEVIEW_ITEM *
3557 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3559 TREEVIEW_ITEM *item;
3560 LONG row;
3562 if (!infoPtr->firstVisible)
3563 return NULL;
3565 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3567 for (item = infoPtr->firstVisible; item != NULL;
3568 item = TREEVIEW_GetNextListItem(infoPtr, item))
3570 if (row >= item->visibleOrder
3571 && row < item->visibleOrder + item->iIntegral)
3572 break;
3575 return item;
3578 static LRESULT
3579 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3581 TREEVIEW_ITEM *item;
3582 RECT rect;
3583 UINT status;
3584 LONG x, y;
3586 lpht->hItem = 0;
3587 GetClientRect(infoPtr->hwnd, &rect);
3588 status = 0;
3589 x = lpht->pt.x;
3590 y = lpht->pt.y;
3592 if (x < rect.left)
3594 status |= TVHT_TOLEFT;
3596 else if (x > rect.right)
3598 status |= TVHT_TORIGHT;
3601 if (y < rect.top)
3603 status |= TVHT_ABOVE;
3605 else if (y > rect.bottom)
3607 status |= TVHT_BELOW;
3610 if (status)
3612 lpht->flags = status;
3613 return 0;
3616 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3617 if (!item)
3619 lpht->flags = TVHT_NOWHERE;
3620 return 0;
3623 if (x >= item->textOffset + item->textWidth)
3625 lpht->flags = TVHT_ONITEMRIGHT;
3627 else if (x >= item->textOffset)
3629 lpht->flags = TVHT_ONITEMLABEL;
3631 else if (x >= item->imageOffset)
3633 lpht->flags = TVHT_ONITEMICON;
3635 else if (x >= item->stateOffset)
3637 lpht->flags = TVHT_ONITEMSTATEICON;
3639 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3641 lpht->flags = TVHT_ONITEMBUTTON;
3643 else
3645 lpht->flags = TVHT_ONITEMINDENT;
3648 lpht->hItem = item;
3649 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3651 return (LRESULT)item;
3654 /* Item Label Editing ***************************************************/
3656 static LRESULT
3657 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3659 return (LRESULT)infoPtr->hwndEdit;
3662 static LRESULT CALLBACK
3663 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3665 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3666 BOOL bCancel = FALSE;
3667 LRESULT rc;
3669 switch (uMsg)
3671 case WM_PAINT:
3672 TRACE("WM_PAINT start\n");
3673 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3674 lParam);
3675 TRACE("WM_PAINT done\n");
3676 return rc;
3678 case WM_KILLFOCUS:
3679 if (infoPtr->bIgnoreEditKillFocus)
3680 return TRUE;
3681 break;
3683 case WM_DESTROY:
3685 WNDPROC editProc = infoPtr->wpEditOrig;
3686 infoPtr->wpEditOrig = 0;
3687 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3688 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3691 case WM_GETDLGCODE:
3692 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3694 case WM_KEYDOWN:
3695 if (wParam == VK_ESCAPE)
3697 bCancel = TRUE;
3698 break;
3700 else if (wParam == VK_RETURN)
3702 break;
3705 /* fall through */
3706 default:
3707 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3710 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3711 /* eg. Using a messagebox */
3713 infoPtr->bIgnoreEditKillFocus = TRUE;
3714 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3715 infoPtr->bIgnoreEditKillFocus = FALSE;
3717 return 0;
3721 /* should handle edit control messages here */
3723 static LRESULT
3724 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3726 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3728 switch (HIWORD(wParam))
3730 case EN_UPDATE:
3733 * Adjust the edit window size
3735 WCHAR buffer[1024];
3736 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3737 HDC hdc = GetDC(infoPtr->hwndEdit);
3738 SIZE sz;
3739 HFONT hFont, hOldFont = 0;
3741 TRACE("edit=%p\n", infoPtr->hwndEdit);
3743 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3745 infoPtr->bLabelChanged = TRUE;
3747 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3749 /* Select font to get the right dimension of the string */
3750 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3752 if (hFont != 0)
3754 hOldFont = SelectObject(hdc, hFont);
3757 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3759 TEXTMETRICW textMetric;
3761 /* Add Extra spacing for the next character */
3762 GetTextMetricsW(hdc, &textMetric);
3763 sz.cx += (textMetric.tmMaxCharWidth * 2);
3765 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3766 sz.cx = min(sz.cx,
3767 infoPtr->clientWidth - editItem->textOffset + 2);
3769 SetWindowPos(infoPtr->hwndEdit,
3770 HWND_TOP,
3773 sz.cx,
3774 editItem->rect.bottom - editItem->rect.top + 3,
3775 SWP_NOMOVE | SWP_DRAWFRAME);
3778 if (hFont != 0)
3780 SelectObject(hdc, hOldFont);
3783 ReleaseDC(infoPtr->hwnd, hdc);
3784 break;
3786 case EN_KILLFOCUS:
3787 /* apparently we should respect passed handle value */
3788 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3790 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3791 break;
3793 default:
3794 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3797 return 0;
3800 static HWND
3801 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3803 HWND hwnd = infoPtr->hwnd;
3804 HWND hwndEdit;
3805 SIZE sz;
3806 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3807 HDC hdc;
3808 HFONT hOldFont=0;
3809 TEXTMETRICW textMetric;
3811 TRACE("%p %p\n", hwnd, hItem);
3812 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3813 return NULL;
3815 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3816 return NULL;
3818 if (infoPtr->hwndEdit)
3819 return infoPtr->hwndEdit;
3821 infoPtr->bLabelChanged = FALSE;
3823 /* make edit item visible */
3824 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3826 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3828 hdc = GetDC(hwnd);
3829 /* Select the font to get appropriate metric dimensions */
3830 if (infoPtr->hFont != 0)
3832 hOldFont = SelectObject(hdc, infoPtr->hFont);
3835 /* Get string length in pixels */
3836 if (hItem->pszText)
3837 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3838 &sz);
3839 else
3840 GetTextExtentPoint32A(hdc, "", 0, &sz);
3842 /* Add Extra spacing for the next character */
3843 GetTextMetricsW(hdc, &textMetric);
3844 sz.cx += (textMetric.tmMaxCharWidth * 2);
3846 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3847 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3849 if (infoPtr->hFont != 0)
3851 SelectObject(hdc, hOldFont);
3854 ReleaseDC(hwnd, hdc);
3856 infoPtr->editItem = hItem;
3858 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3859 WC_EDITW,
3861 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3862 WS_CLIPSIBLINGS | ES_WANTRETURN |
3863 ES_LEFT, hItem->textOffset - 2,
3864 hItem->rect.top - 1, sz.cx + 3,
3865 hItem->rect.bottom -
3866 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3867 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3869 infoPtr->hwndEdit = hwndEdit;
3871 /* Get a 2D border. */
3872 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3873 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3874 SetWindowLongW(hwndEdit, GWL_STYLE,
3875 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3877 SendMessageW(hwndEdit, WM_SETFONT,
3878 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3880 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3881 (DWORD_PTR)
3882 TREEVIEW_Edit_SubclassProc);
3883 if (hItem->pszText)
3884 SetWindowTextW(hwndEdit, hItem->pszText);
3886 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3888 DestroyWindow(hwndEdit);
3889 infoPtr->hwndEdit = 0;
3890 infoPtr->editItem = NULL;
3891 return NULL;
3894 SetFocus(hwndEdit);
3895 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3896 ShowWindow(hwndEdit, SW_SHOW);
3898 return hwndEdit;
3902 static LRESULT
3903 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3905 HWND hwnd = infoPtr->hwnd;
3906 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3907 NMTVDISPINFOW tvdi;
3908 BOOL bCommit;
3909 WCHAR tmpText[1024] = { '\0' };
3910 WCHAR *newText = tmpText;
3911 int iLength = 0;
3913 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3915 tvdi.hdr.hwndFrom = hwnd;
3916 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3917 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3918 tvdi.item.mask = 0;
3919 tvdi.item.hItem = editedItem;
3920 tvdi.item.state = editedItem->state;
3921 tvdi.item.lParam = editedItem->lParam;
3923 if (!bCancel)
3925 if (!infoPtr->bNtfUnicode)
3926 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3927 else
3928 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3930 if (iLength >= 1023)
3932 ERR("Insufficient space to retrieve new item label\n");
3935 tvdi.item.mask = TVIF_TEXT;
3936 tvdi.item.pszText = tmpText;
3937 tvdi.item.cchTextMax = iLength + 1;
3939 else
3941 tvdi.item.pszText = NULL;
3942 tvdi.item.cchTextMax = 0;
3945 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
3947 if (!bCancel && bCommit) /* Apply the changes */
3949 if (!infoPtr->bNtfUnicode)
3951 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3952 newText = Alloc(len * sizeof(WCHAR));
3953 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3954 iLength = len - 1;
3957 if (strcmpW(newText, editedItem->pszText) != 0)
3959 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3960 if (ptr == NULL)
3962 ERR("OutOfMemory, cannot allocate space for label\n");
3963 if(newText != tmpText) Free(newText);
3964 DestroyWindow(infoPtr->hwndEdit);
3965 infoPtr->hwndEdit = 0;
3966 infoPtr->editItem = NULL;
3967 return FALSE;
3969 else
3971 editedItem->pszText = ptr;
3972 editedItem->cchTextMax = iLength + 1;
3973 strcpyW(editedItem->pszText, newText);
3974 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3977 if(newText != tmpText) Free(newText);
3980 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3981 DestroyWindow(infoPtr->hwndEdit);
3982 infoPtr->hwndEdit = 0;
3983 infoPtr->editItem = NULL;
3984 return TRUE;
3987 static LRESULT
3988 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3990 if (wParam != TV_EDIT_TIMER)
3992 ERR("got unknown timer\n");
3993 return 1;
3996 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3997 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3999 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4001 return 0;
4005 /* Mouse Tracking/Drag **************************************************/
4007 /***************************************************************************
4008 * This is quite unusual piece of code, but that's how it's implemented in
4009 * Windows.
4011 static LRESULT
4012 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4014 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4015 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4016 RECT r;
4017 MSG msg;
4019 r.top = pt.y - cyDrag;
4020 r.left = pt.x - cxDrag;
4021 r.bottom = pt.y + cyDrag;
4022 r.right = pt.x + cxDrag;
4024 SetCapture(infoPtr->hwnd);
4026 while (1)
4028 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4030 if (msg.message == WM_MOUSEMOVE)
4032 pt.x = (short)LOWORD(msg.lParam);
4033 pt.y = (short)HIWORD(msg.lParam);
4034 if (PtInRect(&r, pt))
4035 continue;
4036 else
4038 ReleaseCapture();
4039 return 1;
4042 else if (msg.message >= WM_LBUTTONDOWN &&
4043 msg.message <= WM_RBUTTONDBLCLK)
4045 break;
4048 DispatchMessageW(&msg);
4051 if (GetCapture() != infoPtr->hwnd)
4052 return 0;
4055 ReleaseCapture();
4056 return 0;
4060 static LRESULT
4061 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4063 TREEVIEW_ITEM *item;
4064 TVHITTESTINFO hit;
4066 TRACE("\n");
4067 SetFocus(infoPtr->hwnd);
4069 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4071 /* If there is pending 'edit label' event - kill it now */
4072 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4075 hit.pt.x = (short)LOWORD(lParam);
4076 hit.pt.y = (short)HIWORD(lParam);
4078 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4079 if (!item)
4080 return 0;
4081 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4083 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4084 { /* FIXME! */
4085 switch (hit.flags)
4087 case TVHT_ONITEMRIGHT:
4088 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4089 break;
4091 case TVHT_ONITEMINDENT:
4092 if (!(infoPtr->dwStyle & TVS_HASLINES))
4094 break;
4096 else
4098 int level = hit.pt.x / infoPtr->uIndent;
4099 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4101 while (item->iLevel > level)
4103 item = item->parent;
4106 /* fall through */
4109 case TVHT_ONITEMLABEL:
4110 case TVHT_ONITEMICON:
4111 case TVHT_ONITEMBUTTON:
4112 TREEVIEW_Toggle(infoPtr, item, TRUE);
4113 break;
4115 case TVHT_ONITEMSTATEICON:
4116 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4117 TREEVIEW_ToggleItemState(infoPtr, item);
4118 else
4119 TREEVIEW_Toggle(infoPtr, item, TRUE);
4120 break;
4123 return TRUE;
4127 static LRESULT
4128 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4130 HWND hwnd = infoPtr->hwnd;
4131 TVHITTESTINFO ht;
4132 BOOL bTrack, bDoLabelEdit;
4134 /* If Edit control is active - kill it and return.
4135 * The best way to do it is to set focus to itself.
4136 * Edit control subclassed procedure will automatically call
4137 * EndEditLabelNow.
4139 if (infoPtr->hwndEdit)
4141 SetFocus(hwnd);
4142 return 0;
4145 ht.pt.x = (short)LOWORD(lParam);
4146 ht.pt.y = (short)HIWORD(lParam);
4148 TREEVIEW_HitTest(infoPtr, &ht);
4149 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4151 /* update focusedItem and redraw both items */
4152 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4154 infoPtr->focusedItem = ht.hItem;
4155 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4156 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4159 bTrack = (ht.flags & TVHT_ONITEM)
4160 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4163 * If the style allows editing and the node is already selected
4164 * and the click occurred on the item label...
4166 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4167 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4169 /* Send NM_CLICK right away */
4170 if (!bTrack)
4171 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4172 goto setfocus;
4174 if (ht.flags & TVHT_ONITEMBUTTON)
4176 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4177 goto setfocus;
4179 else if (bTrack)
4180 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4181 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4183 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4184 infoPtr->dropItem = ht.hItem;
4186 /* clean up focusedItem as we dragged and won't select this item */
4187 if(infoPtr->focusedItem)
4189 /* refresh the item that was focused */
4190 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4191 infoPtr->focusedItem = NULL;
4193 /* refresh the selected item to return the filled background */
4194 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4197 return 0;
4201 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4202 goto setfocus;
4204 if (bDoLabelEdit)
4206 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4207 KillTimer(hwnd, TV_EDIT_TIMER);
4209 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4210 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4212 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4214 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4216 /* Select the current item */
4217 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4218 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4220 else if (ht.flags & TVHT_ONITEMSTATEICON)
4222 /* TVS_CHECKBOXES requires us to toggle the current state */
4223 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4224 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4227 setfocus:
4228 SetFocus(hwnd);
4229 return 0;
4233 static LRESULT
4234 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4236 TVHITTESTINFO ht;
4238 if (infoPtr->hwndEdit)
4240 SetFocus(infoPtr->hwnd);
4241 return 0;
4244 ht.pt.x = (short)LOWORD(lParam);
4245 ht.pt.y = (short)HIWORD(lParam);
4247 TREEVIEW_HitTest(infoPtr, &ht);
4249 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4251 if (ht.hItem)
4253 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4254 infoPtr->dropItem = ht.hItem;
4257 else
4259 SetFocus(infoPtr->hwnd);
4260 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4262 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4263 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4264 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4268 return 0;
4271 static LRESULT
4272 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4274 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4275 INT cx, cy;
4276 HDC hdc, htopdc;
4277 HWND hwtop;
4278 HBITMAP hbmp, hOldbmp;
4279 SIZE size;
4280 RECT rc;
4281 HFONT hOldFont;
4283 TRACE("\n");
4285 if (!(infoPtr->himlNormal))
4286 return 0;
4288 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4289 return 0;
4291 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4293 hwtop = GetDesktopWindow();
4294 htopdc = GetDC(hwtop);
4295 hdc = CreateCompatibleDC(htopdc);
4297 hOldFont = SelectObject(hdc, infoPtr->hFont);
4299 if (dragItem->pszText)
4300 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4301 &size);
4302 else
4303 GetTextExtentPoint32A(hdc, "", 0, &size);
4305 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4306 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4307 hOldbmp = SelectObject(hdc, hbmp);
4309 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4310 size.cx += cx;
4311 if (cy > size.cy)
4312 size.cy = cy;
4314 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4315 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4316 ILD_NORMAL);
4319 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4320 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4323 /* draw item text */
4325 SetRect(&rc, cx, 0, size.cx, size.cy);
4327 if (dragItem->pszText)
4328 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4329 DT_LEFT);
4331 SelectObject(hdc, hOldFont);
4332 SelectObject(hdc, hOldbmp);
4334 ImageList_Add(infoPtr->dragList, hbmp, 0);
4336 DeleteDC(hdc);
4337 DeleteObject(hbmp);
4338 ReleaseDC(hwtop, htopdc);
4340 return (LRESULT)infoPtr->dragList;
4343 /* Selection ************************************************************/
4345 static LRESULT
4346 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4347 INT cause)
4349 TREEVIEW_ITEM *prevSelect;
4351 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4353 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4354 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4355 newSelect ? newSelect->state : 0);
4357 /* reset and redraw focusedItem if focusedItem was set so we don't */
4358 /* have to worry about the previously focused item when we set a new one */
4359 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4360 infoPtr->focusedItem = NULL;
4362 switch (action)
4364 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4365 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4366 /* Fall through */
4367 case TVGN_CARET:
4368 prevSelect = infoPtr->selectedItem;
4370 if (prevSelect == newSelect) {
4371 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4372 break;
4375 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4376 TVN_SELCHANGINGW,
4377 cause,
4378 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4379 prevSelect,
4380 newSelect))
4381 return FALSE;
4383 if (prevSelect)
4384 prevSelect->state &= ~TVIS_SELECTED;
4385 if (newSelect)
4386 newSelect->state |= TVIS_SELECTED;
4388 infoPtr->selectedItem = newSelect;
4390 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4392 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4393 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4395 TREEVIEW_SendTreeviewNotify(infoPtr,
4396 TVN_SELCHANGEDW,
4397 cause,
4398 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4399 prevSelect,
4400 newSelect);
4401 break;
4403 case TVGN_DROPHILITE:
4404 prevSelect = infoPtr->dropItem;
4406 if (prevSelect)
4407 prevSelect->state &= ~TVIS_DROPHILITED;
4409 infoPtr->dropItem = newSelect;
4411 if (newSelect)
4412 newSelect->state |= TVIS_DROPHILITED;
4414 TREEVIEW_Invalidate(infoPtr, prevSelect);
4415 TREEVIEW_Invalidate(infoPtr, newSelect);
4416 break;
4418 case TVGN_FIRSTVISIBLE:
4419 if (newSelect != NULL)
4421 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4422 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4423 TREEVIEW_Invalidate(infoPtr, NULL);
4425 break;
4428 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4429 return TRUE;
4432 /* FIXME: handle NM_KILLFOCUS etc */
4433 static LRESULT
4434 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4436 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4438 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4439 return FALSE;
4441 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4443 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4444 return FALSE;
4446 TREEVIEW_SingleExpand(infoPtr, selection, item);
4448 return TRUE;
4451 /*************************************************************************
4452 * TREEVIEW_ProcessLetterKeys
4454 * Processes keyboard messages generated by pressing the letter keys
4455 * on the keyboard.
4456 * What this does is perform a case insensitive search from the
4457 * current position with the following quirks:
4458 * - If two chars or more are pressed in quick succession we search
4459 * for the corresponding string (e.g. 'abc').
4460 * - If there is a delay we wipe away the current search string and
4461 * restart with just that char.
4462 * - If the user keeps pressing the same character, whether slowly or
4463 * fast, so that the search string is entirely composed of this
4464 * character ('aaaaa' for instance), then we search for first item
4465 * that starting with that character.
4466 * - If the user types the above character in quick succession, then
4467 * we must also search for the corresponding string ('aaaaa'), and
4468 * go to that string if there is a match.
4470 * RETURNS
4472 * Zero.
4474 * BUGS
4476 * - The current implementation has a list of characters it will
4477 * accept and it ignores everything else. In particular it will
4478 * ignore accentuated characters which seems to match what
4479 * Windows does. But I'm not sure it makes sense to follow
4480 * Windows there.
4481 * - We don't sound a beep when the search fails.
4482 * - The search should start from the focused item, not from the selected
4483 * item. One reason for this is to allow for multiple selections in trees.
4484 * But currently infoPtr->focusedItem does not seem very usable.
4486 * SEE ALSO
4488 * TREEVIEW_ProcessLetterKeys
4490 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4492 HTREEITEM nItem;
4493 HTREEITEM endidx,idx;
4494 TVITEMEXW item;
4495 WCHAR buffer[MAX_PATH];
4496 DWORD timestamp,elapsed;
4498 /* simple parameter checking */
4499 if (!charCode || !keyData) return 0;
4501 /* only allow the valid WM_CHARs through */
4502 if (!isalnum(charCode) &&
4503 charCode != '.' && charCode != '`' && charCode != '!' &&
4504 charCode != '@' && charCode != '#' && charCode != '$' &&
4505 charCode != '%' && charCode != '^' && charCode != '&' &&
4506 charCode != '*' && charCode != '(' && charCode != ')' &&
4507 charCode != '-' && charCode != '_' && charCode != '+' &&
4508 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4509 charCode != '}' && charCode != '[' && charCode != '{' &&
4510 charCode != '/' && charCode != '?' && charCode != '>' &&
4511 charCode != '<' && charCode != ',' && charCode != '~')
4512 return 0;
4514 /* compute how much time elapsed since last keypress */
4515 timestamp = GetTickCount();
4516 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4517 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4518 } else {
4519 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4522 /* update the search parameters */
4523 infoPtr->lastKeyPressTimestamp=timestamp;
4524 if (elapsed < KEY_DELAY) {
4525 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4526 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4528 if (infoPtr->charCode != charCode) {
4529 infoPtr->charCode=charCode=0;
4531 } else {
4532 infoPtr->charCode=charCode;
4533 infoPtr->szSearchParam[0]=charCode;
4534 infoPtr->nSearchParamLength=1;
4535 /* Redundant with the 1 char string */
4536 charCode=0;
4539 /* and search from the current position */
4540 nItem=NULL;
4541 if (infoPtr->selectedItem != NULL) {
4542 endidx=infoPtr->selectedItem;
4543 /* if looking for single character match,
4544 * then we must always move forward
4546 if (infoPtr->nSearchParamLength == 1)
4547 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4548 else
4549 idx=endidx;
4550 } else {
4551 endidx=NULL;
4552 idx=infoPtr->root->firstChild;
4554 do {
4555 /* At the end point, sort out wrapping */
4556 if (idx == NULL) {
4558 /* If endidx is null, stop at the last item (ie top to bottom) */
4559 if (endidx == NULL)
4560 break;
4562 /* Otherwise, start again at the very beginning */
4563 idx=infoPtr->root->firstChild;
4565 /* But if we are stopping on the first child, end now! */
4566 if (idx == endidx) break;
4569 /* get item */
4570 ZeroMemory(&item, sizeof(item));
4571 item.mask = TVIF_TEXT;
4572 item.hItem = idx;
4573 item.pszText = buffer;
4574 item.cchTextMax = sizeof(buffer);
4575 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4577 /* check for a match */
4578 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4579 nItem=idx;
4580 break;
4581 } else if ( (charCode != 0) && (nItem == NULL) &&
4582 (nItem != infoPtr->selectedItem) &&
4583 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4584 /* This would work but we must keep looking for a longer match */
4585 nItem=idx;
4587 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4588 } while (idx != endidx);
4590 if (nItem != NULL) {
4591 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4592 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4596 return 0;
4599 /* Scrolling ************************************************************/
4601 static LRESULT
4602 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4604 int viscount;
4605 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4606 HTREEITEM newFirstVisible = NULL;
4607 int visible_pos = -1;
4609 if (!TREEVIEW_ValidItem(infoPtr, item))
4610 return FALSE;
4612 if (!ISVISIBLE(item))
4614 /* Expand parents as necessary. */
4615 HTREEITEM parent;
4617 /* see if we are trying to ensure that root is visible */
4618 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4619 parent = item->parent;
4620 else
4621 parent = item; /* this item is the topmost item */
4623 while (parent != infoPtr->root)
4625 if (!(parent->state & TVIS_EXPANDED))
4626 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4628 parent = parent->parent;
4632 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4634 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4635 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4637 if (hasFirstVisible)
4638 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4640 if (visible_pos < 0)
4642 /* item is before the start of the list: put it at the top. */
4643 newFirstVisible = item;
4645 else if (visible_pos >= viscount
4646 /* Sometimes, before we are displayed, GVC is 0, causing us to
4647 * spuriously scroll up. */
4648 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4650 /* item is past the end of the list. */
4651 int scroll = visible_pos - viscount;
4653 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4654 scroll + 1);
4657 if (bHScroll)
4659 /* Scroll window so item's text is visible as much as possible */
4660 /* Calculation of amount of extra space is taken from EditLabel code */
4661 INT pos, x;
4662 TEXTMETRICW textMetric;
4663 HDC hdc = GetWindowDC(infoPtr->hwnd);
4665 x = item->textWidth;
4667 GetTextMetricsW(hdc, &textMetric);
4668 ReleaseDC(infoPtr->hwnd, hdc);
4670 x += (textMetric.tmMaxCharWidth * 2);
4671 x = max(x, textMetric.tmMaxCharWidth * 3);
4673 if (item->textOffset < 0)
4674 pos = item->textOffset;
4675 else if (item->textOffset + x > infoPtr->clientWidth)
4677 if (x > infoPtr->clientWidth)
4678 pos = item->textOffset;
4679 else
4680 pos = item->textOffset + x - infoPtr->clientWidth;
4682 else
4683 pos = 0;
4685 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4688 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4690 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4692 return TRUE;
4695 return FALSE;
4698 static VOID
4699 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4700 TREEVIEW_ITEM *newFirstVisible,
4701 BOOL bUpdateScrollPos)
4703 int gap_size;
4705 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4707 if (newFirstVisible != NULL)
4709 /* Prevent an empty gap from appearing at the bottom... */
4710 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4711 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4713 if (gap_size > 0)
4715 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4716 -gap_size);
4718 /* ... unless we just don't have enough items. */
4719 if (newFirstVisible == NULL)
4720 newFirstVisible = infoPtr->root->firstChild;
4724 if (infoPtr->firstVisible != newFirstVisible)
4726 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4728 infoPtr->firstVisible = newFirstVisible;
4729 TREEVIEW_Invalidate(infoPtr, NULL);
4731 else
4733 TREEVIEW_ITEM *item;
4734 int scroll = infoPtr->uItemHeight *
4735 (infoPtr->firstVisible->visibleOrder
4736 - newFirstVisible->visibleOrder);
4738 infoPtr->firstVisible = newFirstVisible;
4740 for (item = infoPtr->root->firstChild; item != NULL;
4741 item = TREEVIEW_GetNextListItem(infoPtr, item))
4743 item->rect.top += scroll;
4744 item->rect.bottom += scroll;
4747 if (bUpdateScrollPos)
4748 SetScrollPos(infoPtr->hwnd, SB_VERT,
4749 newFirstVisible->visibleOrder, TRUE);
4751 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4756 /************************************************************************
4757 * VScroll is always in units of visible items. i.e. we always have a
4758 * visible item aligned to the top of the control. (Unless we have no
4759 * items at all.)
4761 static LRESULT
4762 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4764 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4765 TREEVIEW_ITEM *newFirstVisible = NULL;
4767 int nScrollCode = LOWORD(wParam);
4769 TRACE("wp %lx\n", wParam);
4771 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4772 return 0;
4774 if (!oldFirstVisible)
4776 assert(infoPtr->root->firstChild == NULL);
4777 return 0;
4780 switch (nScrollCode)
4782 case SB_TOP:
4783 newFirstVisible = infoPtr->root->firstChild;
4784 break;
4786 case SB_BOTTOM:
4787 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4788 break;
4790 case SB_LINEUP:
4791 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4792 break;
4794 case SB_LINEDOWN:
4795 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4796 break;
4798 case SB_PAGEUP:
4799 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4800 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4801 break;
4803 case SB_PAGEDOWN:
4804 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4805 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4806 break;
4808 case SB_THUMBTRACK:
4809 case SB_THUMBPOSITION:
4810 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4811 infoPtr->root->firstChild,
4812 (LONG)(SHORT)HIWORD(wParam));
4813 break;
4815 case SB_ENDSCROLL:
4816 return 0;
4819 if (newFirstVisible != NULL)
4821 if (newFirstVisible != oldFirstVisible)
4822 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4823 nScrollCode != SB_THUMBTRACK);
4824 else if (nScrollCode == SB_THUMBPOSITION)
4825 SetScrollPos(infoPtr->hwnd, SB_VERT,
4826 newFirstVisible->visibleOrder, TRUE);
4829 return 0;
4832 static LRESULT
4833 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4835 int maxWidth;
4836 int scrollX = infoPtr->scrollX;
4837 int nScrollCode = LOWORD(wParam);
4839 TRACE("wp %lx\n", wParam);
4841 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4842 return FALSE;
4844 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4845 /* shall never occur */
4846 if (maxWidth <= 0)
4848 scrollX = 0;
4849 goto scroll;
4852 switch (nScrollCode)
4854 case SB_LINELEFT:
4855 scrollX -= infoPtr->uItemHeight;
4856 break;
4857 case SB_LINERIGHT:
4858 scrollX += infoPtr->uItemHeight;
4859 break;
4860 case SB_PAGELEFT:
4861 scrollX -= infoPtr->clientWidth;
4862 break;
4863 case SB_PAGERIGHT:
4864 scrollX += infoPtr->clientWidth;
4865 break;
4867 case SB_THUMBTRACK:
4868 case SB_THUMBPOSITION:
4869 scrollX = (int)(SHORT)HIWORD(wParam);
4870 break;
4872 case SB_ENDSCROLL:
4873 return 0;
4876 if (scrollX > maxWidth)
4877 scrollX = maxWidth;
4878 else if (scrollX < 0)
4879 scrollX = 0;
4881 scroll:
4882 if (scrollX != infoPtr->scrollX)
4884 TREEVIEW_ITEM *item;
4885 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4887 for (item = infoPtr->root->firstChild; item != NULL;
4888 item = TREEVIEW_GetNextListItem(infoPtr, item))
4890 item->linesOffset += scroll_pixels;
4891 item->stateOffset += scroll_pixels;
4892 item->imageOffset += scroll_pixels;
4893 item->textOffset += scroll_pixels;
4896 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4897 infoPtr->scrollX = scrollX;
4898 UpdateWindow(infoPtr->hwnd);
4901 if (nScrollCode != SB_THUMBTRACK)
4902 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4904 return 0;
4907 static LRESULT
4908 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4910 short gcWheelDelta;
4911 UINT pulScrollLines = 3;
4913 if (wParam & (MK_SHIFT | MK_CONTROL))
4914 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4916 if (infoPtr->firstVisible == NULL)
4917 return TRUE;
4919 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4921 gcWheelDelta = -(short)HIWORD(wParam);
4922 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4924 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4926 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4927 int maxDy = infoPtr->maxVisibleOrder;
4929 if (newDy > maxDy)
4930 newDy = maxDy;
4932 if (newDy < 0)
4933 newDy = 0;
4935 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4937 return TRUE;
4940 /* Create/Destroy *******************************************************/
4942 static void
4943 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
4945 RECT rc;
4946 HBITMAP hbm, hbmOld;
4947 HDC hdc, hdcScreen;
4948 int nIndex;
4950 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4952 hdcScreen = GetDC(0);
4954 hdc = CreateCompatibleDC(hdcScreen);
4955 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4956 hbmOld = SelectObject(hdc, hbm);
4958 SetRect(&rc, 0, 0, 48, 16);
4959 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4961 SetRect(&rc, 18, 2, 30, 14);
4962 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4963 DFCS_BUTTONCHECK|DFCS_FLAT);
4965 SetRect(&rc, 34, 2, 46, 14);
4966 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4967 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4969 SelectObject(hdc, hbmOld);
4970 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4971 comctl32_color.clrWindow);
4972 TRACE("checkbox index %d\n", nIndex);
4974 DeleteObject(hbm);
4975 DeleteDC(hdc);
4976 ReleaseDC(0, hdcScreen);
4978 infoPtr->stateImageWidth = 16;
4979 infoPtr->stateImageHeight = 16;
4982 static LRESULT
4983 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4985 RECT rcClient;
4986 TREEVIEW_INFO *infoPtr;
4987 LOGFONTW lf;
4989 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4991 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4993 if (infoPtr == NULL)
4995 ERR("could not allocate info memory!\n");
4996 return 0;
4999 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5001 infoPtr->hwnd = hwnd;
5002 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5003 infoPtr->Timer = 0;
5004 infoPtr->uNumItems = 0;
5005 infoPtr->cdmode = 0;
5006 infoPtr->uScrollTime = 300; /* milliseconds */
5007 infoPtr->bRedraw = TRUE;
5009 GetClientRect(hwnd, &rcClient);
5011 /* No scroll bars yet. */
5012 infoPtr->clientWidth = rcClient.right;
5013 infoPtr->clientHeight = rcClient.bottom;
5014 infoPtr->uInternalStatus = 0;
5016 infoPtr->treeWidth = 0;
5017 infoPtr->treeHeight = 0;
5019 infoPtr->uIndent = MINIMUM_INDENT;
5020 infoPtr->selectedItem = NULL;
5021 infoPtr->focusedItem = NULL;
5022 infoPtr->hotItem = NULL;
5023 infoPtr->editItem = NULL;
5024 infoPtr->firstVisible = NULL;
5025 infoPtr->maxVisibleOrder = 0;
5026 infoPtr->dropItem = NULL;
5027 infoPtr->insertMarkItem = NULL;
5028 infoPtr->insertBeforeorAfter = 0;
5029 /* dragList */
5031 infoPtr->scrollX = 0;
5033 infoPtr->clrBk = CLR_NONE; /* use system color */
5034 infoPtr->clrText = CLR_NONE; /* use system color */
5035 infoPtr->clrLine = CLR_DEFAULT;
5036 infoPtr->clrInsertMark = CLR_DEFAULT;
5038 /* hwndToolTip */
5040 infoPtr->hwndEdit = NULL;
5041 infoPtr->wpEditOrig = NULL;
5042 infoPtr->bIgnoreEditKillFocus = FALSE;
5043 infoPtr->bLabelChanged = FALSE;
5045 infoPtr->himlNormal = NULL;
5046 infoPtr->himlState = NULL;
5047 infoPtr->normalImageWidth = 0;
5048 infoPtr->normalImageHeight = 0;
5049 infoPtr->stateImageWidth = 0;
5050 infoPtr->stateImageHeight = 0;
5052 infoPtr->items = DPA_Create(16);
5054 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5055 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5056 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5057 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5058 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5060 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5062 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5063 infoPtr->root->state = TVIS_EXPANDED;
5064 infoPtr->root->iLevel = -1;
5065 infoPtr->root->visibleOrder = -1;
5067 infoPtr->hwndNotify = lpcs->hwndParent;
5068 infoPtr->hwndToolTip = 0;
5070 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5072 /* Determine what type of notify should be issued */
5073 /* sets infoPtr->bNtfUnicode */
5074 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5076 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5077 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5078 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5079 hwnd, 0, 0, 0);
5081 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5082 TREEVIEW_InitCheckboxes(infoPtr);
5084 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5085 ShowScrollBar(hwnd, SB_VERT, FALSE);
5086 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5088 OpenThemeData (hwnd, themeClass);
5090 return 0;
5094 static LRESULT
5095 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5097 TRACE("\n");
5099 /* free item data */
5100 TREEVIEW_RemoveTree(infoPtr);
5101 /* root isn't freed with other items */
5102 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5103 DPA_Destroy(infoPtr->items);
5105 /* tool tip is automatically destroyed: we are its owner */
5107 /* Restore original wndproc */
5108 if (infoPtr->hwndEdit)
5109 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5110 (DWORD_PTR)infoPtr->wpEditOrig);
5112 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5114 /* Deassociate treeview from the window before doing anything drastic. */
5115 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5117 DeleteObject(infoPtr->hDefaultFont);
5118 DeleteObject(infoPtr->hBoldFont);
5119 DeleteObject(infoPtr->hUnderlineFont);
5120 Free(infoPtr);
5122 return 0;
5125 /* Miscellaneous Messages ***********************************************/
5127 static LRESULT
5128 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5130 static const struct
5132 unsigned char code;
5134 scroll[] =
5136 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5137 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5138 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5139 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5140 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5141 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5142 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5143 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5144 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5145 #undef SCROLL_ENTRY
5148 if (key >= VK_PRIOR && key <= VK_DOWN)
5150 unsigned char code = scroll[key - VK_PRIOR].code;
5152 (((code & (1 << 7)) == (SB_HORZ << 7))
5153 ? TREEVIEW_HScroll
5154 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5157 return 0;
5160 /************************************************************************
5161 * TREEVIEW_KeyDown
5163 * VK_UP Move selection to the previous non-hidden item.
5164 * VK_DOWN Move selection to the next non-hidden item.
5165 * VK_HOME Move selection to the first item.
5166 * VK_END Move selection to the last item.
5167 * VK_LEFT If expanded then collapse, otherwise move to parent.
5168 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5169 * VK_ADD Expand.
5170 * VK_SUBTRACT Collapse.
5171 * VK_MULTIPLY Expand all.
5172 * VK_PRIOR Move up GetVisibleCount items.
5173 * VK_NEXT Move down GetVisibleCount items.
5174 * VK_BACK Move to parent.
5175 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5177 static LRESULT
5178 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5180 /* If it is non-NULL and different, it will be selected and visible. */
5181 TREEVIEW_ITEM *newSelection = NULL;
5183 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5185 TRACE("%lx\n", wParam);
5187 if (prevItem == NULL)
5188 return FALSE;
5190 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5191 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5193 switch (wParam)
5195 case VK_UP:
5196 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5197 if (!newSelection)
5198 newSelection = infoPtr->root->firstChild;
5199 break;
5201 case VK_DOWN:
5202 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5203 break;
5205 case VK_HOME:
5206 newSelection = infoPtr->root->firstChild;
5207 break;
5209 case VK_END:
5210 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5211 break;
5213 case VK_LEFT:
5214 if (prevItem->state & TVIS_EXPANDED)
5216 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5218 else if (prevItem->parent != infoPtr->root)
5220 newSelection = prevItem->parent;
5222 break;
5224 case VK_RIGHT:
5225 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5227 if (!(prevItem->state & TVIS_EXPANDED))
5228 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5229 else
5231 newSelection = prevItem->firstChild;
5235 break;
5237 case VK_MULTIPLY:
5238 TREEVIEW_ExpandAll(infoPtr, prevItem);
5239 break;
5241 case VK_ADD:
5242 if (!(prevItem->state & TVIS_EXPANDED))
5243 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5244 break;
5246 case VK_SUBTRACT:
5247 if (prevItem->state & TVIS_EXPANDED)
5248 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5249 break;
5251 case VK_PRIOR:
5252 newSelection
5253 = TREEVIEW_GetListItem(infoPtr, prevItem,
5254 -TREEVIEW_GetVisibleCount(infoPtr));
5255 break;
5257 case VK_NEXT:
5258 newSelection
5259 = TREEVIEW_GetListItem(infoPtr, prevItem,
5260 TREEVIEW_GetVisibleCount(infoPtr));
5261 break;
5263 case VK_BACK:
5264 newSelection = prevItem->parent;
5265 if (newSelection == infoPtr->root)
5266 newSelection = NULL;
5267 break;
5269 case VK_SPACE:
5270 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5271 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5272 break;
5275 if (newSelection && newSelection != prevItem)
5277 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5278 TVC_BYKEYBOARD))
5280 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5284 return FALSE;
5287 static LRESULT
5288 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5290 /* remove hot effect from item */
5291 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5292 infoPtr->hotItem = NULL;
5294 return 0;
5297 static LRESULT
5298 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5300 POINT pt;
5301 TRACKMOUSEEVENT trackinfo;
5302 TREEVIEW_ITEM * item;
5304 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5306 /* fill in the TRACKMOUSEEVENT struct */
5307 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5308 trackinfo.dwFlags = TME_QUERY;
5309 trackinfo.hwndTrack = infoPtr->hwnd;
5311 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5312 _TrackMouseEvent(&trackinfo);
5314 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5315 if(!(trackinfo.dwFlags & TME_LEAVE))
5317 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5318 trackinfo.hwndTrack = infoPtr->hwnd;
5319 /* do it as fast as possible, minimal systimer latency will be used */
5320 trackinfo.dwHoverTime = 1;
5322 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5323 /* and can properly deactivate the hot item */
5324 _TrackMouseEvent(&trackinfo);
5327 pt.x = (short)LOWORD(lParam);
5328 pt.y = (short)HIWORD(lParam);
5330 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5332 if (item != infoPtr->hotItem)
5334 /* redraw old hot item */
5335 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5336 infoPtr->hotItem = item;
5337 /* redraw new hot item */
5338 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5341 return 0;
5344 /* Draw themed border */
5345 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5347 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5348 HDC dc;
5349 RECT r;
5350 HRGN cliprgn;
5351 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5352 cyEdge = GetSystemMetrics (SM_CYEDGE);
5354 if (!theme)
5355 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5357 GetWindowRect(infoPtr->hwnd, &r);
5359 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5360 r.right - cxEdge, r.bottom - cyEdge);
5361 if (region != (HRGN)1)
5362 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5363 OffsetRect(&r, -r.left, -r.top);
5365 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5366 OffsetRect(&r, -r.left, -r.top);
5368 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5369 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5370 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5371 ReleaseDC(infoPtr->hwnd, dc);
5373 /* Call default proc to get the scrollbars etc. painted */
5374 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5376 return TRUE;
5379 static LRESULT
5380 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5382 LPNMHDR lpnmh = (LPNMHDR)lParam;
5384 if (lpnmh->code == PGN_CALCSIZE) {
5385 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5387 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5388 lppgc->iWidth = infoPtr->treeWidth;
5389 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5390 infoPtr->treeWidth, infoPtr->clientWidth);
5392 else {
5393 lppgc->iHeight = infoPtr->treeHeight;
5394 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5395 infoPtr->treeHeight, infoPtr->clientHeight);
5397 return 0;
5399 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5402 static LRESULT
5403 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5405 if (wParam == SIZE_RESTORED)
5407 infoPtr->clientWidth = (short)LOWORD(lParam);
5408 infoPtr->clientHeight = (short)HIWORD(lParam);
5410 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5411 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5412 TREEVIEW_UpdateScrollBars(infoPtr);
5414 else
5416 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5419 TREEVIEW_Invalidate(infoPtr, NULL);
5420 return 0;
5423 static void TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5425 TREEVIEW_ITEM *child = item->firstChild;
5427 item->state &= ~TVIS_STATEIMAGEMASK;
5428 item->state |= INDEXTOSTATEIMAGEMASK(1);
5430 while (child)
5432 TREEVIEW_ITEM *next = child->nextSibling;
5433 TREEVIEW_ResetImageStateIndex(infoPtr, child);
5434 child = next;
5438 static LRESULT
5439 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5441 TRACE("(%lx %lx)\n", wParam, lParam);
5443 if (wParam == GWL_STYLE)
5445 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5447 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5449 if (dwNewStyle & TVS_CHECKBOXES)
5451 TREEVIEW_InitCheckboxes(infoPtr);
5452 TRACE("checkboxes enabled\n");
5454 /* set all items to state image index 1 */
5455 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5457 else
5459 FIXME("tried to disable checkboxes\n");
5463 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5465 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5467 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5468 TRACE("tooltips enabled\n");
5470 else
5472 DestroyWindow(infoPtr->hwndToolTip);
5473 infoPtr->hwndToolTip = 0;
5474 TRACE("tooltips disabled\n");
5478 infoPtr->dwStyle = dwNewStyle;
5481 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5482 TREEVIEW_UpdateScrollBars(infoPtr);
5483 TREEVIEW_Invalidate(infoPtr, NULL);
5485 return 0;
5488 static LRESULT
5489 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5491 POINT pt;
5492 TREEVIEW_ITEM * item;
5493 NMMOUSE nmmouse;
5495 GetCursorPos(&pt);
5496 ScreenToClient(infoPtr->hwnd, &pt);
5498 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5500 memset(&nmmouse, 0, sizeof(nmmouse));
5501 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5502 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5503 nmmouse.hdr.code = NM_SETCURSOR;
5504 if (item)
5506 nmmouse.dwItemSpec = (DWORD_PTR)item;
5507 nmmouse.dwItemData = item->lParam;
5509 nmmouse.pt.x = 0;
5510 nmmouse.pt.y = 0;
5511 nmmouse.dwHitInfo = lParam;
5512 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, &nmmouse.hdr))
5513 return 0;
5515 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5517 SetCursor(infoPtr->hcurHand);
5518 return 0;
5520 else
5521 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5524 static LRESULT
5525 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5527 TRACE("\n");
5529 if (!infoPtr->selectedItem)
5531 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5532 TVC_UNKNOWN);
5535 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5536 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5537 return 0;
5540 static LRESULT
5541 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5543 TRACE("\n");
5545 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5546 UpdateWindow(infoPtr->hwnd);
5547 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5548 return 0;
5551 /* update theme after a WM_THEMECHANGED message */
5552 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5554 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5555 CloseThemeData (theme);
5556 OpenThemeData (infoPtr->hwnd, themeClass);
5557 return 0;
5561 static LRESULT WINAPI
5562 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5564 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5566 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5568 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5569 else
5571 if (uMsg == WM_CREATE)
5572 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5573 else
5574 goto def;
5577 switch (uMsg)
5579 case TVM_CREATEDRAGIMAGE:
5580 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5582 case TVM_DELETEITEM:
5583 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5585 case TVM_EDITLABELA:
5586 case TVM_EDITLABELW:
5587 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5589 case TVM_ENDEDITLABELNOW:
5590 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5592 case TVM_ENSUREVISIBLE:
5593 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5595 case TVM_EXPAND:
5596 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5598 case TVM_GETBKCOLOR:
5599 return TREEVIEW_GetBkColor(infoPtr);
5601 case TVM_GETCOUNT:
5602 return TREEVIEW_GetCount(infoPtr);
5604 case TVM_GETEDITCONTROL:
5605 return TREEVIEW_GetEditControl(infoPtr);
5607 case TVM_GETIMAGELIST:
5608 return TREEVIEW_GetImageList(infoPtr, wParam);
5610 case TVM_GETINDENT:
5611 return TREEVIEW_GetIndent(infoPtr);
5613 case TVM_GETINSERTMARKCOLOR:
5614 return TREEVIEW_GetInsertMarkColor(infoPtr);
5616 case TVM_GETISEARCHSTRINGA:
5617 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5618 return 0;
5620 case TVM_GETISEARCHSTRINGW:
5621 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5622 return 0;
5624 case TVM_GETITEMA:
5625 case TVM_GETITEMW:
5626 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5627 uMsg == TVM_GETITEMW);
5628 case TVM_GETITEMHEIGHT:
5629 return TREEVIEW_GetItemHeight(infoPtr);
5631 case TVM_GETITEMRECT:
5632 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5634 case TVM_GETITEMSTATE:
5635 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5637 case TVM_GETLINECOLOR:
5638 return TREEVIEW_GetLineColor(infoPtr);
5640 case TVM_GETNEXTITEM:
5641 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5643 case TVM_GETSCROLLTIME:
5644 return TREEVIEW_GetScrollTime(infoPtr);
5646 case TVM_GETTEXTCOLOR:
5647 return TREEVIEW_GetTextColor(infoPtr);
5649 case TVM_GETTOOLTIPS:
5650 return TREEVIEW_GetToolTips(infoPtr);
5652 case TVM_GETUNICODEFORMAT:
5653 return TREEVIEW_GetUnicodeFormat(infoPtr);
5655 case TVM_GETVISIBLECOUNT:
5656 return TREEVIEW_GetVisibleCount(infoPtr);
5658 case TVM_HITTEST:
5659 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5661 case TVM_INSERTITEMA:
5662 case TVM_INSERTITEMW:
5663 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5664 uMsg == TVM_INSERTITEMW);
5665 case TVM_SELECTITEM:
5666 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5668 case TVM_SETBKCOLOR:
5669 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5671 case TVM_SETIMAGELIST:
5672 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5674 case TVM_SETINDENT:
5675 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5677 case TVM_SETINSERTMARK:
5678 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5680 case TVM_SETINSERTMARKCOLOR:
5681 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5683 case TVM_SETITEMA:
5684 case TVM_SETITEMW:
5685 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5686 uMsg == TVM_SETITEMW);
5687 case TVM_SETLINECOLOR:
5688 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5690 case TVM_SETITEMHEIGHT:
5691 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5693 case TVM_SETSCROLLTIME:
5694 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5696 case TVM_SETTEXTCOLOR:
5697 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5699 case TVM_SETTOOLTIPS:
5700 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5702 case TVM_SETUNICODEFORMAT:
5703 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5705 case TVM_SORTCHILDREN:
5706 return TREEVIEW_SortChildren(infoPtr, lParam);
5708 case TVM_SORTCHILDRENCB:
5709 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5711 case WM_CHAR:
5712 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5714 case WM_COMMAND:
5715 return TREEVIEW_Command(infoPtr, wParam, lParam);
5717 case WM_DESTROY:
5718 return TREEVIEW_Destroy(infoPtr);
5720 /* WM_ENABLE */
5722 case WM_ERASEBKGND:
5723 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5725 case WM_GETDLGCODE:
5726 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5728 case WM_GETFONT:
5729 return TREEVIEW_GetFont(infoPtr);
5731 case WM_HSCROLL:
5732 return TREEVIEW_HScroll(infoPtr, wParam);
5734 case WM_KEYDOWN:
5735 return TREEVIEW_KeyDown(infoPtr, wParam);
5737 case WM_KILLFOCUS:
5738 return TREEVIEW_KillFocus(infoPtr);
5740 case WM_LBUTTONDBLCLK:
5741 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5743 case WM_LBUTTONDOWN:
5744 return TREEVIEW_LButtonDown(infoPtr, lParam);
5746 /* WM_MBUTTONDOWN */
5748 case WM_MOUSELEAVE:
5749 return TREEVIEW_MouseLeave(infoPtr);
5751 case WM_MOUSEMOVE:
5752 return TREEVIEW_MouseMove(infoPtr, lParam);
5754 case WM_NCLBUTTONDOWN:
5755 if (infoPtr->hwndEdit)
5756 SetFocus(infoPtr->hwnd);
5757 goto def;
5759 case WM_NCPAINT:
5760 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5762 case WM_NOTIFY:
5763 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5765 case WM_NOTIFYFORMAT:
5766 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5768 case WM_PRINTCLIENT:
5769 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5771 case WM_PAINT:
5772 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5774 case WM_RBUTTONDOWN:
5775 return TREEVIEW_RButtonDown(infoPtr, lParam);
5777 case WM_SETCURSOR:
5778 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5780 case WM_SETFOCUS:
5781 return TREEVIEW_SetFocus(infoPtr);
5783 case WM_SETFONT:
5784 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5786 case WM_SETREDRAW:
5787 return TREEVIEW_SetRedraw(infoPtr, wParam);
5789 case WM_SIZE:
5790 return TREEVIEW_Size(infoPtr, wParam, lParam);
5792 case WM_STYLECHANGED:
5793 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5795 case WM_SYSCOLORCHANGE:
5796 COMCTL32_RefreshSysColors();
5797 return 0;
5799 /* WM_SYSKEYDOWN */
5801 case WM_TIMER:
5802 return TREEVIEW_HandleTimer(infoPtr, wParam);
5804 case WM_THEMECHANGED:
5805 return TREEVIEW_ThemeChanged (infoPtr);
5807 case WM_VSCROLL:
5808 return TREEVIEW_VScroll(infoPtr, wParam);
5810 /* WM_WININICHANGE */
5812 case WM_MOUSEWHEEL:
5813 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5815 case WM_DRAWITEM:
5816 TRACE("drawItem\n");
5817 goto def;
5819 default:
5820 /* This mostly catches MFC and Delphi messages. :( */
5821 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5822 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5823 def:
5824 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5829 /* Class Registration ***************************************************/
5831 VOID
5832 TREEVIEW_Register(void)
5834 WNDCLASSW wndClass;
5836 TRACE("\n");
5838 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5839 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5840 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5841 wndClass.cbClsExtra = 0;
5842 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5844 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5845 wndClass.hbrBackground = 0;
5846 wndClass.lpszClassName = WC_TREEVIEWW;
5848 RegisterClassW(&wndClass);
5852 VOID
5853 TREEVIEW_Unregister(void)
5855 UnregisterClassW(WC_TREEVIEWW, NULL);
5859 /* Tree Verification ****************************************************/
5861 static inline void
5862 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5864 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5865 const TREEVIEW_ITEM *item)
5867 assert(infoPtr != NULL);
5868 assert(item != NULL);
5870 /* both NULL, or both non-null */
5871 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5873 assert(item->firstChild != item);
5874 assert(item->lastChild != item);
5876 if (item->firstChild)
5878 assert(item->firstChild->parent == item);
5879 assert(item->firstChild->prevSibling == NULL);
5882 if (item->lastChild)
5884 assert(item->lastChild->parent == item);
5885 assert(item->lastChild->nextSibling == NULL);
5888 assert(item->nextSibling != item);
5889 if (item->nextSibling)
5891 assert(item->nextSibling->parent == item->parent);
5892 assert(item->nextSibling->prevSibling == item);
5895 assert(item->prevSibling != item);
5896 if (item->prevSibling)
5898 assert(item->prevSibling->parent == item->parent);
5899 assert(item->prevSibling->nextSibling == item);
5903 static inline void
5904 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5906 assert(item != NULL);
5908 assert(item->parent != NULL);
5909 assert(item->parent != item);
5910 assert(item->iLevel == item->parent->iLevel + 1);
5912 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5914 TREEVIEW_VerifyItemCommon(infoPtr, item);
5916 TREEVIEW_VerifyChildren(infoPtr, item);
5919 static inline void
5920 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5922 const TREEVIEW_ITEM *child;
5923 assert(item != NULL);
5925 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5926 TREEVIEW_VerifyItem(infoPtr, child);
5929 static inline void
5930 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5932 TREEVIEW_ITEM *root = infoPtr->root;
5934 assert(root != NULL);
5935 assert(root->iLevel == -1);
5936 assert(root->parent == NULL);
5937 assert(root->prevSibling == NULL);
5939 TREEVIEW_VerifyItemCommon(infoPtr, root);
5941 TREEVIEW_VerifyChildren(infoPtr, root);
5944 static void
5945 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5947 if (!TRACE_ON(treeview)) return;
5949 assert(infoPtr != NULL);
5950 TREEVIEW_VerifyRoot(infoPtr);