comctl32/treeview: Always return state from TVM_GETITEM handler.
[wine/multimedia.git] / dlls / comctl32 / treeview.c
blob84c2c8bcfe23d400da292d7fab06a9651b04c03d
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_CUT, 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 "tmschema.h"
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
69 /* internal structures */
71 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
73 HTREEITEM parent; /* handle to parent or 0 if at root */
74 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
75 HTREEITEM firstChild; /* handle to first child or 0 if no child */
77 UINT callbackMask;
78 UINT state;
79 UINT stateMask;
80 LPWSTR pszText;
81 int cchTextMax;
82 int iImage;
83 int iSelectedImage;
84 int iExpandedImage;
85 int cChildren;
86 LPARAM lParam;
87 int iIntegral; /* item height multiplier (1 is normal) */
88 int iLevel; /* indentation level:0=root level */
89 HTREEITEM lastChild;
90 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
91 RECT rect;
92 LONG linesOffset;
93 LONG stateOffset;
94 LONG imageOffset;
95 LONG textOffset;
96 LONG textWidth; /* horizontal text extent for pszText */
97 LONG visibleOrder; /* visible ordering, 0 is first visible item */
98 } TREEVIEW_ITEM;
101 typedef struct tagTREEVIEW_INFO
103 HWND hwnd;
104 HWND hwndNotify; /* Owner window to send notifications to */
105 DWORD dwStyle;
106 HTREEITEM root;
107 UINT uInternalStatus;
108 INT Timer;
109 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
110 INT cdmode; /* last custom draw setting */
111 UINT uScrollTime; /* max. time for scrolling in milliseconds */
112 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
114 UINT uItemHeight; /* item height */
115 BOOL bHeightSet;
117 LONG clientWidth; /* width of control window */
118 LONG clientHeight; /* height of control window */
120 LONG treeWidth; /* width of visible tree items */
121 LONG treeHeight; /* height of visible tree items */
123 UINT uIndent; /* indentation in pixels */
124 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
125 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
126 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
127 HTREEITEM editItem; /* item being edited with builtin edit box */
129 HTREEITEM firstVisible; /* handle to first visible item */
130 LONG maxVisibleOrder;
131 HTREEITEM dropItem; /* handle to item selected by drag cursor */
132 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
133 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
134 HIMAGELIST dragList; /* Bitmap of dragged item */
135 LONG scrollX;
136 COLORREF clrBk;
137 COLORREF clrText;
138 COLORREF clrLine;
139 COLORREF clrInsertMark;
140 HFONT hFont;
141 HFONT hDefaultFont;
142 HFONT hBoldFont;
143 HFONT hUnderlineFont;
144 HCURSOR hcurHand;
145 HWND hwndToolTip;
147 HWND hwndEdit;
148 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
149 BOOL bIgnoreEditKillFocus;
150 BOOL bLabelChanged;
152 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
153 HIMAGELIST himlNormal;
154 int normalImageHeight;
155 int normalImageWidth;
156 HIMAGELIST himlState;
157 int stateImageHeight;
158 int stateImageWidth;
159 HDPA items;
161 DWORD lastKeyPressTimestamp;
162 WPARAM charCode;
163 INT nSearchParamLength;
164 WCHAR szSearchParam[ MAX_PATH ];
165 } TREEVIEW_INFO;
168 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
169 #define KEY_DELAY 450
171 /* bitflags for infoPtr->uInternalStatus */
173 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
174 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
175 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
176 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
177 #define TV_RDRAG 0x10 /* ditto Rbutton */
178 #define TV_RDRAGGING 0x20
180 /* bitflags for infoPtr->timer */
182 #define TV_EDIT_TIMER 2
183 #define TV_EDIT_TIMER_SET 2
185 #define TEXT_CALLBACK_SIZE 260
187 #define TREEVIEW_LEFT_MARGIN 8
189 #define MINIMUM_INDENT 19
191 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
193 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
194 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
195 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
197 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
198 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
199 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
200 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
202 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
205 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
208 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
210 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
211 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
212 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
213 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
214 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
215 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
216 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
218 /* Random Utilities *****************************************************/
219 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
221 /* Returns the treeview private data if hwnd is a treeview.
222 * Otherwise returns an undefined value. */
223 static inline TREEVIEW_INFO *
224 TREEVIEW_GetInfoPtr(HWND hwnd)
226 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
229 /* Don't call this. Nothing wants an item index. */
230 static inline int
231 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
233 return DPA_GetPtrIndex(infoPtr->items, handle);
236 /* Checks if item has changed and needs to be redrawn */
237 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
238 const TVITEMEXW *tvChange)
240 /* Number of children has changed */
241 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
242 return TRUE;
244 /* Image has changed and it's not a callback */
245 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
246 tiNew->iImage != I_IMAGECALLBACK)
247 return TRUE;
249 /* Selected image has changed and it's not a callback */
250 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
251 tiNew->iSelectedImage != I_IMAGECALLBACK)
252 return TRUE;
254 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
255 tiNew->iExpandedImage != I_IMAGECALLBACK)
256 return TRUE;
258 /* Text has changed and it's not a callback */
259 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
260 tiNew->pszText != LPSTR_TEXTCALLBACKW)
261 return TRUE;
263 /* Indent has changed */
264 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
265 return TRUE;
267 /* Item state has changed */
268 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
269 return TRUE;
271 return FALSE;
274 /***************************************************************************
275 * This method checks that handle is an item for this tree.
277 static BOOL
278 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
280 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
282 TRACE("invalid item %p\n", handle);
283 return FALSE;
285 else
286 return TRUE;
289 static HFONT
290 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
292 LOGFONTW font;
294 GetObjectW(hOrigFont, sizeof(font), &font);
295 font.lfWeight = FW_BOLD;
296 return CreateFontIndirectW(&font);
299 static HFONT
300 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
302 LOGFONTW font;
304 GetObjectW(hOrigFont, sizeof(font), &font);
305 font.lfUnderline = TRUE;
306 return CreateFontIndirectW(&font);
309 static inline HFONT
310 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
312 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
313 return infoPtr->hUnderlineFont;
314 if (item->state & TVIS_BOLD)
315 return infoPtr->hBoldFont;
316 return infoPtr->hFont;
319 /* for trace/debugging purposes only */
320 static const char *
321 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
323 if (item == NULL) return "<null item>";
324 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
325 if (item->pszText == NULL) return "<null>";
326 return debugstr_w(item->pszText);
329 /* An item is not a child of itself. */
330 static BOOL
331 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
335 child = child->parent;
336 if (child == parent) return TRUE;
337 } while (child != NULL);
339 return FALSE;
343 /* Tree Traversal *******************************************************/
345 /***************************************************************************
346 * This method returns the last expanded sibling or child child item
347 * of a tree node
349 static TREEVIEW_ITEM *
350 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
352 if (!wineItem)
353 return NULL;
355 while (wineItem->lastChild)
357 if (wineItem->state & TVIS_EXPANDED)
358 wineItem = wineItem->lastChild;
359 else
360 break;
363 if (wineItem == infoPtr->root)
364 return NULL;
366 return wineItem;
369 /***************************************************************************
370 * This method returns the previous non-hidden item in the list not
371 * considering the tree hierarchy.
373 static TREEVIEW_ITEM *
374 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
376 if (tvItem->prevSibling)
378 /* This item has a prevSibling, get the last item in the sibling's tree. */
379 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
381 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
382 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
383 else
384 return upItem;
386 else
388 /* this item does not have a prevSibling, get the parent */
389 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
394 /***************************************************************************
395 * This method returns the next physical item in the treeview not
396 * considering the tree hierarchy.
398 static TREEVIEW_ITEM *
399 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
402 * If this item has children and is expanded, return the first child
404 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
406 return tvItem->firstChild;
411 * try to get the sibling
413 if (tvItem->nextSibling)
414 return tvItem->nextSibling;
417 * Otherwise, get the parent's sibling.
419 while (tvItem->parent)
421 tvItem = tvItem->parent;
423 if (tvItem->nextSibling)
424 return tvItem->nextSibling;
427 return NULL;
430 /***************************************************************************
431 * This method returns the nth item starting at the given item. It returns
432 * the last item (or first) we we run out of items.
434 * Will scroll backward if count is <0.
435 * forward if count is >0.
437 static TREEVIEW_ITEM *
438 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
439 LONG count)
441 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
442 TREEVIEW_ITEM *previousItem;
444 assert(wineItem != NULL);
446 if (count > 0)
448 next_item = TREEVIEW_GetNextListItem;
450 else if (count < 0)
452 count = -count;
453 next_item = TREEVIEW_GetPrevListItem;
455 else
456 return wineItem;
460 previousItem = wineItem;
461 wineItem = next_item(infoPtr, wineItem);
463 } while (--count && wineItem != NULL);
466 return wineItem ? wineItem : previousItem;
469 /* Notifications ************************************************************/
471 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
473 if (!infoPtr->bNtfUnicode) {
474 switch (code) {
475 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
476 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
477 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
478 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
479 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
480 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
481 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
482 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
483 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
484 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
485 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
486 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
489 return code;
492 static inline BOOL
493 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPNMHDR pnmh)
495 TRACE("wParam=%ld, lParam=%p\n", wParam, pnmh);
496 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, (LPARAM)pnmh);
499 static BOOL
500 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
502 NMHDR nmhdr;
503 HWND hwnd = infoPtr->hwnd;
505 TRACE("%d\n", code);
506 nmhdr.hwndFrom = hwnd;
507 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
508 nmhdr.code = get_notifycode(infoPtr, code);
510 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, &nmhdr);
513 static VOID
514 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
516 tvItem->mask = mask;
517 tvItem->hItem = item;
518 tvItem->state = item->state;
519 tvItem->stateMask = 0;
520 tvItem->iImage = item->iImage;
521 tvItem->iSelectedImage = item->iSelectedImage;
522 tvItem->cChildren = item->cChildren;
523 tvItem->lParam = item->lParam;
525 if(mask & TVIF_TEXT)
527 if (!infoPtr->bNtfUnicode)
529 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
530 tvItem->pszText = Alloc (tvItem->cchTextMax);
531 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
533 else
535 tvItem->cchTextMax = item->cchTextMax;
536 tvItem->pszText = item->pszText;
539 else
541 tvItem->cchTextMax = 0;
542 tvItem->pszText = NULL;
546 static BOOL
547 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
548 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
550 HWND hwnd = infoPtr->hwnd;
551 NMTREEVIEWW nmhdr;
552 BOOL ret;
554 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
555 code, action, oldItem, newItem);
557 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
559 nmhdr.hdr.hwndFrom = hwnd;
560 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
561 nmhdr.hdr.code = get_notifycode(infoPtr, code);
562 nmhdr.action = action;
564 if (oldItem)
565 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
567 if (newItem)
568 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
570 nmhdr.ptDrag.x = 0;
571 nmhdr.ptDrag.y = 0;
573 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
574 if (!infoPtr->bNtfUnicode)
576 Free(nmhdr.itemOld.pszText);
577 Free(nmhdr.itemNew.pszText);
579 return ret;
582 static BOOL
583 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
584 HTREEITEM dragItem, POINT pt)
586 HWND hwnd = infoPtr->hwnd;
587 NMTREEVIEWW nmhdr;
589 TRACE("code:%d dragitem:%p\n", code, dragItem);
591 nmhdr.hdr.hwndFrom = hwnd;
592 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
593 nmhdr.hdr.code = get_notifycode(infoPtr, code);
594 nmhdr.action = 0;
595 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
596 nmhdr.itemNew.hItem = dragItem;
597 nmhdr.itemNew.state = dragItem->state;
598 nmhdr.itemNew.lParam = dragItem->lParam;
600 nmhdr.ptDrag.x = pt.x;
601 nmhdr.ptDrag.y = pt.y;
603 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
607 static BOOL
608 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
609 HDC hdc, RECT rc)
611 HWND hwnd = infoPtr->hwnd;
612 NMTVCUSTOMDRAW nmcdhdr;
613 LPNMCUSTOMDRAW nmcd;
615 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
617 nmcd = &nmcdhdr.nmcd;
618 nmcd->hdr.hwndFrom = hwnd;
619 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
620 nmcd->hdr.code = NM_CUSTOMDRAW;
621 nmcd->dwDrawStage = dwDrawStage;
622 nmcd->hdc = hdc;
623 nmcd->rc = rc;
624 nmcd->dwItemSpec = 0;
625 nmcd->uItemState = 0;
626 nmcd->lItemlParam = 0;
627 nmcdhdr.clrText = infoPtr->clrText;
628 nmcdhdr.clrTextBk = infoPtr->clrBk;
629 nmcdhdr.iLevel = 0;
631 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr.nmcd.hdr);
636 /* FIXME: need to find out when the flags in uItemState need to be set */
638 static BOOL
639 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
640 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
641 NMTVCUSTOMDRAW *nmcdhdr)
643 HWND hwnd = infoPtr->hwnd;
644 LPNMCUSTOMDRAW nmcd;
645 DWORD dwDrawStage;
646 DWORD_PTR dwItemSpec;
647 UINT uItemState;
649 dwDrawStage = CDDS_ITEM | uItemDrawState;
650 dwItemSpec = (DWORD_PTR)wineItem;
651 uItemState = 0;
652 if (wineItem->state & TVIS_SELECTED)
653 uItemState |= CDIS_SELECTED;
654 if (wineItem == infoPtr->selectedItem)
655 uItemState |= CDIS_FOCUS;
656 if (wineItem == infoPtr->hotItem)
657 uItemState |= CDIS_HOT;
659 nmcd = &nmcdhdr->nmcd;
660 nmcd->hdr.hwndFrom = hwnd;
661 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
662 nmcd->hdr.code = NM_CUSTOMDRAW;
663 nmcd->dwDrawStage = dwDrawStage;
664 nmcd->hdc = hdc;
665 nmcd->rc = wineItem->rect;
666 nmcd->dwItemSpec = dwItemSpec;
667 nmcd->uItemState = uItemState;
668 nmcd->lItemlParam = wineItem->lParam;
669 nmcdhdr->iLevel = wineItem->iLevel;
671 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
672 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
673 nmcd->uItemState, nmcd->lItemlParam);
675 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr->nmcd.hdr);
678 static BOOL
679 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
681 HWND hwnd = infoPtr->hwnd;
682 NMTVDISPINFOW tvdi;
683 BOOL ret;
685 tvdi.hdr.hwndFrom = hwnd;
686 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
687 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
689 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
690 &tvdi.item, editItem);
692 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
694 if (!infoPtr->bNtfUnicode)
695 Free(tvdi.item.pszText);
697 return ret;
700 static void
701 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
702 UINT mask)
704 NMTVDISPINFOEXW callback;
705 HWND hwnd = infoPtr->hwnd;
707 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, wineItem->callbackMask);
708 mask &= wineItem->callbackMask;
710 if (mask == 0) return;
712 callback.hdr.hwndFrom = hwnd;
713 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
714 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
716 /* 'state' always contains valid value, as well as 'lParam'.
717 * All other parameters are uninitialized.
719 callback.item.pszText = wineItem->pszText;
720 callback.item.cchTextMax = wineItem->cchTextMax;
721 callback.item.mask = mask;
722 callback.item.hItem = wineItem;
723 callback.item.state = wineItem->state;
724 callback.item.lParam = wineItem->lParam;
726 /* If text is changed we need to recalculate textWidth */
727 if (mask & TVIF_TEXT)
728 wineItem->textWidth = 0;
730 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, &callback.hdr);
731 TRACE("resulting code 0x%08x\n", callback.hdr.code);
733 /* It may have changed due to a call to SetItem. */
734 mask &= wineItem->callbackMask;
736 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
738 /* Instead of copying text into our buffer user specified his own */
739 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
740 LPWSTR newText;
741 int buflen;
742 int len = MultiByteToWideChar( CP_ACP, 0,
743 (LPSTR)callback.item.pszText, -1,
744 NULL, 0);
745 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
746 newText = ReAlloc(wineItem->pszText, buflen);
748 TRACE("returned str %s, len=%d, buflen=%d\n",
749 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
751 if (newText)
753 wineItem->pszText = newText;
754 MultiByteToWideChar( CP_ACP, 0,
755 (LPSTR)callback.item.pszText, -1,
756 wineItem->pszText, buflen/sizeof(WCHAR));
757 wineItem->cchTextMax = buflen/sizeof(WCHAR);
759 /* If ReAlloc fails we have nothing to do, but keep original text */
761 else {
762 int len = max(lstrlenW(callback.item.pszText) + 1,
763 TEXT_CALLBACK_SIZE);
764 LPWSTR newText = ReAlloc(wineItem->pszText, len);
766 TRACE("returned wstr %s, len=%d\n",
767 debugstr_w(callback.item.pszText), len);
769 if (newText)
771 wineItem->pszText = newText;
772 strcpyW(wineItem->pszText, callback.item.pszText);
773 wineItem->cchTextMax = len;
775 /* If ReAlloc fails we have nothing to do, but keep original text */
778 else if (mask & TVIF_TEXT) {
779 /* User put text into our buffer, that is ok unless A string */
780 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
781 LPWSTR newText;
782 LPWSTR oldText = NULL;
783 int buflen;
784 int len = MultiByteToWideChar( CP_ACP, 0,
785 (LPSTR)callback.item.pszText, -1,
786 NULL, 0);
787 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
788 newText = Alloc(buflen);
790 TRACE("same buffer str %s, len=%d, buflen=%d\n",
791 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
793 if (newText)
795 oldText = wineItem->pszText;
796 wineItem->pszText = newText;
797 MultiByteToWideChar( CP_ACP, 0,
798 (LPSTR)callback.item.pszText, -1,
799 wineItem->pszText, buflen/sizeof(WCHAR));
800 wineItem->cchTextMax = buflen/sizeof(WCHAR);
801 Free(oldText);
806 if (mask & TVIF_IMAGE)
807 wineItem->iImage = callback.item.iImage;
809 if (mask & TVIF_SELECTEDIMAGE)
810 wineItem->iSelectedImage = callback.item.iSelectedImage;
812 if (mask & TVIF_EXPANDEDIMAGE)
813 wineItem->iExpandedImage = callback.item.iExpandedImage;
815 if (mask & TVIF_CHILDREN)
816 wineItem->cChildren = callback.item.cChildren;
818 /* These members are now permanently set. */
819 if (callback.item.mask & TVIF_DI_SETITEM)
820 wineItem->callbackMask &= ~callback.item.mask;
823 /***************************************************************************
824 * This function uses cChildren field to decide whether the item has
825 * children or not.
826 * Note: if this returns TRUE, the child items may not actually exist,
827 * they could be virtual.
829 * Just use wineItem->firstChild to check for physical children.
831 static BOOL
832 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
834 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
836 return wineItem->cChildren > 0;
839 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
841 INT format;
843 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
845 if (nCommand != NF_REQUERY) return 0;
847 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
848 TRACE("format=%d\n", format);
850 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
852 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
854 return format;
857 /* Item Position ********************************************************/
859 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
860 static VOID
861 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
863 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
864 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
865 > TVS_LINESATROOT);
867 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
868 - infoPtr->scrollX;
869 item->stateOffset = item->linesOffset + infoPtr->uIndent;
870 item->imageOffset = item->stateOffset
871 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
872 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
875 static VOID
876 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
878 HDC hdc;
879 HFONT hOldFont=0;
880 SIZE sz;
882 /* DRAW's OM docker creates items like this */
883 if (item->pszText == NULL)
885 item->textWidth = 0;
886 return;
889 if (hDC != 0)
891 hdc = hDC;
893 else
895 hdc = GetDC(infoPtr->hwnd);
896 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
899 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
900 item->textWidth = sz.cx;
902 if (hDC == 0)
904 SelectObject(hdc, hOldFont);
905 ReleaseDC(0, hdc);
909 static VOID
910 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
912 item->rect.top = infoPtr->uItemHeight *
913 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
915 item->rect.bottom = item->rect.top
916 + infoPtr->uItemHeight * item->iIntegral - 1;
918 item->rect.left = 0;
919 item->rect.right = infoPtr->clientWidth;
922 /* We know that only items after start need their order updated. */
923 static void
924 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
926 TREEVIEW_ITEM *item;
927 int order;
929 if (!start)
931 start = infoPtr->root->firstChild;
932 order = 0;
934 else
935 order = start->visibleOrder;
937 for (item = start; item != NULL;
938 item = TREEVIEW_GetNextListItem(infoPtr, item))
940 if (!ISVISIBLE(item) && order > 0)
941 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
942 item->visibleOrder = order;
943 order += item->iIntegral;
946 infoPtr->maxVisibleOrder = order;
948 for (item = start; item != NULL;
949 item = TREEVIEW_GetNextListItem(infoPtr, item))
951 TREEVIEW_ComputeItemRect(infoPtr, item);
956 /* Update metrics of all items in selected subtree.
957 * root must be expanded
959 static VOID
960 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
962 TREEVIEW_ITEM *sibling;
963 HDC hdc;
964 HFONT hOldFont;
966 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
967 return;
969 root->state &= ~TVIS_EXPANDED;
970 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
971 root->state |= TVIS_EXPANDED;
973 hdc = GetDC(infoPtr->hwnd);
974 hOldFont = SelectObject(hdc, infoPtr->hFont);
976 for (; root != sibling;
977 root = TREEVIEW_GetNextListItem(infoPtr, root))
979 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
981 if (root->callbackMask & TVIF_TEXT)
982 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
984 if (root->textWidth == 0)
986 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
987 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
991 SelectObject(hdc, hOldFont);
992 ReleaseDC(infoPtr->hwnd, hdc);
995 /* Item Allocation **********************************************************/
997 static TREEVIEW_ITEM *
998 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1000 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1002 if (!newItem)
1003 return NULL;
1005 /* I_IMAGENONE would make more sense but this is neither what is
1006 * documented (MSDN doesn't specify) nor what Windows actually does
1007 * (it sets it to zero)... and I can so imagine an application using
1008 * inc/dec to toggle the images. */
1009 newItem->iImage = 0;
1010 newItem->iSelectedImage = 0;
1011 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1013 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1015 Free(newItem);
1016 return NULL;
1019 return newItem;
1022 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1023 * free item->pszText. */
1024 static void
1025 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1027 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1028 if (infoPtr->selectedItem == item)
1029 infoPtr->selectedItem = NULL;
1030 if (infoPtr->hotItem == item)
1031 infoPtr->hotItem = NULL;
1032 if (infoPtr->focusedItem == item)
1033 infoPtr->focusedItem = NULL;
1034 if (infoPtr->firstVisible == item)
1035 infoPtr->firstVisible = NULL;
1036 if (infoPtr->dropItem == item)
1037 infoPtr->dropItem = NULL;
1038 if (infoPtr->insertMarkItem == item)
1039 infoPtr->insertMarkItem = NULL;
1040 Free(item);
1044 /* Item Insertion *******************************************************/
1046 /***************************************************************************
1047 * This method inserts newItem before sibling as a child of parent.
1048 * sibling can be NULL, but only if parent has no children.
1050 static void
1051 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1052 TREEVIEW_ITEM *parent)
1054 assert(parent != NULL);
1056 if (sibling != NULL)
1058 assert(sibling->parent == parent);
1060 if (sibling->prevSibling != NULL)
1061 sibling->prevSibling->nextSibling = newItem;
1063 newItem->prevSibling = sibling->prevSibling;
1064 sibling->prevSibling = newItem;
1066 else
1067 newItem->prevSibling = NULL;
1069 newItem->nextSibling = sibling;
1071 if (parent->firstChild == sibling)
1072 parent->firstChild = newItem;
1074 if (parent->lastChild == NULL)
1075 parent->lastChild = newItem;
1078 /***************************************************************************
1079 * This method inserts newItem after sibling as a child of parent.
1080 * sibling can be NULL, but only if parent has no children.
1082 static void
1083 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1084 TREEVIEW_ITEM *parent)
1086 assert(parent != NULL);
1088 if (sibling != NULL)
1090 assert(sibling->parent == parent);
1092 if (sibling->nextSibling != NULL)
1093 sibling->nextSibling->prevSibling = newItem;
1095 newItem->nextSibling = sibling->nextSibling;
1096 sibling->nextSibling = newItem;
1098 else
1099 newItem->nextSibling = NULL;
1101 newItem->prevSibling = sibling;
1103 if (parent->lastChild == sibling)
1104 parent->lastChild = newItem;
1106 if (parent->firstChild == NULL)
1107 parent->firstChild = newItem;
1110 static BOOL
1111 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1112 const TVITEMEXW *tvItem, BOOL isW)
1114 UINT callbackClear = 0;
1115 UINT callbackSet = 0;
1117 TRACE("item %p\n", wineItem);
1118 /* Do this first in case it fails. */
1119 if (tvItem->mask & TVIF_TEXT)
1121 wineItem->textWidth = 0; /* force width recalculation */
1122 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1124 int len;
1125 LPWSTR newText;
1126 if (isW)
1127 len = lstrlenW(tvItem->pszText) + 1;
1128 else
1129 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1131 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1133 if (newText == NULL) return FALSE;
1135 callbackClear |= TVIF_TEXT;
1137 wineItem->pszText = newText;
1138 wineItem->cchTextMax = len;
1139 if (isW)
1140 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1141 else
1142 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1143 wineItem->pszText, len);
1145 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1147 else
1149 callbackSet |= TVIF_TEXT;
1151 wineItem->pszText = ReAlloc(wineItem->pszText,
1152 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1153 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1154 TRACE("setting callback, item %p\n", wineItem);
1158 if (tvItem->mask & TVIF_CHILDREN)
1160 wineItem->cChildren = tvItem->cChildren;
1162 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1163 callbackSet |= TVIF_CHILDREN;
1164 else
1165 callbackClear |= TVIF_CHILDREN;
1168 if (tvItem->mask & TVIF_IMAGE)
1170 wineItem->iImage = tvItem->iImage;
1172 if (wineItem->iImage == I_IMAGECALLBACK)
1173 callbackSet |= TVIF_IMAGE;
1174 else
1175 callbackClear |= TVIF_IMAGE;
1178 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1180 wineItem->iSelectedImage = tvItem->iSelectedImage;
1182 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1183 callbackSet |= TVIF_SELECTEDIMAGE;
1184 else
1185 callbackClear |= TVIF_SELECTEDIMAGE;
1188 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1190 wineItem->iExpandedImage = tvItem->iExpandedImage;
1192 if (wineItem->iExpandedImage == I_IMAGECALLBACK)
1193 callbackSet |= TVIF_EXPANDEDIMAGE;
1194 else
1195 callbackClear |= TVIF_EXPANDEDIMAGE;
1198 if (tvItem->mask & TVIF_PARAM)
1199 wineItem->lParam = tvItem->lParam;
1201 /* If the application sets TVIF_INTEGRAL without
1202 * supplying a TVITEMEX structure, it's toast. */
1203 if (tvItem->mask & TVIF_INTEGRAL)
1204 wineItem->iIntegral = tvItem->iIntegral;
1206 if (tvItem->mask & TVIF_STATE)
1208 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1209 tvItem->stateMask);
1210 wineItem->state &= ~tvItem->stateMask;
1211 wineItem->state |= (tvItem->state & tvItem->stateMask);
1214 if (tvItem->mask & TVIF_STATEEX)
1216 FIXME("New extended state: %x\n", tvItem->uStateEx);
1219 wineItem->callbackMask |= callbackSet;
1220 wineItem->callbackMask &= ~callbackClear;
1222 return TRUE;
1225 /* Note that the new item is pre-zeroed. */
1226 static LRESULT
1227 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1229 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1230 HTREEITEM insertAfter;
1231 TREEVIEW_ITEM *newItem, *parentItem;
1232 BOOL bTextUpdated = FALSE;
1234 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1236 parentItem = infoPtr->root;
1238 else
1240 parentItem = ptdi->hParent;
1242 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1244 WARN("invalid parent %p\n", parentItem);
1245 return 0;
1249 insertAfter = ptdi->hInsertAfter;
1251 /* Validate this now for convenience. */
1252 switch ((DWORD_PTR)insertAfter)
1254 case (DWORD_PTR)TVI_FIRST:
1255 case (DWORD_PTR)TVI_LAST:
1256 case (DWORD_PTR)TVI_SORT:
1257 break;
1259 default:
1260 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1261 insertAfter->parent != parentItem)
1263 WARN("invalid insert after %p\n", insertAfter);
1264 insertAfter = TVI_LAST;
1268 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1269 (tvItem->mask & TVIF_TEXT)
1270 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1271 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1272 : "<no label>");
1274 newItem = TREEVIEW_AllocateItem(infoPtr);
1275 if (newItem == NULL)
1276 return 0;
1278 newItem->parent = parentItem;
1279 newItem->iIntegral = 1;
1280 newItem->visibleOrder = -1;
1282 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1283 return 0;
1285 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1287 infoPtr->uNumItems++;
1289 switch ((DWORD_PTR)insertAfter)
1291 case (DWORD_PTR)TVI_FIRST:
1293 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1294 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1295 if (infoPtr->firstVisible == originalFirst)
1296 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1298 break;
1300 case (DWORD_PTR)TVI_LAST:
1301 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1302 break;
1304 /* hInsertAfter names a specific item we want to insert after */
1305 default:
1306 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1307 break;
1309 case (DWORD_PTR)TVI_SORT:
1311 TREEVIEW_ITEM *aChild;
1312 TREEVIEW_ITEM *previousChild = NULL;
1313 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1314 BOOL bItemInserted = FALSE;
1316 aChild = parentItem->firstChild;
1318 bTextUpdated = TRUE;
1319 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1321 /* Iterate the parent children to see where we fit in */
1322 while (aChild != NULL)
1324 INT comp;
1326 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1327 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1329 if (comp < 0) /* we are smaller than the current one */
1331 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1332 if (infoPtr->firstVisible == originalFirst &&
1333 aChild == originalFirst)
1334 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1335 bItemInserted = TRUE;
1336 break;
1338 else if (comp > 0) /* we are bigger than the current one */
1340 previousChild = aChild;
1342 /* This will help us to exit if there is no more sibling */
1343 aChild = (aChild->nextSibling == 0)
1344 ? NULL
1345 : aChild->nextSibling;
1347 /* Look at the next item */
1348 continue;
1350 else if (comp == 0)
1353 * An item with this name is already existing, therefore,
1354 * we add after the one we found
1356 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1357 bItemInserted = TRUE;
1358 break;
1363 * we reach the end of the child list and the item has not
1364 * yet been inserted, therefore, insert it after the last child.
1366 if ((!bItemInserted) && (aChild == NULL))
1367 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1369 break;
1374 TRACE("new item %p; parent %p, mask %x\n", newItem,
1375 newItem->parent, tvItem->mask);
1377 newItem->iLevel = newItem->parent->iLevel + 1;
1379 if (newItem->parent->cChildren == 0)
1380 newItem->parent->cChildren = 1;
1382 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1384 if (STATEIMAGEINDEX(newItem->state) == 0)
1385 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1388 if (infoPtr->firstVisible == NULL)
1389 infoPtr->firstVisible = newItem;
1391 TREEVIEW_VerifyTree(infoPtr);
1393 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1395 if (parentItem == infoPtr->root ||
1396 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1398 TREEVIEW_ITEM *item;
1399 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1401 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1402 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1404 if (!bTextUpdated)
1405 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1407 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1408 TREEVIEW_UpdateScrollBars(infoPtr);
1410 * if the item was inserted in a visible part of the tree,
1411 * invalidate it, as well as those after it
1413 for (item = newItem;
1414 item != NULL;
1415 item = TREEVIEW_GetNextListItem(infoPtr, item))
1416 TREEVIEW_Invalidate(infoPtr, item);
1418 else
1420 /* refresh treeview if newItem is the first item inserted under parentItem */
1421 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1423 /* parent got '+' - update it */
1424 TREEVIEW_Invalidate(infoPtr, parentItem);
1428 return (LRESULT)newItem;
1431 /* Item Deletion ************************************************************/
1432 static void
1433 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1435 static void
1436 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1438 TREEVIEW_ITEM *kill = parentItem->firstChild;
1440 while (kill != NULL)
1442 TREEVIEW_ITEM *next = kill->nextSibling;
1444 TREEVIEW_RemoveItem(infoPtr, kill);
1446 kill = next;
1449 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1450 assert(parentItem->firstChild == NULL);
1451 assert(parentItem->lastChild == NULL);
1454 static void
1455 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1457 TREEVIEW_ITEM *parentItem = item->parent;
1459 assert(item != NULL);
1460 assert(item->parent != NULL); /* i.e. it must not be the root */
1462 if (parentItem->firstChild == item)
1463 parentItem->firstChild = item->nextSibling;
1465 if (parentItem->lastChild == item)
1466 parentItem->lastChild = item->prevSibling;
1468 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1469 && parentItem->cChildren > 0)
1470 parentItem->cChildren = 0;
1472 if (item->prevSibling)
1473 item->prevSibling->nextSibling = item->nextSibling;
1475 if (item->nextSibling)
1476 item->nextSibling->prevSibling = item->prevSibling;
1479 static void
1480 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1482 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1484 if (wineItem->firstChild)
1485 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1487 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1488 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1490 TREEVIEW_UnlinkItem(wineItem);
1492 infoPtr->uNumItems--;
1494 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1495 Free(wineItem->pszText);
1497 TREEVIEW_FreeItem(infoPtr, wineItem);
1501 /* Empty out the tree. */
1502 static void
1503 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1505 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1507 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1510 static LRESULT
1511 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1513 TREEVIEW_ITEM *newSelection = NULL;
1514 TREEVIEW_ITEM *newFirstVisible = NULL;
1515 TREEVIEW_ITEM *parent, *prev = NULL;
1516 BOOL visible = FALSE;
1518 if (wineItem == TVI_ROOT || !wineItem)
1520 TRACE("TVI_ROOT\n");
1521 parent = infoPtr->root;
1522 newSelection = NULL;
1523 visible = TRUE;
1524 TREEVIEW_RemoveTree(infoPtr);
1526 else
1528 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1529 return FALSE;
1531 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1532 parent = wineItem->parent;
1534 if (ISVISIBLE(wineItem))
1536 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1537 visible = TRUE;
1540 if (infoPtr->selectedItem != NULL
1541 && (wineItem == infoPtr->selectedItem
1542 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1544 if (wineItem->nextSibling)
1545 newSelection = wineItem->nextSibling;
1546 else if (wineItem->parent != infoPtr->root)
1547 newSelection = wineItem->parent;
1548 else
1549 newSelection = wineItem->prevSibling;
1550 TRACE("newSelection = %p\n", newSelection);
1553 if (infoPtr->firstVisible == wineItem)
1555 if (wineItem->nextSibling)
1556 newFirstVisible = wineItem->nextSibling;
1557 else if (wineItem->prevSibling)
1558 newFirstVisible = wineItem->prevSibling;
1559 else if (wineItem->parent != infoPtr->root)
1560 newFirstVisible = wineItem->parent;
1561 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1563 else
1564 newFirstVisible = infoPtr->firstVisible;
1566 TREEVIEW_RemoveItem(infoPtr, wineItem);
1569 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1570 if (!infoPtr->selectedItem && newSelection)
1572 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1573 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1576 /* Validate insertMark dropItem.
1577 * hotItem ??? - used for comparison only.
1579 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1580 infoPtr->insertMarkItem = 0;
1582 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1583 infoPtr->dropItem = 0;
1585 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1586 newFirstVisible = infoPtr->root->firstChild;
1588 TREEVIEW_VerifyTree(infoPtr);
1590 if (!infoPtr->bRedraw) return TRUE;
1592 if (visible)
1594 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1595 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1596 TREEVIEW_UpdateScrollBars(infoPtr);
1597 TREEVIEW_Invalidate(infoPtr, NULL);
1599 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1601 /* parent lost '+/-' - update it */
1602 TREEVIEW_Invalidate(infoPtr, parent);
1605 return TRUE;
1609 /* Get/Set Messages *********************************************************/
1610 static LRESULT
1611 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1613 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1615 if (infoPtr->bRedraw)
1617 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1618 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1619 TREEVIEW_UpdateScrollBars(infoPtr);
1620 TREEVIEW_Invalidate(infoPtr, NULL);
1622 return 0;
1625 static LRESULT
1626 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1628 TRACE("\n");
1629 return infoPtr->uIndent;
1632 static LRESULT
1633 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1635 TRACE("\n");
1637 if (newIndent < MINIMUM_INDENT)
1638 newIndent = MINIMUM_INDENT;
1640 if (infoPtr->uIndent != newIndent)
1642 infoPtr->uIndent = newIndent;
1643 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1644 TREEVIEW_UpdateScrollBars(infoPtr);
1645 TREEVIEW_Invalidate(infoPtr, NULL);
1648 return 0;
1652 static LRESULT
1653 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1655 TRACE("\n");
1656 return (LRESULT)infoPtr->hwndToolTip;
1659 static LRESULT
1660 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1662 HWND prevToolTip;
1664 TRACE("\n");
1665 prevToolTip = infoPtr->hwndToolTip;
1666 infoPtr->hwndToolTip = hwndTT;
1668 return (LRESULT)prevToolTip;
1671 static LRESULT
1672 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1674 BOOL rc = infoPtr->bNtfUnicode;
1675 infoPtr->bNtfUnicode = fUnicode;
1676 return rc;
1679 static LRESULT
1680 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1682 return infoPtr->bNtfUnicode;
1685 static LRESULT
1686 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1688 return infoPtr->uScrollTime;
1691 static LRESULT
1692 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1694 UINT uOldScrollTime = infoPtr->uScrollTime;
1696 infoPtr->uScrollTime = min(uScrollTime, 100);
1698 return uOldScrollTime;
1702 static LRESULT
1703 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1705 TRACE("\n");
1707 switch (wParam)
1709 case TVSIL_NORMAL:
1710 return (LRESULT)infoPtr->himlNormal;
1712 case TVSIL_STATE:
1713 return (LRESULT)infoPtr->himlState;
1715 default:
1716 return 0;
1720 #define TVHEIGHT_MIN 16
1721 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1723 /* Compute the natural height for items. */
1724 static UINT
1725 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1727 TEXTMETRICW tm;
1728 HDC hdc = GetDC(0);
1729 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1730 UINT height;
1732 /* Height is the maximum of:
1733 * 16 (a hack because our fonts are tiny), and
1734 * The text height + border & margin, and
1735 * The size of the normal image list
1737 GetTextMetricsW(hdc, &tm);
1738 SelectObject(hdc, hOldFont);
1739 ReleaseDC(0, hdc);
1741 height = TVHEIGHT_MIN;
1742 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1743 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1744 if (height < infoPtr->normalImageHeight)
1745 height = infoPtr->normalImageHeight;
1747 /* Round down, unless we support odd ("non even") heights. */
1748 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1749 height &= ~1;
1751 return height;
1754 static LRESULT
1755 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1757 HIMAGELIST himlOld = 0;
1758 int oldWidth = infoPtr->normalImageWidth;
1759 int oldHeight = infoPtr->normalImageHeight;
1762 TRACE("%lx,%p\n", wParam, himlNew);
1764 switch (wParam)
1766 case TVSIL_NORMAL:
1767 himlOld = infoPtr->himlNormal;
1768 infoPtr->himlNormal = himlNew;
1770 if (himlNew != NULL)
1771 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1772 &infoPtr->normalImageHeight);
1773 else
1775 infoPtr->normalImageWidth = 0;
1776 infoPtr->normalImageHeight = 0;
1779 break;
1781 case TVSIL_STATE:
1782 himlOld = infoPtr->himlState;
1783 infoPtr->himlState = himlNew;
1785 if (himlNew != NULL)
1786 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1787 &infoPtr->stateImageHeight);
1788 else
1790 infoPtr->stateImageWidth = 0;
1791 infoPtr->stateImageHeight = 0;
1794 break;
1797 if (oldWidth != infoPtr->normalImageWidth ||
1798 oldHeight != infoPtr->normalImageHeight)
1800 BOOL bRecalcVisible = FALSE;
1802 if (oldHeight != infoPtr->normalImageHeight &&
1803 !infoPtr->bHeightSet)
1805 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1806 bRecalcVisible = TRUE;
1809 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1810 infoPtr->normalImageWidth != infoPtr->uIndent)
1812 infoPtr->uIndent = infoPtr->normalImageWidth;
1813 bRecalcVisible = TRUE;
1816 if (bRecalcVisible)
1817 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1819 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1820 TREEVIEW_UpdateScrollBars(infoPtr);
1823 TREEVIEW_Invalidate(infoPtr, NULL);
1825 return (LRESULT)himlOld;
1828 static LRESULT
1829 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1831 INT prevHeight = infoPtr->uItemHeight;
1833 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1834 if (newHeight == -1)
1836 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1837 infoPtr->bHeightSet = FALSE;
1839 else
1841 if (newHeight == 0) newHeight = 1;
1842 infoPtr->uItemHeight = newHeight;
1843 infoPtr->bHeightSet = TRUE;
1846 /* Round down, unless we support odd ("non even") heights. */
1847 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1849 infoPtr->uItemHeight &= ~1;
1850 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1853 if (infoPtr->uItemHeight != prevHeight)
1855 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1856 TREEVIEW_UpdateScrollBars(infoPtr);
1857 TREEVIEW_Invalidate(infoPtr, NULL);
1860 return prevHeight;
1863 static LRESULT
1864 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1866 TRACE("\n");
1867 return infoPtr->uItemHeight;
1871 static LRESULT
1872 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1874 TRACE("%p\n", infoPtr->hFont);
1875 return (LRESULT)infoPtr->hFont;
1879 static INT CALLBACK
1880 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1882 (void)unused;
1884 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1886 return 1;
1889 static LRESULT
1890 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1892 UINT uHeight = infoPtr->uItemHeight;
1894 TRACE("%p %i\n", hFont, bRedraw);
1896 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1898 DeleteObject(infoPtr->hBoldFont);
1899 DeleteObject(infoPtr->hUnderlineFont);
1900 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1901 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1903 if (!infoPtr->bHeightSet)
1904 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1906 if (uHeight != infoPtr->uItemHeight)
1907 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1909 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1911 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1912 TREEVIEW_UpdateScrollBars(infoPtr);
1914 if (bRedraw)
1915 TREEVIEW_Invalidate(infoPtr, NULL);
1917 return 0;
1921 static LRESULT
1922 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1924 TRACE("\n");
1925 return (LRESULT)infoPtr->clrLine;
1928 static LRESULT
1929 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1931 COLORREF prevColor = infoPtr->clrLine;
1933 TRACE("\n");
1934 infoPtr->clrLine = color;
1935 return (LRESULT)prevColor;
1939 static LRESULT
1940 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1942 TRACE("\n");
1943 return (LRESULT)infoPtr->clrText;
1946 static LRESULT
1947 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1949 COLORREF prevColor = infoPtr->clrText;
1951 TRACE("\n");
1952 infoPtr->clrText = color;
1954 if (infoPtr->clrText != prevColor)
1955 TREEVIEW_Invalidate(infoPtr, NULL);
1957 return (LRESULT)prevColor;
1961 static LRESULT
1962 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1964 TRACE("\n");
1965 return (LRESULT)infoPtr->clrBk;
1968 static LRESULT
1969 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1971 COLORREF prevColor = infoPtr->clrBk;
1973 TRACE("\n");
1974 infoPtr->clrBk = newColor;
1976 if (newColor != prevColor)
1977 TREEVIEW_Invalidate(infoPtr, NULL);
1979 return (LRESULT)prevColor;
1983 static LRESULT
1984 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1986 TRACE("\n");
1987 return (LRESULT)infoPtr->clrInsertMark;
1990 static LRESULT
1991 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1993 COLORREF prevColor = infoPtr->clrInsertMark;
1995 TRACE("%x\n", color);
1996 infoPtr->clrInsertMark = color;
1998 return (LRESULT)prevColor;
2002 static LRESULT
2003 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2005 TRACE("%d %p\n", wParam, item);
2007 if (!TREEVIEW_ValidItem(infoPtr, item))
2008 return 0;
2010 infoPtr->insertBeforeorAfter = wParam;
2011 infoPtr->insertMarkItem = item;
2013 TREEVIEW_Invalidate(infoPtr, NULL);
2015 return 1;
2019 /************************************************************************
2020 * Some serious braindamage here. lParam is a pointer to both the
2021 * input HTREEITEM and the output RECT.
2023 static LRESULT
2024 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2026 TREEVIEW_ITEM *wineItem;
2027 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2029 TRACE("\n");
2031 * validate parameters
2033 if (pItem == NULL)
2034 return FALSE;
2036 wineItem = *pItem;
2037 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2038 return FALSE;
2041 * If wParam is TRUE return the text size otherwise return
2042 * the whole item size
2044 if (fTextRect)
2046 /* Windows does not send TVN_GETDISPINFO here. */
2048 lpRect->top = wineItem->rect.top;
2049 lpRect->bottom = wineItem->rect.bottom;
2051 lpRect->left = wineItem->textOffset;
2052 if (!wineItem->textWidth)
2053 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2055 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2057 else
2059 *lpRect = wineItem->rect;
2062 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2064 return TRUE;
2067 static inline LRESULT
2068 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2070 /* Surprise! This does not take integral height into account. */
2071 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2072 return infoPtr->clientHeight / infoPtr->uItemHeight;
2076 static LRESULT
2077 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2079 TREEVIEW_ITEM *wineItem;
2081 wineItem = tvItem->hItem;
2082 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2083 return FALSE;
2085 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2087 if (tvItem->mask & TVIF_CHILDREN)
2089 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2090 FIXME("I_CHILDRENCALLBACK not supported\n");
2091 tvItem->cChildren = wineItem->cChildren;
2094 if (tvItem->mask & TVIF_HANDLE)
2095 tvItem->hItem = wineItem;
2097 if (tvItem->mask & TVIF_IMAGE)
2098 tvItem->iImage = wineItem->iImage;
2100 if (tvItem->mask & TVIF_INTEGRAL)
2101 tvItem->iIntegral = wineItem->iIntegral;
2103 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2104 tvItem->lParam = wineItem->lParam;
2106 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2107 tvItem->iSelectedImage = wineItem->iSelectedImage;
2109 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2110 tvItem->iExpandedImage = wineItem->iExpandedImage;
2112 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2113 tvItem->state = wineItem->state;
2115 if (tvItem->mask & TVIF_TEXT)
2117 if (wineItem->pszText == NULL)
2119 if (tvItem->cchTextMax > 0)
2120 tvItem->pszText[0] = '\0';
2122 else if (isW)
2124 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2126 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2127 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2129 else
2131 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2134 else
2136 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2138 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2139 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2141 else
2143 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2144 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2149 if (tvItem->mask & TVIF_STATEEX)
2151 FIXME("Extended item state not supported, returning 0.\n");
2152 tvItem->uStateEx = 0;
2155 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2156 wineItem, tvItem->pszText, tvItem->iImage, tvItem->mask);
2158 return TRUE;
2161 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2162 * which is wrong. */
2163 static LRESULT
2164 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2166 TREEVIEW_ITEM *wineItem;
2167 TREEVIEW_ITEM originalItem;
2169 wineItem = tvItem->hItem;
2171 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2172 tvItem->mask);
2174 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2175 return FALSE;
2177 /* store the original item values */
2178 originalItem = *wineItem;
2180 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2181 return FALSE;
2183 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2184 if ((tvItem->mask & TVIF_TEXT
2185 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2186 && ISVISIBLE(wineItem))
2188 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2189 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2192 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2194 /* The refresh updates everything, but we can't wait until then. */
2195 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2197 /* if any of the item's values changed and it's not a callback, redraw the item */
2198 if (item_changed(&originalItem, wineItem, tvItem))
2200 if (tvItem->mask & TVIF_INTEGRAL)
2202 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2203 TREEVIEW_UpdateScrollBars(infoPtr);
2205 TREEVIEW_Invalidate(infoPtr, NULL);
2207 else
2209 TREEVIEW_UpdateScrollBars(infoPtr);
2210 TREEVIEW_Invalidate(infoPtr, wineItem);
2215 return TRUE;
2218 static LRESULT
2219 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2221 TRACE("\n");
2223 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2224 return 0;
2226 return (wineItem->state & mask);
2229 static LRESULT
2230 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2232 TREEVIEW_ITEM *retval;
2234 retval = 0;
2236 /* handle all the global data here */
2237 switch (which)
2239 case TVGN_CHILD: /* Special case: child of 0 is root */
2240 if (wineItem)
2241 break;
2242 /* fall through */
2243 case TVGN_ROOT:
2244 retval = infoPtr->root->firstChild;
2245 break;
2247 case TVGN_CARET:
2248 retval = infoPtr->selectedItem;
2249 break;
2251 case TVGN_FIRSTVISIBLE:
2252 retval = infoPtr->firstVisible;
2253 break;
2255 case TVGN_DROPHILITE:
2256 retval = infoPtr->dropItem;
2257 break;
2259 case TVGN_LASTVISIBLE:
2260 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2261 break;
2264 if (retval)
2266 TRACE("flags:%x, returns %p\n", which, retval);
2267 return (LRESULT)retval;
2270 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2272 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2273 return FALSE;
2275 switch (which)
2277 case TVGN_NEXT:
2278 retval = wineItem->nextSibling;
2279 break;
2280 case TVGN_PREVIOUS:
2281 retval = wineItem->prevSibling;
2282 break;
2283 case TVGN_PARENT:
2284 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2285 break;
2286 case TVGN_CHILD:
2287 retval = wineItem->firstChild;
2288 break;
2289 case TVGN_NEXTVISIBLE:
2290 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2291 break;
2292 case TVGN_PREVIOUSVISIBLE:
2293 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2294 break;
2295 default:
2296 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2297 break;
2300 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2301 return (LRESULT)retval;
2305 static LRESULT
2306 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2308 TRACE(" %d\n", infoPtr->uNumItems);
2309 return (LRESULT)infoPtr->uNumItems;
2312 static VOID
2313 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2315 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2317 static const unsigned int state_table[] = { 0, 2, 1 };
2319 unsigned int state;
2321 state = STATEIMAGEINDEX(item->state);
2322 TRACE("state:%x\n", state);
2323 item->state &= ~TVIS_STATEIMAGEMASK;
2325 if (state < 3)
2326 state = state_table[state];
2328 item->state |= INDEXTOSTATEIMAGEMASK(state);
2330 TRACE("state:%x\n", state);
2331 TREEVIEW_Invalidate(infoPtr, item);
2336 /* Painting *************************************************************/
2338 /* Draw the lines and expand button for an item. Also draws one section
2339 * of the line from item's parent to item's parent's next sibling. */
2340 static void
2341 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2343 LONG centerx, centery;
2344 BOOL lar = ((infoPtr->dwStyle
2345 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2346 > TVS_LINESATROOT);
2347 HBRUSH hbr, hbrOld;
2348 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2350 if (!lar && item->iLevel == 0)
2351 return;
2353 hbr = CreateSolidBrush(clrBk);
2354 hbrOld = SelectObject(hdc, hbr);
2356 centerx = (item->linesOffset + item->stateOffset) / 2;
2357 centery = (item->rect.top + item->rect.bottom) / 2;
2359 if (infoPtr->dwStyle & TVS_HASLINES)
2361 HPEN hOldPen, hNewPen;
2362 HTREEITEM parent;
2363 LOGBRUSH lb;
2365 /* Get a dotted grey pen */
2366 lb.lbStyle = BS_SOLID;
2367 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2368 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2369 hOldPen = SelectObject(hdc, hNewPen);
2371 /* Make sure the center is on a dot (using +2 instead
2372 * of +1 gives us pixel-by-pixel compat with native) */
2373 centery = (centery + 2) & ~1;
2375 MoveToEx(hdc, item->stateOffset, centery, NULL);
2376 LineTo(hdc, centerx - 1, centery);
2378 if (item->prevSibling || item->parent != infoPtr->root)
2380 MoveToEx(hdc, centerx, item->rect.top, NULL);
2381 LineTo(hdc, centerx, centery);
2384 if (item->nextSibling)
2386 MoveToEx(hdc, centerx, centery, NULL);
2387 LineTo(hdc, centerx, item->rect.bottom + 1);
2390 /* Draw the line from our parent to its next sibling. */
2391 parent = item->parent;
2392 while (parent != infoPtr->root)
2394 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2396 if (parent->nextSibling
2397 /* skip top-levels unless TVS_LINESATROOT */
2398 && parent->stateOffset > parent->linesOffset)
2400 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2401 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2404 parent = parent->parent;
2407 SelectObject(hdc, hOldPen);
2408 DeleteObject(hNewPen);
2412 * Display the (+/-) signs
2415 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2417 if (item->cChildren)
2419 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2420 if (theme)
2422 RECT glyphRect = item->rect;
2423 glyphRect.left = item->linesOffset;
2424 glyphRect.right = item->stateOffset;
2425 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2426 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2427 &glyphRect, NULL);
2429 else
2431 LONG height = item->rect.bottom - item->rect.top;
2432 LONG width = item->stateOffset - item->linesOffset;
2433 LONG rectsize = min(height, width) / 4;
2434 /* plussize = ceil(rectsize * 3/4) */
2435 LONG plussize = (rectsize + 1) * 3 / 4;
2437 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2438 HPEN old_pen = SelectObject(hdc, new_pen);
2440 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2441 centerx + rectsize + 2, centery + rectsize + 2);
2443 SelectObject(hdc, old_pen);
2444 DeleteObject(new_pen);
2446 /* draw +/- signs with current text color */
2447 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2448 old_pen = SelectObject(hdc, new_pen);
2450 if (height < 18 || width < 18)
2452 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2453 LineTo(hdc, centerx + plussize, centery);
2455 if (!(item->state & TVIS_EXPANDED))
2457 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2458 LineTo(hdc, centerx, centery + plussize);
2461 else
2463 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2464 centerx + plussize, centery + 2);
2466 if (!(item->state & TVIS_EXPANDED))
2468 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2469 centerx + 2, centery + plussize);
2470 SetPixel(hdc, centerx - 1, centery, clrBk);
2471 SetPixel(hdc, centerx + 1, centery, clrBk);
2475 SelectObject(hdc, old_pen);
2476 DeleteObject(new_pen);
2480 SelectObject(hdc, hbrOld);
2481 DeleteObject(hbr);
2484 static void
2485 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2487 INT cditem;
2488 HFONT hOldFont;
2489 COLORREF oldTextColor, oldTextBkColor;
2490 int centery;
2491 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2492 NMTVCUSTOMDRAW nmcdhdr;
2494 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2496 /* - If item is drop target or it is selected and window is in focus -
2497 * use blue background (COLOR_HIGHLIGHT).
2498 * - If item is selected, window is not in focus, but it has style
2499 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2500 * - Otherwise - use background color
2502 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2503 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2504 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2506 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2508 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2509 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2511 else
2513 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2514 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2517 else
2519 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2520 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2521 nmcdhdr.clrText = comctl32_color.clrHighlight;
2522 else
2523 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2526 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2528 /* The custom draw handler can query the text rectangle,
2529 * so get ready. */
2530 /* should already be known, set to 0 when changed */
2531 if (!wineItem->textWidth)
2532 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2534 cditem = 0;
2536 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2538 cditem = TREEVIEW_SendCustomDrawItemNotify
2539 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2540 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2542 if (cditem & CDRF_SKIPDEFAULT)
2544 SelectObject(hdc, hOldFont);
2545 return;
2549 if (cditem & CDRF_NEWFONT)
2550 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2552 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2554 /* Set colors. Custom draw handler can change these so we do this after it. */
2555 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2556 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2558 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2561 * Display the images associated with this item
2564 INT imageIndex;
2566 /* State images are displayed to the left of the Normal image
2567 * image number is in state; zero should be `display no image'.
2569 imageIndex = STATEIMAGEINDEX(wineItem->state);
2571 if (infoPtr->himlState && imageIndex)
2573 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2574 wineItem->stateOffset,
2575 centery - infoPtr->stateImageHeight / 2,
2576 ILD_NORMAL);
2579 /* Now, draw the normal image; can be either selected,
2580 * non-selected or expanded image.
2583 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2585 /* The item is currently selected */
2586 imageIndex = wineItem->iSelectedImage;
2588 else if ((wineItem->state & TVIS_EXPANDED) && (wineItem->iExpandedImage != (WORD)I_IMAGENONE))
2590 /* The item is currently not selected but expanded */
2591 imageIndex = wineItem->iExpandedImage;
2593 else
2595 /* The item is not selected and not expanded */
2596 imageIndex = wineItem->iImage;
2599 if (infoPtr->himlNormal)
2601 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2603 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2604 wineItem->imageOffset,
2605 centery - infoPtr->normalImageHeight / 2,
2606 ILD_NORMAL | ovlIdx);
2612 * Display the text associated with this item
2615 /* Don't paint item's text if it's being edited */
2616 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2618 if (wineItem->pszText)
2620 RECT rcText;
2622 rcText.top = wineItem->rect.top;
2623 rcText.bottom = wineItem->rect.bottom;
2624 rcText.left = wineItem->textOffset;
2625 rcText.right = rcText.left + wineItem->textWidth + 4;
2627 TRACE("drawing text %s at (%s)\n",
2628 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2630 /* Draw it */
2631 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2632 ETO_CLIPPED | ETO_OPAQUE,
2633 &rcText,
2634 wineItem->pszText,
2635 lstrlenW(wineItem->pszText),
2636 NULL);
2638 /* Draw the box around the selected item */
2639 if ((wineItem == infoPtr->selectedItem) && inFocus)
2641 DrawFocusRect(hdc,&rcText);
2647 /* Draw insertion mark if necessary */
2649 if (infoPtr->insertMarkItem)
2650 TRACE("item:%d,mark:%p\n",
2651 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2652 infoPtr->insertMarkItem);
2654 if (wineItem == infoPtr->insertMarkItem)
2656 HPEN hNewPen, hOldPen;
2657 int offset;
2658 int left, right;
2660 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2661 hOldPen = SelectObject(hdc, hNewPen);
2663 if (infoPtr->insertBeforeorAfter)
2664 offset = wineItem->rect.bottom - 1;
2665 else
2666 offset = wineItem->rect.top + 1;
2668 left = wineItem->textOffset - 2;
2669 right = wineItem->textOffset + wineItem->textWidth + 2;
2671 MoveToEx(hdc, left, offset - 3, NULL);
2672 LineTo(hdc, left, offset + 4);
2674 MoveToEx(hdc, left, offset, NULL);
2675 LineTo(hdc, right + 1, offset);
2677 MoveToEx(hdc, right, offset + 3, NULL);
2678 LineTo(hdc, right, offset - 4);
2680 SelectObject(hdc, hOldPen);
2681 DeleteObject(hNewPen);
2684 if (cditem & CDRF_NOTIFYPOSTPAINT)
2686 cditem = TREEVIEW_SendCustomDrawItemNotify
2687 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2688 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2691 /* Restore the hdc state */
2692 SetTextColor(hdc, oldTextColor);
2693 SetBkColor(hdc, oldTextBkColor);
2694 SelectObject(hdc, hOldFont);
2697 /* Computes treeHeight and treeWidth and updates the scroll bars.
2699 static void
2700 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2702 TREEVIEW_ITEM *wineItem;
2703 HWND hwnd = infoPtr->hwnd;
2704 BOOL vert = FALSE;
2705 BOOL horz = FALSE;
2706 SCROLLINFO si;
2707 LONG scrollX = infoPtr->scrollX;
2709 infoPtr->treeWidth = 0;
2710 infoPtr->treeHeight = 0;
2712 /* We iterate through all visible items in order to get the tree height
2713 * and width */
2714 wineItem = infoPtr->root->firstChild;
2716 while (wineItem != NULL)
2718 if (ISVISIBLE(wineItem))
2720 /* actually we draw text at textOffset + 2 */
2721 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2722 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2724 /* This is scroll-adjusted, but we fix this below. */
2725 infoPtr->treeHeight = wineItem->rect.bottom;
2728 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2731 /* Fix the scroll adjusted treeHeight and treeWidth. */
2732 if (infoPtr->root->firstChild)
2733 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2735 infoPtr->treeWidth += infoPtr->scrollX;
2737 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2739 /* Adding one scroll bar may take up enough space that it forces us
2740 * to add the other as well. */
2741 if (infoPtr->treeHeight > infoPtr->clientHeight)
2743 vert = TRUE;
2745 if (infoPtr->treeWidth
2746 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2747 horz = TRUE;
2749 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2750 horz = TRUE;
2752 if (!vert && horz && infoPtr->treeHeight
2753 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2754 vert = TRUE;
2756 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2758 si.cbSize = sizeof(SCROLLINFO);
2759 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2760 si.nMin = 0;
2762 if (vert)
2764 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2765 if ( si.nPage && NULL != infoPtr->firstVisible)
2767 si.nPos = infoPtr->firstVisible->visibleOrder;
2768 si.nMax = infoPtr->maxVisibleOrder - 1;
2770 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2772 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2773 ShowScrollBar(hwnd, SB_VERT, TRUE);
2774 infoPtr->uInternalStatus |= TV_VSCROLL;
2776 else
2778 if (infoPtr->uInternalStatus & TV_VSCROLL)
2779 ShowScrollBar(hwnd, SB_VERT, FALSE);
2780 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2783 else
2785 if (infoPtr->uInternalStatus & TV_VSCROLL)
2786 ShowScrollBar(hwnd, SB_VERT, FALSE);
2787 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2790 if (horz)
2792 si.nPage = infoPtr->clientWidth;
2793 si.nPos = infoPtr->scrollX;
2794 si.nMax = infoPtr->treeWidth - 1;
2796 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2798 si.nPos = si.nMax - max( si.nPage-1, 0 );
2799 scrollX = si.nPos;
2802 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2803 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2804 infoPtr->uInternalStatus |= TV_HSCROLL;
2806 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2807 TREEVIEW_HScroll(infoPtr,
2808 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2810 else
2812 if (infoPtr->uInternalStatus & TV_HSCROLL)
2813 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2814 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2816 scrollX = 0;
2817 if (infoPtr->scrollX != 0)
2819 TREEVIEW_HScroll(infoPtr,
2820 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2824 if (!horz)
2825 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2828 static void
2829 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2831 HBRUSH hBrush;
2832 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2834 hBrush = CreateSolidBrush(clrBk);
2835 FillRect(hdc, rc, hBrush);
2836 DeleteObject(hBrush);
2839 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2840 static LRESULT
2841 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2843 RECT rect;
2845 TRACE("%p\n", infoPtr);
2847 GetClientRect(infoPtr->hwnd, &rect);
2848 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2850 return 1;
2853 static void
2854 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2856 HWND hwnd = infoPtr->hwnd;
2857 RECT rect = *rc;
2858 TREEVIEW_ITEM *wineItem;
2860 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2862 TRACE("empty window\n");
2863 return;
2866 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2867 hdc, rect);
2869 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2871 ReleaseDC(hwnd, hdc);
2872 return;
2875 for (wineItem = infoPtr->root->firstChild;
2876 wineItem != NULL;
2877 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2879 if (ISVISIBLE(wineItem))
2881 /* Avoid unneeded calculations */
2882 if (wineItem->rect.top > rect.bottom)
2883 break;
2884 if (wineItem->rect.bottom < rect.top)
2885 continue;
2887 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2891 TREEVIEW_UpdateScrollBars(infoPtr);
2893 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2894 infoPtr->cdmode =
2895 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2898 static inline void
2899 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2901 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2904 static void
2905 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2907 if (item)
2908 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2909 else
2910 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2913 static LRESULT
2914 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2916 HDC hdc;
2917 PAINTSTRUCT ps;
2918 RECT rc;
2920 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2922 if (hdc_ref)
2924 hdc = hdc_ref;
2925 GetClientRect(infoPtr->hwnd, &rc);
2926 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2928 else
2930 hdc = BeginPaint(infoPtr->hwnd, &ps);
2931 rc = ps.rcPaint;
2932 if(ps.fErase)
2933 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2936 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2937 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2939 if (!hdc_ref)
2940 EndPaint(infoPtr->hwnd, &ps);
2942 return 0;
2945 static LRESULT
2946 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2948 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2950 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2951 return 0;
2953 if (options & PRF_ERASEBKGND)
2954 TREEVIEW_EraseBackground(infoPtr, hdc);
2956 if (options & PRF_CLIENT)
2958 RECT rc;
2959 GetClientRect(infoPtr->hwnd, &rc);
2960 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2963 return 0;
2966 /* Sorting **************************************************************/
2968 /***************************************************************************
2969 * Forward the DPA local callback to the treeview owner callback
2971 static INT WINAPI
2972 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2973 const TVSORTCB *pCallBackSort)
2975 /* Forward the call to the client-defined callback */
2976 return pCallBackSort->lpfnCompare(first->lParam,
2977 second->lParam,
2978 pCallBackSort->lParam);
2981 /***************************************************************************
2982 * Treeview native sort routine: sort on item text.
2984 static INT WINAPI
2985 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2986 const TREEVIEW_INFO *infoPtr)
2988 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2989 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2991 if(first->pszText && second->pszText)
2992 return lstrcmpiW(first->pszText, second->pszText);
2993 else if(first->pszText)
2994 return -1;
2995 else if(second->pszText)
2996 return 1;
2997 else
2998 return 0;
3001 /* Returns the number of physical children belonging to item. */
3002 static INT
3003 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3005 INT cChildren = 0;
3006 HTREEITEM hti;
3008 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3009 cChildren++;
3011 return cChildren;
3014 /* Returns a DPA containing a pointer to each physical child of item in
3015 * sibling order. If item has no children, an empty DPA is returned. */
3016 static HDPA
3017 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3019 HTREEITEM child = item->firstChild;
3021 HDPA list = DPA_Create(8);
3022 if (list == 0) return NULL;
3024 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3026 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3028 DPA_Destroy(list);
3029 return NULL;
3033 return list;
3036 /***************************************************************************
3037 * Setup the treeview structure with regards of the sort method
3038 * and sort the children of the TV item specified in lParam
3039 * fRecurse: currently unused. Should be zero.
3040 * parent: if pSort!=NULL, should equal pSort->hParent.
3041 * otherwise, item which child items are to be sorted.
3042 * pSort: sort method info. if NULL, sort on item text.
3043 * if non-NULL, sort on item's lParam content, and let the
3044 * application decide what that means. See also TVM_SORTCHILDRENCB.
3047 static LRESULT
3048 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3049 LPTVSORTCB pSort)
3051 INT cChildren;
3052 PFNDPACOMPARE pfnCompare;
3053 LPARAM lpCompare;
3055 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3056 if (parent == TVI_ROOT || parent == NULL)
3057 parent = infoPtr->root;
3059 /* Check for a valid handle to the parent item */
3060 if (!TREEVIEW_ValidItem(infoPtr, parent))
3062 ERR("invalid item hParent=%p\n", parent);
3063 return FALSE;
3066 if (pSort)
3068 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3069 lpCompare = (LPARAM)pSort;
3071 else
3073 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3074 lpCompare = (LPARAM)infoPtr;
3077 cChildren = TREEVIEW_CountChildren(parent);
3079 /* Make sure there is something to sort */
3080 if (cChildren > 1)
3082 /* TREEVIEW_ITEM rechaining */
3083 INT count = 0;
3084 HTREEITEM item = 0;
3085 HTREEITEM nextItem = 0;
3086 HTREEITEM prevItem = 0;
3088 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3090 if (sortList == NULL)
3091 return FALSE;
3093 /* let DPA sort the list */
3094 DPA_Sort(sortList, pfnCompare, lpCompare);
3096 /* The order of DPA entries has been changed, so fixup the
3097 * nextSibling and prevSibling pointers. */
3099 item = DPA_GetPtr(sortList, count++);
3100 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3102 /* link the two current item together */
3103 item->nextSibling = nextItem;
3104 nextItem->prevSibling = item;
3106 if (prevItem == NULL)
3108 /* this is the first item, update the parent */
3109 parent->firstChild = item;
3110 item->prevSibling = NULL;
3112 else
3114 /* fix the back chaining */
3115 item->prevSibling = prevItem;
3118 /* get ready for the next one */
3119 prevItem = item;
3120 item = nextItem;
3123 /* the last item is pointed to by item and never has a sibling */
3124 item->nextSibling = NULL;
3125 parent->lastChild = item;
3127 DPA_Destroy(sortList);
3129 TREEVIEW_VerifyTree(infoPtr);
3131 if (parent->state & TVIS_EXPANDED)
3133 int visOrder = infoPtr->firstVisible->visibleOrder;
3135 if (parent == infoPtr->root)
3136 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3137 else
3138 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3140 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3142 TREEVIEW_ITEM *item;
3144 for (item = infoPtr->root->firstChild; item != NULL;
3145 item = TREEVIEW_GetNextListItem(infoPtr, item))
3147 if (item->visibleOrder == visOrder)
3148 break;
3151 if (!item) item = parent->firstChild;
3152 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3155 TREEVIEW_Invalidate(infoPtr, NULL);
3158 return TRUE;
3160 return FALSE;
3164 /***************************************************************************
3165 * Setup the treeview structure with regards of the sort method
3166 * and sort the children of the TV item specified in lParam
3168 static LRESULT
3169 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3171 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3175 /***************************************************************************
3176 * Sort the children of the TV item specified in lParam.
3178 static LRESULT
3179 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3181 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3185 /* Expansion/Collapse ***************************************************/
3187 static BOOL
3188 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3189 UINT action)
3191 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3192 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3193 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3194 0, wineItem);
3197 static VOID
3198 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3199 UINT action)
3201 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3202 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3203 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3204 0, wineItem);
3208 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3209 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3210 static BOOL
3211 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3212 BOOL bRemoveChildren, BOOL bUser)
3214 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3215 BOOL bSetSelection, bSetFirstVisible;
3216 RECT scrollRect;
3217 LONG scrollDist = 0;
3218 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3220 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3222 if (!(wineItem->state & TVIS_EXPANDED))
3223 return FALSE;
3225 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3226 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3228 if (wineItem->firstChild == NULL)
3229 return FALSE;
3231 wineItem->state &= ~TVIS_EXPANDED;
3233 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3234 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3236 bSetSelection = (infoPtr->selectedItem != NULL
3237 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3239 bSetFirstVisible = (infoPtr->firstVisible != NULL
3240 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3242 tmpItem = wineItem;
3243 while (tmpItem)
3245 if (tmpItem->nextSibling)
3247 nextItem = tmpItem->nextSibling;
3248 break;
3250 tmpItem = tmpItem->parent;
3253 if (nextItem)
3254 scrollDist = nextItem->rect.top;
3256 if (bRemoveChildren)
3258 INT old_cChildren = wineItem->cChildren;
3259 TRACE("TVE_COLLAPSERESET\n");
3260 wineItem->state &= ~TVIS_EXPANDEDONCE;
3261 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3262 wineItem->cChildren = old_cChildren;
3265 if (wineItem->firstChild)
3267 TREEVIEW_ITEM *item, *sibling;
3269 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3271 for (item = wineItem->firstChild; item != sibling;
3272 item = TREEVIEW_GetNextListItem(infoPtr, item))
3274 item->visibleOrder = -1;
3278 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3280 if (nextItem)
3281 scrollDist = -(scrollDist - nextItem->rect.top);
3283 if (bSetSelection)
3285 /* Don't call DoSelectItem, it sends notifications. */
3286 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3287 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3288 wineItem->state |= TVIS_SELECTED;
3289 infoPtr->selectedItem = wineItem;
3292 TREEVIEW_UpdateScrollBars(infoPtr);
3294 scrollRect.left = 0;
3295 scrollRect.right = infoPtr->clientWidth;
3296 scrollRect.bottom = infoPtr->clientHeight;
3298 if (nextItem)
3300 scrollRect.top = nextItem->rect.top;
3302 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3303 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3304 TREEVIEW_Invalidate(infoPtr, wineItem);
3305 } else {
3306 scrollRect.top = wineItem->rect.top;
3307 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3310 TREEVIEW_SetFirstVisible(infoPtr,
3311 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3312 TRUE);
3314 return TRUE;
3317 static BOOL
3318 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3319 BOOL partial, BOOL user)
3321 LONG scrollDist;
3322 LONG orgNextTop = 0;
3323 RECT scrollRect;
3324 TREEVIEW_ITEM *nextItem, *tmpItem;
3325 BOOL sendsNotifications;
3327 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, wineItem, partial, user);
3329 if (wineItem->state & TVIS_EXPANDED)
3330 return TRUE;
3332 tmpItem = wineItem; nextItem = NULL;
3333 while (tmpItem)
3335 if (tmpItem->nextSibling)
3337 nextItem = tmpItem->nextSibling;
3338 break;
3340 tmpItem = tmpItem->parent;
3343 if (nextItem)
3344 orgNextTop = nextItem->rect.top;
3346 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3348 sendsNotifications = user || ((wineItem->cChildren != 0) &&
3349 !(wineItem->state & TVIS_EXPANDEDONCE));
3350 if (sendsNotifications)
3352 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3354 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3355 return FALSE;
3358 if (!wineItem->firstChild)
3359 return FALSE;
3361 wineItem->state |= TVIS_EXPANDED;
3363 if (partial)
3364 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3366 if (ISVISIBLE(wineItem))
3368 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3369 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3370 TREEVIEW_UpdateScrollBars(infoPtr);
3372 scrollRect.left = 0;
3373 scrollRect.bottom = infoPtr->treeHeight;
3374 scrollRect.right = infoPtr->clientWidth;
3375 if (nextItem)
3377 scrollDist = nextItem->rect.top - orgNextTop;
3378 scrollRect.top = orgNextTop;
3380 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3381 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3382 TREEVIEW_Invalidate (infoPtr, wineItem);
3383 } else {
3384 scrollRect.top = wineItem->rect.top;
3385 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3388 /* Scroll up so that as many children as possible are visible.
3389 * This fails when expanding causes an HScroll bar to appear, but we
3390 * don't know that yet, so the last item is obscured. */
3391 if (wineItem->firstChild != NULL)
3393 int nChildren = wineItem->lastChild->visibleOrder
3394 - wineItem->firstChild->visibleOrder + 1;
3396 int visible_pos = wineItem->visibleOrder
3397 - infoPtr->firstVisible->visibleOrder;
3399 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3401 if (visible_pos > 0 && nChildren > rows_below)
3403 int scroll = nChildren - rows_below;
3405 if (scroll > visible_pos)
3406 scroll = visible_pos;
3408 if (scroll > 0)
3410 TREEVIEW_ITEM *newFirstVisible
3411 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3412 scroll);
3415 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3421 if (sendsNotifications) {
3422 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3423 wineItem->state |= TVIS_EXPANDEDONCE;
3426 return TRUE;
3429 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3430 to mouse messages and TVM_SELECTITEM.
3432 selection - previously selected item, used to collapse a part of a tree
3433 item - new selected item
3435 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3436 HTREEITEM selection, HTREEITEM item)
3438 TREEVIEW_ITEM *SelItem;
3440 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3442 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3445 * Close the previous selection all the way to the root
3446 * as long as the new selection is not a child
3448 if(selection && (selection != item))
3450 BOOL closeit = TRUE;
3451 SelItem = item;
3453 /* determine if the hitItem is a child of the currently selected item */
3454 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3455 (SelItem->parent != infoPtr->root))
3457 closeit = (SelItem != selection);
3458 SelItem = SelItem->parent;
3461 if(closeit)
3463 if(TREEVIEW_ValidItem(infoPtr, selection))
3464 SelItem = selection;
3466 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3467 SelItem->parent != infoPtr->root)
3469 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3470 SelItem = SelItem->parent;
3476 * Expand the current item
3478 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3481 static BOOL
3482 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3484 TRACE("\n");
3486 if (wineItem->state & TVIS_EXPANDED)
3487 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3488 else
3489 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3492 static VOID
3493 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3495 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3497 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3499 if (TREEVIEW_HasChildren(infoPtr, item))
3500 TREEVIEW_ExpandAll(infoPtr, item);
3504 /* Note:If the specified item is the child of a collapsed parent item,
3505 the parent's list of child items is (recursively) expanded to reveal the
3506 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3507 know if it also applies here.
3510 static LRESULT
3511 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3513 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3514 return 0;
3516 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3517 TREEVIEW_ItemName(wineItem), TREEVIEW_GetItemIndex(infoPtr, wineItem),
3518 flag, wineItem->state);
3520 switch (flag & TVE_TOGGLE)
3522 case TVE_COLLAPSE:
3523 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3524 FALSE);
3526 case TVE_EXPAND:
3527 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3528 FALSE);
3530 case TVE_TOGGLE:
3531 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3533 default:
3534 return 0;
3538 /* Hit-Testing **********************************************************/
3540 static TREEVIEW_ITEM *
3541 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3543 TREEVIEW_ITEM *wineItem;
3544 LONG row;
3546 if (!infoPtr->firstVisible)
3547 return NULL;
3549 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3551 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3552 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3554 if (row >= wineItem->visibleOrder
3555 && row < wineItem->visibleOrder + wineItem->iIntegral)
3556 break;
3559 return wineItem;
3562 static LRESULT
3563 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3565 TREEVIEW_ITEM *wineItem;
3566 RECT rect;
3567 UINT status;
3568 LONG x, y;
3570 lpht->hItem = 0;
3571 GetClientRect(infoPtr->hwnd, &rect);
3572 status = 0;
3573 x = lpht->pt.x;
3574 y = lpht->pt.y;
3576 if (x < rect.left)
3578 status |= TVHT_TOLEFT;
3580 else if (x > rect.right)
3582 status |= TVHT_TORIGHT;
3585 if (y < rect.top)
3587 status |= TVHT_ABOVE;
3589 else if (y > rect.bottom)
3591 status |= TVHT_BELOW;
3594 if (status)
3596 lpht->flags = status;
3597 return 0;
3600 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3601 if (!wineItem)
3603 lpht->flags = TVHT_NOWHERE;
3604 return 0;
3607 if (x >= wineItem->textOffset + wineItem->textWidth)
3609 lpht->flags = TVHT_ONITEMRIGHT;
3611 else if (x >= wineItem->textOffset)
3613 lpht->flags = TVHT_ONITEMLABEL;
3615 else if (x >= wineItem->imageOffset)
3617 lpht->flags = TVHT_ONITEMICON;
3619 else if (x >= wineItem->stateOffset)
3621 lpht->flags = TVHT_ONITEMSTATEICON;
3623 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3625 lpht->flags = TVHT_ONITEMBUTTON;
3627 else
3629 lpht->flags = TVHT_ONITEMINDENT;
3632 lpht->hItem = wineItem;
3633 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3635 return (LRESULT)wineItem;
3638 /* Item Label Editing ***************************************************/
3640 static LRESULT
3641 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3643 return (LRESULT)infoPtr->hwndEdit;
3646 static LRESULT CALLBACK
3647 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3649 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3650 BOOL bCancel = FALSE;
3651 LRESULT rc;
3653 switch (uMsg)
3655 case WM_PAINT:
3656 TRACE("WM_PAINT start\n");
3657 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3658 lParam);
3659 TRACE("WM_PAINT done\n");
3660 return rc;
3662 case WM_KILLFOCUS:
3663 if (infoPtr->bIgnoreEditKillFocus)
3664 return TRUE;
3665 break;
3667 case WM_DESTROY:
3669 WNDPROC editProc = infoPtr->wpEditOrig;
3670 infoPtr->wpEditOrig = 0;
3671 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3672 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3675 case WM_GETDLGCODE:
3676 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3678 case WM_KEYDOWN:
3679 if (wParam == VK_ESCAPE)
3681 bCancel = TRUE;
3682 break;
3684 else if (wParam == VK_RETURN)
3686 break;
3689 /* fall through */
3690 default:
3691 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3694 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3695 /* eg. Using a messagebox */
3697 infoPtr->bIgnoreEditKillFocus = TRUE;
3698 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3699 infoPtr->bIgnoreEditKillFocus = FALSE;
3701 return 0;
3705 /* should handle edit control messages here */
3707 static LRESULT
3708 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3710 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3712 switch (HIWORD(wParam))
3714 case EN_UPDATE:
3717 * Adjust the edit window size
3719 WCHAR buffer[1024];
3720 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3721 HDC hdc = GetDC(infoPtr->hwndEdit);
3722 SIZE sz;
3723 HFONT hFont, hOldFont = 0;
3725 TRACE("edit=%p\n", infoPtr->hwndEdit);
3727 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3729 infoPtr->bLabelChanged = TRUE;
3731 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3733 /* Select font to get the right dimension of the string */
3734 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3736 if (hFont != 0)
3738 hOldFont = SelectObject(hdc, hFont);
3741 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3743 TEXTMETRICW textMetric;
3745 /* Add Extra spacing for the next character */
3746 GetTextMetricsW(hdc, &textMetric);
3747 sz.cx += (textMetric.tmMaxCharWidth * 2);
3749 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3750 sz.cx = min(sz.cx,
3751 infoPtr->clientWidth - editItem->textOffset + 2);
3753 SetWindowPos(infoPtr->hwndEdit,
3754 HWND_TOP,
3757 sz.cx,
3758 editItem->rect.bottom - editItem->rect.top + 3,
3759 SWP_NOMOVE | SWP_DRAWFRAME);
3762 if (hFont != 0)
3764 SelectObject(hdc, hOldFont);
3767 ReleaseDC(infoPtr->hwnd, hdc);
3768 break;
3770 case EN_KILLFOCUS:
3771 /* apparently we should respect passed handle value */
3772 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3774 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3775 break;
3777 default:
3778 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3781 return 0;
3784 static HWND
3785 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3787 HWND hwnd = infoPtr->hwnd;
3788 HWND hwndEdit;
3789 SIZE sz;
3790 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3791 HDC hdc;
3792 HFONT hOldFont=0;
3793 TEXTMETRICW textMetric;
3795 TRACE("%p %p\n", hwnd, hItem);
3796 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3797 return NULL;
3799 if (infoPtr->hwndEdit)
3800 return infoPtr->hwndEdit;
3802 infoPtr->bLabelChanged = FALSE;
3804 /* make edit item visible */
3805 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3807 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3809 hdc = GetDC(hwnd);
3810 /* Select the font to get appropriate metric dimensions */
3811 if (infoPtr->hFont != 0)
3813 hOldFont = SelectObject(hdc, infoPtr->hFont);
3816 /* Get string length in pixels */
3817 if (hItem->pszText)
3818 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3819 &sz);
3820 else
3821 GetTextExtentPoint32A(hdc, "", 0, &sz);
3823 /* Add Extra spacing for the next character */
3824 GetTextMetricsW(hdc, &textMetric);
3825 sz.cx += (textMetric.tmMaxCharWidth * 2);
3827 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3828 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3830 if (infoPtr->hFont != 0)
3832 SelectObject(hdc, hOldFont);
3835 ReleaseDC(hwnd, hdc);
3837 infoPtr->editItem = hItem;
3839 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3840 WC_EDITW,
3842 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3843 WS_CLIPSIBLINGS | ES_WANTRETURN |
3844 ES_LEFT, hItem->textOffset - 2,
3845 hItem->rect.top - 1, sz.cx + 3,
3846 hItem->rect.bottom -
3847 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3848 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3850 infoPtr->hwndEdit = hwndEdit;
3852 /* Get a 2D border. */
3853 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3854 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3855 SetWindowLongW(hwndEdit, GWL_STYLE,
3856 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3858 SendMessageW(hwndEdit, WM_SETFONT,
3859 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3861 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3862 (DWORD_PTR)
3863 TREEVIEW_Edit_SubclassProc);
3865 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3867 DestroyWindow(hwndEdit);
3868 infoPtr->hwndEdit = 0;
3869 infoPtr->editItem = NULL;
3870 return NULL;
3873 if (hItem->pszText)
3874 SetWindowTextW(hwndEdit, hItem->pszText);
3876 SetFocus(hwndEdit);
3877 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3878 ShowWindow(hwndEdit, SW_SHOW);
3880 return hwndEdit;
3884 static LRESULT
3885 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3887 HWND hwnd = infoPtr->hwnd;
3888 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3889 NMTVDISPINFOW tvdi;
3890 BOOL bCommit;
3891 WCHAR tmpText[1024] = { '\0' };
3892 WCHAR *newText = tmpText;
3893 int iLength = 0;
3895 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3897 tvdi.hdr.hwndFrom = hwnd;
3898 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3899 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3900 tvdi.item.mask = 0;
3901 tvdi.item.hItem = editedItem;
3902 tvdi.item.state = editedItem->state;
3903 tvdi.item.lParam = editedItem->lParam;
3905 if (!bCancel)
3907 if (!infoPtr->bNtfUnicode)
3908 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3909 else
3910 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3912 if (iLength >= 1023)
3914 ERR("Insufficient space to retrieve new item label\n");
3917 tvdi.item.mask = TVIF_TEXT;
3918 tvdi.item.pszText = tmpText;
3919 tvdi.item.cchTextMax = iLength + 1;
3921 else
3923 tvdi.item.pszText = NULL;
3924 tvdi.item.cchTextMax = 0;
3927 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
3929 if (!bCancel && bCommit) /* Apply the changes */
3931 if (!infoPtr->bNtfUnicode)
3933 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3934 newText = Alloc(len * sizeof(WCHAR));
3935 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3936 iLength = len - 1;
3939 if (strcmpW(newText, editedItem->pszText) != 0)
3941 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3942 if (ptr == NULL)
3944 ERR("OutOfMemory, cannot allocate space for label\n");
3945 if(newText != tmpText) Free(newText);
3946 DestroyWindow(infoPtr->hwndEdit);
3947 infoPtr->hwndEdit = 0;
3948 infoPtr->editItem = NULL;
3949 return FALSE;
3951 else
3953 editedItem->pszText = ptr;
3954 editedItem->cchTextMax = iLength + 1;
3955 strcpyW(editedItem->pszText, newText);
3956 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3959 if(newText != tmpText) Free(newText);
3962 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3963 DestroyWindow(infoPtr->hwndEdit);
3964 infoPtr->hwndEdit = 0;
3965 infoPtr->editItem = NULL;
3966 return TRUE;
3969 static LRESULT
3970 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3972 if (wParam != TV_EDIT_TIMER)
3974 ERR("got unknown timer\n");
3975 return 1;
3978 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3979 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3981 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3983 return 0;
3987 /* Mouse Tracking/Drag **************************************************/
3989 /***************************************************************************
3990 * This is quite unusual piece of code, but that's how it's implemented in
3991 * Windows.
3993 static LRESULT
3994 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3996 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3997 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3998 RECT r;
3999 MSG msg;
4001 r.top = pt.y - cyDrag;
4002 r.left = pt.x - cxDrag;
4003 r.bottom = pt.y + cyDrag;
4004 r.right = pt.x + cxDrag;
4006 SetCapture(infoPtr->hwnd);
4008 while (1)
4010 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4012 if (msg.message == WM_MOUSEMOVE)
4014 pt.x = (short)LOWORD(msg.lParam);
4015 pt.y = (short)HIWORD(msg.lParam);
4016 if (PtInRect(&r, pt))
4017 continue;
4018 else
4020 ReleaseCapture();
4021 return 1;
4024 else if (msg.message >= WM_LBUTTONDOWN &&
4025 msg.message <= WM_RBUTTONDBLCLK)
4027 if (msg.message == WM_RBUTTONUP)
4028 TREEVIEW_RButtonUp(infoPtr, &pt);
4029 break;
4032 DispatchMessageW(&msg);
4035 if (GetCapture() != infoPtr->hwnd)
4036 return 0;
4039 ReleaseCapture();
4040 return 0;
4044 static LRESULT
4045 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4047 TREEVIEW_ITEM *wineItem;
4048 TVHITTESTINFO hit;
4050 TRACE("\n");
4051 SetFocus(infoPtr->hwnd);
4053 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4055 /* If there is pending 'edit label' event - kill it now */
4056 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4059 hit.pt.x = (short)LOWORD(lParam);
4060 hit.pt.y = (short)HIWORD(lParam);
4062 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4063 if (!wineItem)
4064 return 0;
4065 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
4067 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4068 { /* FIXME! */
4069 switch (hit.flags)
4071 case TVHT_ONITEMRIGHT:
4072 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4073 break;
4075 case TVHT_ONITEMINDENT:
4076 if (!(infoPtr->dwStyle & TVS_HASLINES))
4078 break;
4080 else
4082 int level = hit.pt.x / infoPtr->uIndent;
4083 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4085 while (wineItem->iLevel > level)
4087 wineItem = wineItem->parent;
4090 /* fall through */
4093 case TVHT_ONITEMLABEL:
4094 case TVHT_ONITEMICON:
4095 case TVHT_ONITEMBUTTON:
4096 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4097 break;
4099 case TVHT_ONITEMSTATEICON:
4100 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4101 TREEVIEW_ToggleItemState(infoPtr, wineItem);
4102 else
4103 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4104 break;
4107 return TRUE;
4111 static LRESULT
4112 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4114 HWND hwnd = infoPtr->hwnd;
4115 TVHITTESTINFO ht;
4116 BOOL bTrack, bDoLabelEdit;
4118 /* If Edit control is active - kill it and return.
4119 * The best way to do it is to set focus to itself.
4120 * Edit control subclassed procedure will automatically call
4121 * EndEditLabelNow.
4123 if (infoPtr->hwndEdit)
4125 SetFocus(hwnd);
4126 return 0;
4129 ht.pt.x = (short)LOWORD(lParam);
4130 ht.pt.y = (short)HIWORD(lParam);
4132 TREEVIEW_HitTest(infoPtr, &ht);
4133 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4135 /* update focusedItem and redraw both items */
4136 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4138 infoPtr->focusedItem = ht.hItem;
4139 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4140 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4143 bTrack = (ht.flags & TVHT_ONITEM)
4144 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4147 * If the style allows editing and the node is already selected
4148 * and the click occurred on the item label...
4150 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4151 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4153 /* Send NM_CLICK right away */
4154 if (!bTrack)
4155 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4156 goto setfocus;
4158 if (ht.flags & TVHT_ONITEMBUTTON)
4160 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4161 goto setfocus;
4163 else if (bTrack)
4164 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4165 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4167 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4168 infoPtr->dropItem = ht.hItem;
4170 /* clean up focusedItem as we dragged and won't select this item */
4171 if(infoPtr->focusedItem)
4173 /* refresh the item that was focused */
4174 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4175 infoPtr->focusedItem = NULL;
4177 /* refresh the selected item to return the filled background */
4178 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4181 return 0;
4185 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4186 goto setfocus;
4188 if (bDoLabelEdit)
4190 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4191 KillTimer(hwnd, TV_EDIT_TIMER);
4193 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4194 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4196 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4198 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4200 /* Select the current item */
4201 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4202 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4204 else if (ht.flags & TVHT_ONITEMSTATEICON)
4206 /* TVS_CHECKBOXES requires us to toggle the current state */
4207 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4208 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4211 setfocus:
4212 SetFocus(hwnd);
4213 return 0;
4217 static LRESULT
4218 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4220 TVHITTESTINFO ht;
4222 if (infoPtr->hwndEdit)
4224 SetFocus(infoPtr->hwnd);
4225 return 0;
4228 ht.pt.x = (short)LOWORD(lParam);
4229 ht.pt.y = (short)HIWORD(lParam);
4231 TREEVIEW_HitTest(infoPtr, &ht);
4233 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4235 if (ht.hItem)
4237 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4238 infoPtr->dropItem = ht.hItem;
4241 else
4243 SetFocus(infoPtr->hwnd);
4244 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4247 return 0;
4250 static LRESULT
4251 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4253 TVHITTESTINFO ht;
4255 ht.pt = *pPt;
4257 TREEVIEW_HitTest(infoPtr, &ht);
4259 if (ht.hItem)
4261 /* Change to screen coordinate for WM_CONTEXTMENU */
4262 ClientToScreen(infoPtr->hwnd, &ht.pt);
4264 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4265 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4266 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4268 return 0;
4272 static LRESULT
4273 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4275 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4276 INT cx, cy;
4277 HDC hdc, htopdc;
4278 HWND hwtop;
4279 HBITMAP hbmp, hOldbmp;
4280 SIZE size;
4281 RECT rc;
4282 HFONT hOldFont;
4284 TRACE("\n");
4286 if (!(infoPtr->himlNormal))
4287 return 0;
4289 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4290 return 0;
4292 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4294 hwtop = GetDesktopWindow();
4295 htopdc = GetDC(hwtop);
4296 hdc = CreateCompatibleDC(htopdc);
4298 hOldFont = SelectObject(hdc, infoPtr->hFont);
4300 if (dragItem->pszText)
4301 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4302 &size);
4303 else
4304 GetTextExtentPoint32A(hdc, "", 0, &size);
4306 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4307 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4308 hOldbmp = SelectObject(hdc, hbmp);
4310 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4311 size.cx += cx;
4312 if (cy > size.cy)
4313 size.cy = cy;
4315 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4316 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4317 ILD_NORMAL);
4320 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4321 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4324 /* draw item text */
4326 SetRect(&rc, cx, 0, size.cx, size.cy);
4328 if (dragItem->pszText)
4329 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4330 DT_LEFT);
4332 SelectObject(hdc, hOldFont);
4333 SelectObject(hdc, hOldbmp);
4335 ImageList_Add(infoPtr->dragList, hbmp, 0);
4337 DeleteDC(hdc);
4338 DeleteObject(hbmp);
4339 ReleaseDC(hwtop, htopdc);
4341 return (LRESULT)infoPtr->dragList;
4344 /* Selection ************************************************************/
4346 static LRESULT
4347 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4348 INT cause)
4350 TREEVIEW_ITEM *prevSelect;
4352 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4354 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4355 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4356 newSelect ? newSelect->state : 0);
4358 /* reset and redraw focusedItem if focusedItem was set so we don't */
4359 /* have to worry about the previously focused item when we set a new one */
4360 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4361 infoPtr->focusedItem = NULL;
4363 switch (action)
4365 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4366 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4367 /* Fall through */
4368 case TVGN_CARET:
4369 prevSelect = infoPtr->selectedItem;
4371 if (prevSelect == newSelect) {
4372 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4373 break;
4376 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4377 TVN_SELCHANGINGW,
4378 cause,
4379 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4380 prevSelect,
4381 newSelect))
4382 return FALSE;
4384 if (prevSelect)
4385 prevSelect->state &= ~TVIS_SELECTED;
4386 if (newSelect)
4387 newSelect->state |= TVIS_SELECTED;
4389 infoPtr->selectedItem = newSelect;
4391 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4393 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4394 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4396 TREEVIEW_SendTreeviewNotify(infoPtr,
4397 TVN_SELCHANGEDW,
4398 cause,
4399 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4400 prevSelect,
4401 newSelect);
4402 break;
4404 case TVGN_DROPHILITE:
4405 prevSelect = infoPtr->dropItem;
4407 if (prevSelect)
4408 prevSelect->state &= ~TVIS_DROPHILITED;
4410 infoPtr->dropItem = newSelect;
4412 if (newSelect)
4413 newSelect->state |= TVIS_DROPHILITED;
4415 TREEVIEW_Invalidate(infoPtr, prevSelect);
4416 TREEVIEW_Invalidate(infoPtr, newSelect);
4417 break;
4419 case TVGN_FIRSTVISIBLE:
4420 if (newSelect != NULL)
4422 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4423 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4424 TREEVIEW_Invalidate(infoPtr, NULL);
4426 break;
4429 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4430 return TRUE;
4433 /* FIXME: handle NM_KILLFOCUS etc */
4434 static LRESULT
4435 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4437 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4439 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4440 return FALSE;
4442 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4444 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4445 return FALSE;
4447 TREEVIEW_SingleExpand(infoPtr, selection, item);
4449 return TRUE;
4452 /*************************************************************************
4453 * TREEVIEW_ProcessLetterKeys
4455 * Processes keyboard messages generated by pressing the letter keys
4456 * on the keyboard.
4457 * What this does is perform a case insensitive search from the
4458 * current position with the following quirks:
4459 * - If two chars or more are pressed in quick succession we search
4460 * for the corresponding string (e.g. 'abc').
4461 * - If there is a delay we wipe away the current search string and
4462 * restart with just that char.
4463 * - If the user keeps pressing the same character, whether slowly or
4464 * fast, so that the search string is entirely composed of this
4465 * character ('aaaaa' for instance), then we search for first item
4466 * that starting with that character.
4467 * - If the user types the above character in quick succession, then
4468 * we must also search for the corresponding string ('aaaaa'), and
4469 * go to that string if there is a match.
4471 * RETURNS
4473 * Zero.
4475 * BUGS
4477 * - The current implementation has a list of characters it will
4478 * accept and it ignores everything else. In particular it will
4479 * ignore accentuated characters which seems to match what
4480 * Windows does. But I'm not sure it makes sense to follow
4481 * Windows there.
4482 * - We don't sound a beep when the search fails.
4483 * - The search should start from the focused item, not from the selected
4484 * item. One reason for this is to allow for multiple selections in trees.
4485 * But currently infoPtr->focusedItem does not seem very usable.
4487 * SEE ALSO
4489 * TREEVIEW_ProcessLetterKeys
4491 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4493 HTREEITEM nItem;
4494 HTREEITEM endidx,idx;
4495 TVITEMEXW item;
4496 WCHAR buffer[MAX_PATH];
4497 DWORD timestamp,elapsed;
4499 /* simple parameter checking */
4500 if (!charCode || !keyData) return 0;
4502 /* only allow the valid WM_CHARs through */
4503 if (!isalnum(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 charCode != '<' && charCode != ',' && charCode != '~')
4513 return 0;
4515 /* compute how much time elapsed since last keypress */
4516 timestamp = GetTickCount();
4517 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4518 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4519 } else {
4520 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4523 /* update the search parameters */
4524 infoPtr->lastKeyPressTimestamp=timestamp;
4525 if (elapsed < KEY_DELAY) {
4526 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4527 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4529 if (infoPtr->charCode != charCode) {
4530 infoPtr->charCode=charCode=0;
4532 } else {
4533 infoPtr->charCode=charCode;
4534 infoPtr->szSearchParam[0]=charCode;
4535 infoPtr->nSearchParamLength=1;
4536 /* Redundant with the 1 char string */
4537 charCode=0;
4540 /* and search from the current position */
4541 nItem=NULL;
4542 if (infoPtr->selectedItem != NULL) {
4543 endidx=infoPtr->selectedItem;
4544 /* if looking for single character match,
4545 * then we must always move forward
4547 if (infoPtr->nSearchParamLength == 1)
4548 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4549 else
4550 idx=endidx;
4551 } else {
4552 endidx=NULL;
4553 idx=infoPtr->root->firstChild;
4555 do {
4556 /* At the end point, sort out wrapping */
4557 if (idx == NULL) {
4559 /* If endidx is null, stop at the last item (ie top to bottom) */
4560 if (endidx == NULL)
4561 break;
4563 /* Otherwise, start again at the very beginning */
4564 idx=infoPtr->root->firstChild;
4566 /* But if we are stopping on the first child, end now! */
4567 if (idx == endidx) break;
4570 /* get item */
4571 ZeroMemory(&item, sizeof(item));
4572 item.mask = TVIF_TEXT;
4573 item.hItem = idx;
4574 item.pszText = buffer;
4575 item.cchTextMax = sizeof(buffer);
4576 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4578 /* check for a match */
4579 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4580 nItem=idx;
4581 break;
4582 } else if ( (charCode != 0) && (nItem == NULL) &&
4583 (nItem != infoPtr->selectedItem) &&
4584 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4585 /* This would work but we must keep looking for a longer match */
4586 nItem=idx;
4588 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4589 } while (idx != endidx);
4591 if (nItem != NULL) {
4592 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4593 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4597 return 0;
4600 /* Scrolling ************************************************************/
4602 static LRESULT
4603 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4605 int viscount;
4606 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4607 HTREEITEM newFirstVisible = NULL;
4608 int visible_pos = -1;
4610 if (!TREEVIEW_ValidItem(infoPtr, item))
4611 return FALSE;
4613 if (!ISVISIBLE(item))
4615 /* Expand parents as necessary. */
4616 HTREEITEM parent;
4618 /* see if we are trying to ensure that root is visible */
4619 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4620 parent = item->parent;
4621 else
4622 parent = item; /* this item is the topmost item */
4624 while (parent != infoPtr->root)
4626 if (!(parent->state & TVIS_EXPANDED))
4627 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4629 parent = parent->parent;
4633 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4635 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4636 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4638 if (hasFirstVisible)
4639 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4641 if (visible_pos < 0)
4643 /* item is before the start of the list: put it at the top. */
4644 newFirstVisible = item;
4646 else if (visible_pos >= viscount
4647 /* Sometimes, before we are displayed, GVC is 0, causing us to
4648 * spuriously scroll up. */
4649 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4651 /* item is past the end of the list. */
4652 int scroll = visible_pos - viscount;
4654 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4655 scroll + 1);
4658 if (bHScroll)
4660 /* Scroll window so item's text is visible as much as possible */
4661 /* Calculation of amount of extra space is taken from EditLabel code */
4662 INT pos, x;
4663 TEXTMETRICW textMetric;
4664 HDC hdc = GetWindowDC(infoPtr->hwnd);
4666 x = item->textWidth;
4668 GetTextMetricsW(hdc, &textMetric);
4669 ReleaseDC(infoPtr->hwnd, hdc);
4671 x += (textMetric.tmMaxCharWidth * 2);
4672 x = max(x, textMetric.tmMaxCharWidth * 3);
4674 if (item->textOffset < 0)
4675 pos = item->textOffset;
4676 else if (item->textOffset + x > infoPtr->clientWidth)
4678 if (x > infoPtr->clientWidth)
4679 pos = item->textOffset;
4680 else
4681 pos = item->textOffset + x - infoPtr->clientWidth;
4683 else
4684 pos = 0;
4686 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4689 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4691 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4693 return TRUE;
4696 return FALSE;
4699 static VOID
4700 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4701 TREEVIEW_ITEM *newFirstVisible,
4702 BOOL bUpdateScrollPos)
4704 int gap_size;
4706 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4708 if (newFirstVisible != NULL)
4710 /* Prevent an empty gap from appearing at the bottom... */
4711 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4712 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4714 if (gap_size > 0)
4716 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4717 -gap_size);
4719 /* ... unless we just don't have enough items. */
4720 if (newFirstVisible == NULL)
4721 newFirstVisible = infoPtr->root->firstChild;
4725 if (infoPtr->firstVisible != newFirstVisible)
4727 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4729 infoPtr->firstVisible = newFirstVisible;
4730 TREEVIEW_Invalidate(infoPtr, NULL);
4732 else
4734 TREEVIEW_ITEM *item;
4735 int scroll = infoPtr->uItemHeight *
4736 (infoPtr->firstVisible->visibleOrder
4737 - newFirstVisible->visibleOrder);
4739 infoPtr->firstVisible = newFirstVisible;
4741 for (item = infoPtr->root->firstChild; item != NULL;
4742 item = TREEVIEW_GetNextListItem(infoPtr, item))
4744 item->rect.top += scroll;
4745 item->rect.bottom += scroll;
4748 if (bUpdateScrollPos)
4749 SetScrollPos(infoPtr->hwnd, SB_VERT,
4750 newFirstVisible->visibleOrder, TRUE);
4752 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4757 /************************************************************************
4758 * VScroll is always in units of visible items. i.e. we always have a
4759 * visible item aligned to the top of the control. (Unless we have no
4760 * items at all.)
4762 static LRESULT
4763 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4765 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4766 TREEVIEW_ITEM *newFirstVisible = NULL;
4768 int nScrollCode = LOWORD(wParam);
4770 TRACE("wp %lx\n", wParam);
4772 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4773 return 0;
4775 if (!oldFirstVisible)
4777 assert(infoPtr->root->firstChild == NULL);
4778 return 0;
4781 switch (nScrollCode)
4783 case SB_TOP:
4784 newFirstVisible = infoPtr->root->firstChild;
4785 break;
4787 case SB_BOTTOM:
4788 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4789 break;
4791 case SB_LINEUP:
4792 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4793 break;
4795 case SB_LINEDOWN:
4796 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4797 break;
4799 case SB_PAGEUP:
4800 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4801 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4802 break;
4804 case SB_PAGEDOWN:
4805 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4806 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4807 break;
4809 case SB_THUMBTRACK:
4810 case SB_THUMBPOSITION:
4811 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4812 infoPtr->root->firstChild,
4813 (LONG)(SHORT)HIWORD(wParam));
4814 break;
4816 case SB_ENDSCROLL:
4817 return 0;
4820 if (newFirstVisible != NULL)
4822 if (newFirstVisible != oldFirstVisible)
4823 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4824 nScrollCode != SB_THUMBTRACK);
4825 else if (nScrollCode == SB_THUMBPOSITION)
4826 SetScrollPos(infoPtr->hwnd, SB_VERT,
4827 newFirstVisible->visibleOrder, TRUE);
4830 return 0;
4833 static LRESULT
4834 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4836 int maxWidth;
4837 int scrollX = infoPtr->scrollX;
4838 int nScrollCode = LOWORD(wParam);
4840 TRACE("wp %lx\n", wParam);
4842 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4843 return FALSE;
4845 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4846 /* shall never occur */
4847 if (maxWidth <= 0)
4849 scrollX = 0;
4850 goto scroll;
4853 switch (nScrollCode)
4855 case SB_LINELEFT:
4856 scrollX -= infoPtr->uItemHeight;
4857 break;
4858 case SB_LINERIGHT:
4859 scrollX += infoPtr->uItemHeight;
4860 break;
4861 case SB_PAGELEFT:
4862 scrollX -= infoPtr->clientWidth;
4863 break;
4864 case SB_PAGERIGHT:
4865 scrollX += infoPtr->clientWidth;
4866 break;
4868 case SB_THUMBTRACK:
4869 case SB_THUMBPOSITION:
4870 scrollX = (int)(SHORT)HIWORD(wParam);
4871 break;
4873 case SB_ENDSCROLL:
4874 return 0;
4877 if (scrollX > maxWidth)
4878 scrollX = maxWidth;
4879 else if (scrollX < 0)
4880 scrollX = 0;
4882 scroll:
4883 if (scrollX != infoPtr->scrollX)
4885 TREEVIEW_ITEM *item;
4886 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4888 for (item = infoPtr->root->firstChild; item != NULL;
4889 item = TREEVIEW_GetNextListItem(infoPtr, item))
4891 item->linesOffset += scroll_pixels;
4892 item->stateOffset += scroll_pixels;
4893 item->imageOffset += scroll_pixels;
4894 item->textOffset += scroll_pixels;
4897 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4898 infoPtr->scrollX = scrollX;
4899 UpdateWindow(infoPtr->hwnd);
4902 if (nScrollCode != SB_THUMBTRACK)
4903 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4905 return 0;
4908 static LRESULT
4909 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4911 short gcWheelDelta;
4912 UINT pulScrollLines = 3;
4914 if (wParam & (MK_SHIFT | MK_CONTROL))
4915 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4917 if (infoPtr->firstVisible == NULL)
4918 return TRUE;
4920 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4922 gcWheelDelta = -(short)HIWORD(wParam);
4923 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4925 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4927 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4928 int maxDy = infoPtr->maxVisibleOrder;
4930 if (newDy > maxDy)
4931 newDy = maxDy;
4933 if (newDy < 0)
4934 newDy = 0;
4936 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4938 return TRUE;
4941 /* Create/Destroy *******************************************************/
4943 static void
4944 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4946 RECT rc;
4947 HBITMAP hbm, hbmOld;
4948 HDC hdc, hdcScreen;
4949 int nIndex;
4951 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4953 hdcScreen = GetDC(0);
4955 hdc = CreateCompatibleDC(hdcScreen);
4956 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4957 hbmOld = SelectObject(hdc, hbm);
4959 SetRect(&rc, 0, 0, 48, 16);
4960 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4962 SetRect(&rc, 18, 2, 30, 14);
4963 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4964 DFCS_BUTTONCHECK|DFCS_FLAT);
4966 SetRect(&rc, 34, 2, 46, 14);
4967 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4968 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4970 SelectObject(hdc, hbmOld);
4971 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4972 comctl32_color.clrWindow);
4973 TRACE("checkbox index %d\n", nIndex);
4975 DeleteObject(hbm);
4976 DeleteDC(hdc);
4977 ReleaseDC(0, hdcScreen);
4979 infoPtr->stateImageWidth = 16;
4980 infoPtr->stateImageHeight = 16;
4983 static LRESULT
4984 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4986 RECT rcClient;
4987 TREEVIEW_INFO *infoPtr;
4988 LOGFONTW lf;
4990 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4992 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4994 if (infoPtr == NULL)
4996 ERR("could not allocate info memory!\n");
4997 return 0;
5000 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5002 infoPtr->hwnd = hwnd;
5003 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5004 infoPtr->Timer = 0;
5005 infoPtr->uNumItems = 0;
5006 infoPtr->cdmode = 0;
5007 infoPtr->uScrollTime = 300; /* milliseconds */
5008 infoPtr->bRedraw = TRUE;
5010 GetClientRect(hwnd, &rcClient);
5012 /* No scroll bars yet. */
5013 infoPtr->clientWidth = rcClient.right;
5014 infoPtr->clientHeight = rcClient.bottom;
5015 infoPtr->uInternalStatus = 0;
5017 infoPtr->treeWidth = 0;
5018 infoPtr->treeHeight = 0;
5020 infoPtr->uIndent = MINIMUM_INDENT;
5021 infoPtr->selectedItem = NULL;
5022 infoPtr->focusedItem = NULL;
5023 infoPtr->hotItem = NULL;
5024 infoPtr->editItem = NULL;
5025 infoPtr->firstVisible = NULL;
5026 infoPtr->maxVisibleOrder = 0;
5027 infoPtr->dropItem = NULL;
5028 infoPtr->insertMarkItem = NULL;
5029 infoPtr->insertBeforeorAfter = 0;
5030 /* dragList */
5032 infoPtr->scrollX = 0;
5034 infoPtr->clrBk = CLR_NONE; /* use system color */
5035 infoPtr->clrText = CLR_NONE; /* use system color */
5036 infoPtr->clrLine = CLR_DEFAULT;
5037 infoPtr->clrInsertMark = CLR_DEFAULT;
5039 /* hwndToolTip */
5041 infoPtr->hwndEdit = NULL;
5042 infoPtr->wpEditOrig = NULL;
5043 infoPtr->bIgnoreEditKillFocus = FALSE;
5044 infoPtr->bLabelChanged = FALSE;
5046 infoPtr->himlNormal = NULL;
5047 infoPtr->himlState = NULL;
5048 infoPtr->normalImageWidth = 0;
5049 infoPtr->normalImageHeight = 0;
5050 infoPtr->stateImageWidth = 0;
5051 infoPtr->stateImageHeight = 0;
5053 infoPtr->items = DPA_Create(16);
5055 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5056 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5057 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5058 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5059 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5061 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5063 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5064 infoPtr->root->state = TVIS_EXPANDED;
5065 infoPtr->root->iLevel = -1;
5066 infoPtr->root->visibleOrder = -1;
5068 infoPtr->hwndNotify = lpcs->hwndParent;
5069 infoPtr->hwndToolTip = 0;
5071 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5073 /* Determine what type of notify should be issued */
5074 /* sets infoPtr->bNtfUnicode */
5075 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5077 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5078 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5079 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5080 hwnd, 0, 0, 0);
5082 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5083 initialize_checkboxes(infoPtr);
5085 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5086 ShowScrollBar(hwnd, SB_VERT, FALSE);
5087 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5089 OpenThemeData (hwnd, themeClass);
5091 return 0;
5095 static LRESULT
5096 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5098 TRACE("\n");
5100 /* free item data */
5101 TREEVIEW_RemoveTree(infoPtr);
5102 /* root isn't freed with other items */
5103 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5104 DPA_Destroy(infoPtr->items);
5106 /* tool tip is automatically destroyed: we are its owner */
5108 /* Restore original wndproc */
5109 if (infoPtr->hwndEdit)
5110 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5111 (DWORD_PTR)infoPtr->wpEditOrig);
5113 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5115 /* Deassociate treeview from the window before doing anything drastic. */
5116 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5119 DeleteObject(infoPtr->hDefaultFont);
5120 DeleteObject(infoPtr->hBoldFont);
5121 DeleteObject(infoPtr->hUnderlineFont);
5122 Free(infoPtr);
5124 return 0;
5127 /* Miscellaneous Messages ***********************************************/
5129 static LRESULT
5130 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5132 static const struct
5134 unsigned char code;
5136 scroll[] =
5138 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5139 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5140 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5141 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5142 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5143 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5144 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5145 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5146 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5147 #undef SCROLL_ENTRY
5150 if (key >= VK_PRIOR && key <= VK_DOWN)
5152 unsigned char code = scroll[key - VK_PRIOR].code;
5154 (((code & (1 << 7)) == (SB_HORZ << 7))
5155 ? TREEVIEW_HScroll
5156 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5159 return 0;
5162 /************************************************************************
5163 * TREEVIEW_KeyDown
5165 * VK_UP Move selection to the previous non-hidden item.
5166 * VK_DOWN Move selection to the next non-hidden item.
5167 * VK_HOME Move selection to the first item.
5168 * VK_END Move selection to the last item.
5169 * VK_LEFT If expanded then collapse, otherwise move to parent.
5170 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5171 * VK_ADD Expand.
5172 * VK_SUBTRACT Collapse.
5173 * VK_MULTIPLY Expand all.
5174 * VK_PRIOR Move up GetVisibleCount items.
5175 * VK_NEXT Move down GetVisibleCount items.
5176 * VK_BACK Move to parent.
5177 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5179 static LRESULT
5180 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5182 /* If it is non-NULL and different, it will be selected and visible. */
5183 TREEVIEW_ITEM *newSelection = NULL;
5185 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5187 TRACE("%lx\n", wParam);
5189 if (prevItem == NULL)
5190 return FALSE;
5192 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5193 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5195 switch (wParam)
5197 case VK_UP:
5198 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5199 if (!newSelection)
5200 newSelection = infoPtr->root->firstChild;
5201 break;
5203 case VK_DOWN:
5204 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5205 break;
5207 case VK_HOME:
5208 newSelection = infoPtr->root->firstChild;
5209 break;
5211 case VK_END:
5212 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5213 break;
5215 case VK_LEFT:
5216 if (prevItem->state & TVIS_EXPANDED)
5218 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5220 else if (prevItem->parent != infoPtr->root)
5222 newSelection = prevItem->parent;
5224 break;
5226 case VK_RIGHT:
5227 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5229 if (!(prevItem->state & TVIS_EXPANDED))
5230 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5231 else
5233 newSelection = prevItem->firstChild;
5237 break;
5239 case VK_MULTIPLY:
5240 TREEVIEW_ExpandAll(infoPtr, prevItem);
5241 break;
5243 case VK_ADD:
5244 if (!(prevItem->state & TVIS_EXPANDED))
5245 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5246 break;
5248 case VK_SUBTRACT:
5249 if (prevItem->state & TVIS_EXPANDED)
5250 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5251 break;
5253 case VK_PRIOR:
5254 newSelection
5255 = TREEVIEW_GetListItem(infoPtr, prevItem,
5256 -TREEVIEW_GetVisibleCount(infoPtr));
5257 break;
5259 case VK_NEXT:
5260 newSelection
5261 = TREEVIEW_GetListItem(infoPtr, prevItem,
5262 TREEVIEW_GetVisibleCount(infoPtr));
5263 break;
5265 case VK_BACK:
5266 newSelection = prevItem->parent;
5267 if (newSelection == infoPtr->root)
5268 newSelection = NULL;
5269 break;
5271 case VK_SPACE:
5272 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5273 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5274 break;
5277 if (newSelection && newSelection != prevItem)
5279 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5280 TVC_BYKEYBOARD))
5282 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5286 return FALSE;
5289 static LRESULT
5290 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5292 /* remove hot effect from item */
5293 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5294 infoPtr->hotItem = NULL;
5296 return 0;
5299 static LRESULT
5300 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5302 POINT pt;
5303 TRACKMOUSEEVENT trackinfo;
5304 TREEVIEW_ITEM * item;
5306 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5308 /* fill in the TRACKMOUSEEVENT struct */
5309 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5310 trackinfo.dwFlags = TME_QUERY;
5311 trackinfo.hwndTrack = infoPtr->hwnd;
5313 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5314 _TrackMouseEvent(&trackinfo);
5316 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5317 if(!(trackinfo.dwFlags & TME_LEAVE))
5319 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5320 trackinfo.hwndTrack = infoPtr->hwnd;
5321 /* do it as fast as possible, minimal systimer latency will be used */
5322 trackinfo.dwHoverTime = 1;
5324 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5325 /* and can properly deactivate the hot item */
5326 _TrackMouseEvent(&trackinfo);
5329 pt.x = (short)LOWORD(lParam);
5330 pt.y = (short)HIWORD(lParam);
5332 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5334 if (item != infoPtr->hotItem)
5336 /* redraw old hot item */
5337 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5338 infoPtr->hotItem = item;
5339 /* redraw new hot item */
5340 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5343 return 0;
5346 /* Draw themed border */
5347 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5349 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5350 HDC dc;
5351 RECT r;
5352 HRGN cliprgn;
5353 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5354 cyEdge = GetSystemMetrics (SM_CYEDGE);
5356 if (!theme)
5357 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5359 GetWindowRect(infoPtr->hwnd, &r);
5361 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5362 r.right - cxEdge, r.bottom - cyEdge);
5363 if (region != (HRGN)1)
5364 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5365 OffsetRect(&r, -r.left, -r.top);
5367 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5368 OffsetRect(&r, -r.left, -r.top);
5370 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5371 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5372 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5373 ReleaseDC(infoPtr->hwnd, dc);
5375 /* Call default proc to get the scrollbars etc. painted */
5376 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5378 return TRUE;
5381 static LRESULT
5382 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5384 LPNMHDR lpnmh = (LPNMHDR)lParam;
5386 if (lpnmh->code == PGN_CALCSIZE) {
5387 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5389 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5390 lppgc->iWidth = infoPtr->treeWidth;
5391 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5392 infoPtr->treeWidth, infoPtr->clientWidth);
5394 else {
5395 lppgc->iHeight = infoPtr->treeHeight;
5396 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5397 infoPtr->treeHeight, infoPtr->clientHeight);
5399 return 0;
5401 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5404 static LRESULT
5405 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5407 if (wParam == SIZE_RESTORED)
5409 infoPtr->clientWidth = (short)LOWORD(lParam);
5410 infoPtr->clientHeight = (short)HIWORD(lParam);
5412 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5413 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5414 TREEVIEW_UpdateScrollBars(infoPtr);
5416 else
5418 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5421 TREEVIEW_Invalidate(infoPtr, NULL);
5422 return 0;
5425 static LRESULT
5426 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5428 TRACE("(%lx %lx)\n", wParam, lParam);
5430 if (wParam == GWL_STYLE)
5432 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5434 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5436 if (dwNewStyle & TVS_CHECKBOXES)
5438 initialize_checkboxes(infoPtr);
5439 TRACE("checkboxes enabled\n");
5441 else
5443 FIXME("tried to disable checkboxes\n");
5447 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5449 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5451 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5452 TRACE("tooltips enabled\n");
5454 else
5456 DestroyWindow(infoPtr->hwndToolTip);
5457 infoPtr->hwndToolTip = 0;
5458 TRACE("tooltips disabled\n");
5462 infoPtr->dwStyle = dwNewStyle;
5465 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5466 TREEVIEW_UpdateScrollBars(infoPtr);
5467 TREEVIEW_Invalidate(infoPtr, NULL);
5469 return 0;
5472 static LRESULT
5473 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5475 POINT pt;
5476 TREEVIEW_ITEM * item;
5477 NMMOUSE nmmouse;
5479 GetCursorPos(&pt);
5480 ScreenToClient(infoPtr->hwnd, &pt);
5482 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5484 memset(&nmmouse, 0, sizeof(nmmouse));
5485 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5486 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5487 nmmouse.hdr.code = NM_SETCURSOR;
5488 if (item)
5490 nmmouse.dwItemSpec = (DWORD_PTR)item;
5491 nmmouse.dwItemData = item->lParam;
5493 nmmouse.pt.x = 0;
5494 nmmouse.pt.y = 0;
5495 nmmouse.dwHitInfo = lParam;
5496 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, &nmmouse.hdr))
5497 return 0;
5499 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5501 SetCursor(infoPtr->hcurHand);
5502 return 0;
5504 else
5505 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5508 static LRESULT
5509 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5511 TRACE("\n");
5513 if (!infoPtr->selectedItem)
5515 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5516 TVC_UNKNOWN);
5519 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5520 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5521 return 0;
5524 static LRESULT
5525 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5527 TRACE("\n");
5529 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5530 UpdateWindow(infoPtr->hwnd);
5531 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5532 return 0;
5535 /* update theme after a WM_THEMECHANGED message */
5536 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5538 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5539 CloseThemeData (theme);
5540 OpenThemeData (infoPtr->hwnd, themeClass);
5541 return 0;
5545 static LRESULT WINAPI
5546 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5548 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5550 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5552 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5553 else
5555 if (uMsg == WM_CREATE)
5556 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5557 else
5558 goto def;
5561 switch (uMsg)
5563 case TVM_CREATEDRAGIMAGE:
5564 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5566 case TVM_DELETEITEM:
5567 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5569 case TVM_EDITLABELA:
5570 case TVM_EDITLABELW:
5571 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5573 case TVM_ENDEDITLABELNOW:
5574 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5576 case TVM_ENSUREVISIBLE:
5577 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5579 case TVM_EXPAND:
5580 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5582 case TVM_GETBKCOLOR:
5583 return TREEVIEW_GetBkColor(infoPtr);
5585 case TVM_GETCOUNT:
5586 return TREEVIEW_GetCount(infoPtr);
5588 case TVM_GETEDITCONTROL:
5589 return TREEVIEW_GetEditControl(infoPtr);
5591 case TVM_GETIMAGELIST:
5592 return TREEVIEW_GetImageList(infoPtr, wParam);
5594 case TVM_GETINDENT:
5595 return TREEVIEW_GetIndent(infoPtr);
5597 case TVM_GETINSERTMARKCOLOR:
5598 return TREEVIEW_GetInsertMarkColor(infoPtr);
5600 case TVM_GETISEARCHSTRINGA:
5601 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5602 return 0;
5604 case TVM_GETISEARCHSTRINGW:
5605 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5606 return 0;
5608 case TVM_GETITEMA:
5609 case TVM_GETITEMW:
5610 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5611 uMsg == TVM_GETITEMW);
5612 case TVM_GETITEMHEIGHT:
5613 return TREEVIEW_GetItemHeight(infoPtr);
5615 case TVM_GETITEMRECT:
5616 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5618 case TVM_GETITEMSTATE:
5619 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5621 case TVM_GETLINECOLOR:
5622 return TREEVIEW_GetLineColor(infoPtr);
5624 case TVM_GETNEXTITEM:
5625 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5627 case TVM_GETSCROLLTIME:
5628 return TREEVIEW_GetScrollTime(infoPtr);
5630 case TVM_GETTEXTCOLOR:
5631 return TREEVIEW_GetTextColor(infoPtr);
5633 case TVM_GETTOOLTIPS:
5634 return TREEVIEW_GetToolTips(infoPtr);
5636 case TVM_GETUNICODEFORMAT:
5637 return TREEVIEW_GetUnicodeFormat(infoPtr);
5639 case TVM_GETVISIBLECOUNT:
5640 return TREEVIEW_GetVisibleCount(infoPtr);
5642 case TVM_HITTEST:
5643 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5645 case TVM_INSERTITEMA:
5646 case TVM_INSERTITEMW:
5647 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5648 uMsg == TVM_INSERTITEMW);
5649 case TVM_SELECTITEM:
5650 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5652 case TVM_SETBKCOLOR:
5653 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5655 case TVM_SETIMAGELIST:
5656 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5658 case TVM_SETINDENT:
5659 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5661 case TVM_SETINSERTMARK:
5662 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5664 case TVM_SETINSERTMARKCOLOR:
5665 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5667 case TVM_SETITEMA:
5668 case TVM_SETITEMW:
5669 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5670 uMsg == TVM_SETITEMW);
5671 case TVM_SETLINECOLOR:
5672 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5674 case TVM_SETITEMHEIGHT:
5675 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5677 case TVM_SETSCROLLTIME:
5678 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5680 case TVM_SETTEXTCOLOR:
5681 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5683 case TVM_SETTOOLTIPS:
5684 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5686 case TVM_SETUNICODEFORMAT:
5687 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5689 case TVM_SORTCHILDREN:
5690 return TREEVIEW_SortChildren(infoPtr, lParam);
5692 case TVM_SORTCHILDRENCB:
5693 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5695 case WM_CHAR:
5696 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5698 case WM_COMMAND:
5699 return TREEVIEW_Command(infoPtr, wParam, lParam);
5701 case WM_DESTROY:
5702 return TREEVIEW_Destroy(infoPtr);
5704 /* WM_ENABLE */
5706 case WM_ERASEBKGND:
5707 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5709 case WM_GETDLGCODE:
5710 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5712 case WM_GETFONT:
5713 return TREEVIEW_GetFont(infoPtr);
5715 case WM_HSCROLL:
5716 return TREEVIEW_HScroll(infoPtr, wParam);
5718 case WM_KEYDOWN:
5719 return TREEVIEW_KeyDown(infoPtr, wParam);
5721 case WM_KILLFOCUS:
5722 return TREEVIEW_KillFocus(infoPtr);
5724 case WM_LBUTTONDBLCLK:
5725 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5727 case WM_LBUTTONDOWN:
5728 return TREEVIEW_LButtonDown(infoPtr, lParam);
5730 /* WM_MBUTTONDOWN */
5732 case WM_MOUSELEAVE:
5733 return TREEVIEW_MouseLeave(infoPtr);
5735 case WM_MOUSEMOVE:
5736 return TREEVIEW_MouseMove(infoPtr, lParam);
5738 case WM_NCLBUTTONDOWN:
5739 if (infoPtr->hwndEdit)
5740 SetFocus(infoPtr->hwnd);
5741 goto def;
5743 case WM_NCPAINT:
5744 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5746 case WM_NOTIFY:
5747 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5749 case WM_NOTIFYFORMAT:
5750 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5752 case WM_PRINTCLIENT:
5753 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5755 case WM_PAINT:
5756 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5758 case WM_RBUTTONDOWN:
5759 return TREEVIEW_RButtonDown(infoPtr, lParam);
5761 case WM_SETCURSOR:
5762 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5764 case WM_SETFOCUS:
5765 return TREEVIEW_SetFocus(infoPtr);
5767 case WM_SETFONT:
5768 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5770 case WM_SETREDRAW:
5771 return TREEVIEW_SetRedraw(infoPtr, wParam);
5773 case WM_SIZE:
5774 return TREEVIEW_Size(infoPtr, wParam, lParam);
5776 case WM_STYLECHANGED:
5777 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5779 case WM_SYSCOLORCHANGE:
5780 COMCTL32_RefreshSysColors();
5781 return 0;
5783 /* WM_SYSKEYDOWN */
5785 case WM_TIMER:
5786 return TREEVIEW_HandleTimer(infoPtr, wParam);
5788 case WM_THEMECHANGED:
5789 return TREEVIEW_ThemeChanged (infoPtr);
5791 case WM_VSCROLL:
5792 return TREEVIEW_VScroll(infoPtr, wParam);
5794 /* WM_WININICHANGE */
5796 case WM_MOUSEWHEEL:
5797 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5799 case WM_DRAWITEM:
5800 TRACE("drawItem\n");
5801 goto def;
5803 default:
5804 /* This mostly catches MFC and Delphi messages. :( */
5805 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5806 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5807 def:
5808 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5813 /* Class Registration ***************************************************/
5815 VOID
5816 TREEVIEW_Register(void)
5818 WNDCLASSW wndClass;
5820 TRACE("\n");
5822 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5823 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5824 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5825 wndClass.cbClsExtra = 0;
5826 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5828 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5829 wndClass.hbrBackground = 0;
5830 wndClass.lpszClassName = WC_TREEVIEWW;
5832 RegisterClassW(&wndClass);
5836 VOID
5837 TREEVIEW_Unregister(void)
5839 UnregisterClassW(WC_TREEVIEWW, NULL);
5843 /* Tree Verification ****************************************************/
5845 static inline void
5846 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5848 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5849 const TREEVIEW_ITEM *item)
5851 assert(infoPtr != NULL);
5852 assert(item != NULL);
5854 /* both NULL, or both non-null */
5855 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5857 assert(item->firstChild != item);
5858 assert(item->lastChild != item);
5860 if (item->firstChild)
5862 assert(item->firstChild->parent == item);
5863 assert(item->firstChild->prevSibling == NULL);
5866 if (item->lastChild)
5868 assert(item->lastChild->parent == item);
5869 assert(item->lastChild->nextSibling == NULL);
5872 assert(item->nextSibling != item);
5873 if (item->nextSibling)
5875 assert(item->nextSibling->parent == item->parent);
5876 assert(item->nextSibling->prevSibling == item);
5879 assert(item->prevSibling != item);
5880 if (item->prevSibling)
5882 assert(item->prevSibling->parent == item->parent);
5883 assert(item->prevSibling->nextSibling == item);
5887 static inline void
5888 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5890 assert(item != NULL);
5892 assert(item->parent != NULL);
5893 assert(item->parent != item);
5894 assert(item->iLevel == item->parent->iLevel + 1);
5896 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5898 TREEVIEW_VerifyItemCommon(infoPtr, item);
5900 TREEVIEW_VerifyChildren(infoPtr, item);
5903 static inline void
5904 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5906 const TREEVIEW_ITEM *child;
5907 assert(item != NULL);
5909 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5910 TREEVIEW_VerifyItem(infoPtr, child);
5913 static inline void
5914 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5916 TREEVIEW_ITEM *root = infoPtr->root;
5918 assert(root != NULL);
5919 assert(root->iLevel == -1);
5920 assert(root->parent == NULL);
5921 assert(root->prevSibling == NULL);
5923 TREEVIEW_VerifyItemCommon(infoPtr, root);
5925 TREEVIEW_VerifyChildren(infoPtr, root);
5928 static void
5929 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5931 if (!TRACE_ON(treeview)) return;
5933 assert(infoPtr != NULL);
5934 TREEVIEW_VerifyRoot(infoPtr);