wined3d: Fix a few small bugs in WineD3D_ChoosePixelFormat.
[wine/wine-kai.git] / dlls / comctl32 / treeview.c
blobfb67c24960e36e9098d04aa68f88a2a960494e91
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: All items always! have valid (allocated) pszText field.
27 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
28 * of size TEXT_CALLBACK_SIZE in DoSetItem.
29 * We use callbackMask to keep track of fields to be updated.
31 * TODO:
32 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
33 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
35 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
37 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
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 /* internal structures */
69 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
71 UINT callbackMask;
72 UINT state;
73 UINT stateMask;
74 LPWSTR pszText;
75 int cchTextMax;
76 int iImage;
77 int iSelectedImage;
78 int cChildren;
79 LPARAM lParam;
80 int iIntegral; /* item height multiplier (1 is normal) */
81 int iLevel; /* indentation level:0=root level */
82 HTREEITEM parent; /* handle to parent or 0 if at root */
83 HTREEITEM firstChild; /* handle to first child or 0 if no child */
84 HTREEITEM lastChild;
85 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
86 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
87 RECT rect;
88 LONG linesOffset;
89 LONG stateOffset;
90 LONG imageOffset;
91 LONG textOffset;
92 LONG textWidth; /* horizontal text extent for pszText */
93 LONG visibleOrder; /* visible ordering, 0 is first visible item */
94 } TREEVIEW_ITEM;
97 typedef struct tagTREEVIEW_INFO
99 HWND hwnd;
100 HWND hwndNotify; /* Owner window to send notifications to */
101 DWORD dwStyle;
102 HTREEITEM root;
103 UINT uInternalStatus;
104 INT Timer;
105 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
106 INT cdmode; /* last custom draw setting */
107 UINT uScrollTime; /* max. time for scrolling in milliseconds */
108 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
110 UINT uItemHeight; /* item height */
111 BOOL bHeightSet;
113 LONG clientWidth; /* width of control window */
114 LONG clientHeight; /* height of control window */
116 LONG treeWidth; /* width of visible tree items */
117 LONG treeHeight; /* height of visible tree items */
119 UINT uIndent; /* indentation in pixels */
120 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
121 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
122 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
124 HTREEITEM firstVisible; /* handle to first visible item */
125 LONG maxVisibleOrder;
126 HTREEITEM dropItem; /* handle to item selected by drag cursor */
127 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
128 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
129 HIMAGELIST dragList; /* Bitmap of dragged item */
130 LONG scrollX;
131 COLORREF clrBk;
132 COLORREF clrText;
133 COLORREF clrLine;
134 COLORREF clrInsertMark;
135 HFONT hFont;
136 HFONT hDefaultFont;
137 HFONT hBoldFont;
138 HFONT hUnderlineFont;
139 HCURSOR hcurHand;
140 HWND hwndToolTip;
142 HWND hwndEdit;
143 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
144 BOOL bIgnoreEditKillFocus;
145 BOOL bLabelChanged;
147 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
148 HIMAGELIST himlNormal;
149 int normalImageHeight;
150 int normalImageWidth;
151 HIMAGELIST himlState;
152 int stateImageHeight;
153 int stateImageWidth;
154 HDPA items;
156 DWORD lastKeyPressTimestamp; /* Added */
157 WPARAM charCode; /* Added */
158 INT nSearchParamLength; /* Added */
159 WCHAR szSearchParam[ MAX_PATH ]; /* Added */
160 } TREEVIEW_INFO;
163 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
164 #define KEY_DELAY 450
166 /* bitflags for infoPtr->uInternalStatus */
168 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
169 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
170 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
171 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
172 #define TV_RDRAG 0x10 /* ditto Rbutton */
173 #define TV_RDRAGGING 0x20
175 /* bitflags for infoPtr->timer */
177 #define TV_EDIT_TIMER 2
178 #define TV_EDIT_TIMER_SET 2
181 VOID TREEVIEW_Register (VOID);
182 VOID TREEVIEW_Unregister (VOID);
185 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
188 #define TEXT_CALLBACK_SIZE 260
190 #define TREEVIEW_LEFT_MARGIN 8
192 #define MINIMUM_INDENT 19
194 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
196 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
197 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
198 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
201 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
204 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
207 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
209 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
210 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
211 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
212 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
213 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
214 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
215 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
216 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND wParam, UINT lParam);
219 /* Random Utilities *****************************************************/
221 #ifndef NDEBUG
222 static inline void
223 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
225 (void)infoPtr;
227 #else
228 /* The definition is at the end of the file. */
229 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
230 #endif
232 /* Returns the treeview private data if hwnd is a treeview.
233 * Otherwise returns an undefined value. */
234 static TREEVIEW_INFO *
235 TREEVIEW_GetInfoPtr(HWND hwnd)
237 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
240 /* Don't call this. Nothing wants an item index. */
241 static inline int
242 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
244 return DPA_GetPtrIndex(infoPtr->items, handle);
247 /* Checks if item has changed and needs to be redrawn */
248 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
249 const TVITEMEXW *tvChange)
251 /* Number of children has changed */
252 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
253 return TRUE;
255 /* Image has changed and it's not a callback */
256 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
257 tiNew->iImage != I_IMAGECALLBACK)
258 return TRUE;
260 /* Selected image has changed and it's not a callback */
261 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
262 tiNew->iSelectedImage != I_IMAGECALLBACK)
263 return TRUE;
265 /* Text has changed and it's not a callback */
266 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
267 tiNew->pszText != LPSTR_TEXTCALLBACKW)
268 return TRUE;
270 /* Indent has changed */
271 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
272 return TRUE;
274 /* Item state has changed */
275 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
276 return TRUE;
278 return FALSE;
281 /***************************************************************************
282 * This method checks that handle is an item for this tree.
284 static BOOL
285 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
287 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
289 TRACE("invalid item %p\n", handle);
290 return FALSE;
292 else
293 return TRUE;
296 static HFONT
297 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
299 LOGFONTW font;
301 GetObjectW(hOrigFont, sizeof(font), &font);
302 font.lfWeight = FW_BOLD;
303 return CreateFontIndirectW(&font);
306 static HFONT
307 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
309 LOGFONTW font;
311 GetObjectW(hOrigFont, sizeof(font), &font);
312 font.lfUnderline = TRUE;
313 return CreateFontIndirectW(&font);
316 static inline HFONT
317 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
319 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
320 return infoPtr->hUnderlineFont;
321 if (item->state & TVIS_BOLD)
322 return infoPtr->hBoldFont;
323 return infoPtr->hFont;
326 /* for trace/debugging purposes only */
327 static const char *
328 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
330 if (item == NULL) return "<null item>";
331 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
332 if (item->pszText == NULL) return "<null>";
333 return debugstr_w(item->pszText);
336 /* An item is not a child of itself. */
337 static BOOL
338 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
342 child = child->parent;
343 if (child == parent) return TRUE;
344 } while (child != NULL);
346 return FALSE;
350 /* Tree Traversal *******************************************************/
352 /***************************************************************************
353 * This method returns the last expanded sibling or child child item
354 * of a tree node
356 static TREEVIEW_ITEM *
357 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
359 if (!wineItem)
360 return NULL;
362 while (wineItem->lastChild)
364 if (wineItem->state & TVIS_EXPANDED)
365 wineItem = wineItem->lastChild;
366 else
367 break;
370 if (wineItem == infoPtr->root)
371 return NULL;
373 return wineItem;
376 /***************************************************************************
377 * This method returns the previous non-hidden item in the list not
378 * considering the tree hierarchy.
380 static TREEVIEW_ITEM *
381 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
383 if (tvItem->prevSibling)
385 /* This item has a prevSibling, get the last item in the sibling's tree. */
386 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
388 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
389 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
390 else
391 return upItem;
393 else
395 /* this item does not have a prevSibling, get the parent */
396 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
401 /***************************************************************************
402 * This method returns the next physical item in the treeview not
403 * considering the tree hierarchy.
405 static TREEVIEW_ITEM *
406 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
409 * If this item has children and is expanded, return the first child
411 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
413 return tvItem->firstChild;
418 * try to get the sibling
420 if (tvItem->nextSibling)
421 return tvItem->nextSibling;
424 * Otherwise, get the parent's sibling.
426 while (tvItem->parent)
428 tvItem = tvItem->parent;
430 if (tvItem->nextSibling)
431 return tvItem->nextSibling;
434 return NULL;
437 /***************************************************************************
438 * This method returns the nth item starting at the given item. It returns
439 * the last item (or first) we we run out of items.
441 * Will scroll backward if count is <0.
442 * forward if count is >0.
444 static TREEVIEW_ITEM *
445 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
446 LONG count)
448 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
449 TREEVIEW_ITEM *previousItem;
451 assert(wineItem != NULL);
453 if (count > 0)
455 next_item = TREEVIEW_GetNextListItem;
457 else if (count < 0)
459 count = -count;
460 next_item = TREEVIEW_GetPrevListItem;
462 else
463 return wineItem;
467 previousItem = wineItem;
468 wineItem = next_item(infoPtr, wineItem);
470 } while (--count && wineItem != NULL);
473 return wineItem ? wineItem : previousItem;
476 /* Notifications ************************************************************/
478 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
480 if (!infoPtr->bNtfUnicode) {
481 switch (code) {
482 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
483 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
484 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
485 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
486 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
487 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
488 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
489 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
490 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
491 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
492 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
493 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
496 return code;
499 static LRESULT
500 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
502 TRACE("wParam=%ld, lParam=%ld\n", wParam, lParam);
503 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
506 static BOOL
507 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
509 NMHDR nmhdr;
510 HWND hwnd = infoPtr->hwnd;
512 TRACE("%d\n", code);
513 nmhdr.hwndFrom = hwnd;
514 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
515 nmhdr.code = get_notifycode(infoPtr, code);
517 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, (LPARAM)&nmhdr);
520 static VOID
521 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
523 tvItem->mask = mask;
524 tvItem->hItem = item;
525 tvItem->state = item->state;
526 tvItem->stateMask = 0;
527 tvItem->iImage = item->iImage;
528 tvItem->iSelectedImage = item->iSelectedImage;
529 tvItem->cChildren = item->cChildren;
530 tvItem->lParam = item->lParam;
532 if(mask & TVIF_TEXT)
534 if (!infoPtr->bNtfUnicode)
536 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
537 tvItem->pszText = Alloc (tvItem->cchTextMax);
538 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
540 else
542 tvItem->cchTextMax = item->cchTextMax;
543 tvItem->pszText = item->pszText;
546 else
548 tvItem->cchTextMax = 0;
549 tvItem->pszText = NULL;
553 static BOOL
554 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
555 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
557 HWND hwnd = infoPtr->hwnd;
558 NMTREEVIEWW nmhdr;
559 BOOL ret;
561 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
562 code, action, oldItem, newItem);
564 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
566 nmhdr.hdr.hwndFrom = hwnd;
567 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
568 nmhdr.hdr.code = get_notifycode(infoPtr, code);
569 nmhdr.action = action;
571 if (oldItem)
572 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
574 if (newItem)
575 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
577 nmhdr.ptDrag.x = 0;
578 nmhdr.ptDrag.y = 0;
580 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
581 if (!infoPtr->bNtfUnicode)
583 Free(nmhdr.itemOld.pszText);
584 Free(nmhdr.itemNew.pszText);
586 return ret;
589 static BOOL
590 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
591 HTREEITEM dragItem, POINT pt)
593 HWND hwnd = infoPtr->hwnd;
594 NMTREEVIEWW nmhdr;
596 TRACE("code:%d dragitem:%p\n", code, dragItem);
598 nmhdr.hdr.hwndFrom = hwnd;
599 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
600 nmhdr.hdr.code = get_notifycode(infoPtr, code);
601 nmhdr.action = 0;
602 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
603 nmhdr.itemNew.hItem = dragItem;
604 nmhdr.itemNew.state = dragItem->state;
605 nmhdr.itemNew.lParam = dragItem->lParam;
607 nmhdr.ptDrag.x = pt.x;
608 nmhdr.ptDrag.y = pt.y;
610 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
614 static BOOL
615 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
616 HDC hdc, RECT rc)
618 HWND hwnd = infoPtr->hwnd;
619 NMTVCUSTOMDRAW nmcdhdr;
620 LPNMCUSTOMDRAW nmcd;
622 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
624 nmcd = &nmcdhdr.nmcd;
625 nmcd->hdr.hwndFrom = hwnd;
626 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
627 nmcd->hdr.code = NM_CUSTOMDRAW;
628 nmcd->dwDrawStage = dwDrawStage;
629 nmcd->hdc = hdc;
630 nmcd->rc = rc;
631 nmcd->dwItemSpec = 0;
632 nmcd->uItemState = 0;
633 nmcd->lItemlParam = 0;
634 nmcdhdr.clrText = infoPtr->clrText;
635 nmcdhdr.clrTextBk = infoPtr->clrBk;
636 nmcdhdr.iLevel = 0;
638 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)&nmcdhdr);
643 /* FIXME: need to find out when the flags in uItemState need to be set */
645 static BOOL
646 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
647 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
648 NMTVCUSTOMDRAW *nmcdhdr)
650 HWND hwnd = infoPtr->hwnd;
651 LPNMCUSTOMDRAW nmcd;
652 DWORD dwDrawStage;
653 DWORD_PTR dwItemSpec;
654 UINT uItemState;
655 INT retval;
657 dwDrawStage = CDDS_ITEM | uItemDrawState;
658 dwItemSpec = (DWORD_PTR)wineItem;
659 uItemState = 0;
660 if (wineItem->state & TVIS_SELECTED)
661 uItemState |= CDIS_SELECTED;
662 if (wineItem == infoPtr->selectedItem)
663 uItemState |= CDIS_FOCUS;
664 if (wineItem == infoPtr->hotItem)
665 uItemState |= CDIS_HOT;
667 nmcd = &nmcdhdr->nmcd;
668 nmcd->hdr.hwndFrom = hwnd;
669 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
670 nmcd->hdr.code = NM_CUSTOMDRAW;
671 nmcd->dwDrawStage = dwDrawStage;
672 nmcd->hdc = hdc;
673 nmcd->rc = wineItem->rect;
674 nmcd->dwItemSpec = dwItemSpec;
675 nmcd->uItemState = uItemState;
676 nmcd->lItemlParam = wineItem->lParam;
677 nmcdhdr->iLevel = wineItem->iLevel;
679 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
680 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
681 nmcd->uItemState, nmcd->lItemlParam);
683 retval = TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)nmcdhdr);
685 return retval;
688 static BOOL
689 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
691 HWND hwnd = infoPtr->hwnd;
692 NMTVDISPINFOW tvdi;
693 BOOL ret;
695 tvdi.hdr.hwndFrom = hwnd;
696 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
697 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
699 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
700 &tvdi.item, editItem);
702 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
704 if (!infoPtr->bNtfUnicode)
705 Free(tvdi.item.pszText);
707 return ret;
710 static void
711 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
712 UINT mask)
714 NMTVDISPINFOW callback;
715 HWND hwnd = infoPtr->hwnd;
717 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
718 mask &= wineItem->callbackMask;
720 if (mask == 0) return;
722 callback.hdr.hwndFrom = hwnd;
723 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
724 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
726 /* 'state' always contains valid value, as well as 'lParam'.
727 * All other parameters are uninitialized.
729 callback.item.pszText = wineItem->pszText;
730 callback.item.cchTextMax = wineItem->cchTextMax;
731 callback.item.mask = mask;
732 callback.item.hItem = wineItem;
733 callback.item.state = wineItem->state;
734 callback.item.lParam = wineItem->lParam;
736 /* If text is changed we need to recalculate textWidth */
737 if (mask & TVIF_TEXT)
738 wineItem->textWidth = 0;
740 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, (LPARAM)&callback);
742 /* It may have changed due to a call to SetItem. */
743 mask &= wineItem->callbackMask;
745 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
747 /* Instead of copying text into our buffer user specified its own */
748 if (!infoPtr->bNtfUnicode) {
749 LPWSTR newText;
750 int buflen;
751 int len = MultiByteToWideChar( CP_ACP, 0,
752 (LPSTR)callback.item.pszText, -1,
753 NULL, 0);
754 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
755 newText = (LPWSTR)ReAlloc(wineItem->pszText, buflen);
757 TRACE("returned str %s, len=%d, buflen=%d\n",
758 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
760 if (newText)
762 wineItem->pszText = newText;
763 MultiByteToWideChar( CP_ACP, 0,
764 (LPSTR)callback.item.pszText, -1,
765 wineItem->pszText, buflen/sizeof(WCHAR));
766 wineItem->cchTextMax = buflen/sizeof(WCHAR);
768 /* If ReAlloc fails we have nothing to do, but keep original text */
770 else {
771 int len = max(lstrlenW(callback.item.pszText) + 1,
772 TEXT_CALLBACK_SIZE);
773 LPWSTR newText = ReAlloc(wineItem->pszText, len);
775 TRACE("returned wstr %s, len=%d\n",
776 debugstr_w(callback.item.pszText), len);
778 if (newText)
780 wineItem->pszText = newText;
781 strcpyW(wineItem->pszText, callback.item.pszText);
782 wineItem->cchTextMax = len;
784 /* If ReAlloc fails we have nothing to do, but keep original text */
787 else if (mask & TVIF_TEXT) {
788 /* User put text into our buffer, that is ok unless A string */
789 if (!infoPtr->bNtfUnicode) {
790 LPWSTR newText;
791 LPWSTR oldText = NULL;
792 int buflen;
793 int len = MultiByteToWideChar( CP_ACP, 0,
794 (LPSTR)callback.item.pszText, -1,
795 NULL, 0);
796 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
797 newText = (LPWSTR)Alloc(buflen);
799 TRACE("same buffer str %s, len=%d, buflen=%d\n",
800 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
802 if (newText)
804 oldText = wineItem->pszText;
805 wineItem->pszText = newText;
806 MultiByteToWideChar( CP_ACP, 0,
807 (LPSTR)callback.item.pszText, -1,
808 wineItem->pszText, buflen/sizeof(WCHAR));
809 wineItem->cchTextMax = buflen/sizeof(WCHAR);
810 Free(oldText);
815 if (mask & TVIF_IMAGE)
816 wineItem->iImage = callback.item.iImage;
818 if (mask & TVIF_SELECTEDIMAGE)
819 wineItem->iSelectedImage = callback.item.iSelectedImage;
821 if (mask & TVIF_CHILDREN)
822 wineItem->cChildren = callback.item.cChildren;
824 /* These members are now permanently set. */
825 if (callback.item.mask & TVIF_DI_SETITEM)
826 wineItem->callbackMask &= ~callback.item.mask;
829 /***************************************************************************
830 * This function uses cChildren field to decide whether the item has
831 * children or not.
832 * Note: if this returns TRUE, the child items may not actually exist,
833 * they could be virtual.
835 * Just use wineItem->firstChild to check for physical children.
837 static BOOL
838 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
840 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
842 return wineItem->cChildren > 0;
846 /* Item Position ********************************************************/
848 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
849 static VOID
850 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
852 /* Same effect, different optimisation. */
853 #if 0
854 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
855 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
856 #else
857 BOOL lar = ((infoPtr->dwStyle
858 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
859 > TVS_LINESATROOT);
860 #endif
862 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
863 - infoPtr->scrollX;
864 item->stateOffset = item->linesOffset + infoPtr->uIndent;
865 item->imageOffset = item->stateOffset
866 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
867 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
870 static VOID
871 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
873 HDC hdc;
874 HFONT hOldFont=0;
875 SIZE sz;
877 /* DRAW's OM docker creates items like this */
878 if (item->pszText == NULL)
880 item->textWidth = 0;
881 return;
884 if (hDC != 0)
886 hdc = hDC;
888 else
890 hdc = GetDC(infoPtr->hwnd);
891 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
894 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
895 item->textWidth = sz.cx;
897 if (hDC == 0)
899 SelectObject(hdc, hOldFont);
900 ReleaseDC(0, hdc);
904 static VOID
905 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
907 item->rect.top = infoPtr->uItemHeight *
908 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
910 item->rect.bottom = item->rect.top
911 + infoPtr->uItemHeight * item->iIntegral - 1;
913 item->rect.left = 0;
914 item->rect.right = infoPtr->clientWidth;
917 /* We know that only items after start need their order updated. */
918 static void
919 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
921 TREEVIEW_ITEM *item;
922 int order;
924 if (!start)
926 start = infoPtr->root->firstChild;
927 order = 0;
929 else
930 order = start->visibleOrder;
932 for (item = start; item != NULL;
933 item = TREEVIEW_GetNextListItem(infoPtr, item))
935 if (!ISVISIBLE(item) && order > 0)
936 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
937 item->visibleOrder = order;
938 order += item->iIntegral;
941 infoPtr->maxVisibleOrder = order;
943 for (item = start; item != NULL;
944 item = TREEVIEW_GetNextListItem(infoPtr, item))
946 TREEVIEW_ComputeItemRect(infoPtr, item);
951 /* Update metrics of all items in selected subtree.
952 * root must be expanded
954 static VOID
955 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
957 TREEVIEW_ITEM *sibling;
958 HDC hdc;
959 HFONT hOldFont;
961 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
962 return;
964 root->state &= ~TVIS_EXPANDED;
965 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
966 root->state |= TVIS_EXPANDED;
968 hdc = GetDC(infoPtr->hwnd);
969 hOldFont = SelectObject(hdc, infoPtr->hFont);
971 for (; root != sibling;
972 root = TREEVIEW_GetNextListItem(infoPtr, root))
974 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
976 if (root->callbackMask & TVIF_TEXT)
977 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
979 if (root->textWidth == 0)
981 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
982 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
986 SelectObject(hdc, hOldFont);
987 ReleaseDC(infoPtr->hwnd, hdc);
990 /* Item Allocation **********************************************************/
992 static TREEVIEW_ITEM *
993 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
995 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
997 if (!newItem)
998 return NULL;
1000 /* I_IMAGENONE would make more sense but this is neither what is
1001 * documented (MSDN doesn't specify) nor what Windows actually does
1002 * (it sets it to zero)... and I can so imagine an application using
1003 * inc/dec to toggle the images. */
1004 newItem->iImage = 0;
1005 newItem->iSelectedImage = 0;
1007 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1009 Free(newItem);
1010 return NULL;
1013 return newItem;
1016 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1017 * free item->pszText. */
1018 static void
1019 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1021 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1022 if (infoPtr->selectedItem == item)
1023 infoPtr->selectedItem = NULL;
1024 if (infoPtr->hotItem == item)
1025 infoPtr->hotItem = NULL;
1026 if (infoPtr->focusedItem == item)
1027 infoPtr->focusedItem = NULL;
1028 if (infoPtr->firstVisible == item)
1029 infoPtr->firstVisible = NULL;
1030 if (infoPtr->dropItem == item)
1031 infoPtr->dropItem = NULL;
1032 if (infoPtr->insertMarkItem == item)
1033 infoPtr->insertMarkItem = NULL;
1034 Free(item);
1038 /* Item Insertion *******************************************************/
1040 /***************************************************************************
1041 * This method inserts newItem before sibling as a child of parent.
1042 * sibling can be NULL, but only if parent has no children.
1044 static void
1045 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1046 TREEVIEW_ITEM *parent)
1048 assert(parent != NULL);
1050 if (sibling != NULL)
1052 assert(sibling->parent == parent);
1054 if (sibling->prevSibling != NULL)
1055 sibling->prevSibling->nextSibling = newItem;
1057 newItem->prevSibling = sibling->prevSibling;
1058 sibling->prevSibling = newItem;
1060 else
1061 newItem->prevSibling = NULL;
1063 newItem->nextSibling = sibling;
1065 if (parent->firstChild == sibling)
1066 parent->firstChild = newItem;
1068 if (parent->lastChild == NULL)
1069 parent->lastChild = newItem;
1072 /***************************************************************************
1073 * This method inserts newItem after sibling as a child of parent.
1074 * sibling can be NULL, but only if parent has no children.
1076 static void
1077 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1078 TREEVIEW_ITEM *parent)
1080 assert(parent != NULL);
1082 if (sibling != NULL)
1084 assert(sibling->parent == parent);
1086 if (sibling->nextSibling != NULL)
1087 sibling->nextSibling->prevSibling = newItem;
1089 newItem->nextSibling = sibling->nextSibling;
1090 sibling->nextSibling = newItem;
1092 else
1093 newItem->nextSibling = NULL;
1095 newItem->prevSibling = sibling;
1097 if (parent->lastChild == sibling)
1098 parent->lastChild = newItem;
1100 if (parent->firstChild == NULL)
1101 parent->firstChild = newItem;
1104 static BOOL
1105 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1106 const TVITEMEXW *tvItem, BOOL isW)
1108 UINT callbackClear = 0;
1109 UINT callbackSet = 0;
1111 TRACE("item %p\n", wineItem);
1112 /* Do this first in case it fails. */
1113 if (tvItem->mask & TVIF_TEXT)
1115 wineItem->textWidth = 0; /* force width recalculation */
1116 if (tvItem->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
1118 int len;
1119 LPWSTR newText;
1120 if (isW)
1121 len = lstrlenW(tvItem->pszText) + 1;
1122 else
1123 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1125 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1127 if (newText == NULL) return FALSE;
1129 callbackClear |= TVIF_TEXT;
1131 wineItem->pszText = newText;
1132 wineItem->cchTextMax = len;
1133 if (isW)
1134 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1135 else
1136 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1137 wineItem->pszText, len);
1139 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1141 else
1143 callbackSet |= TVIF_TEXT;
1145 wineItem->pszText = ReAlloc(wineItem->pszText,
1146 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1147 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1148 TRACE("setting callback, item %p\n", wineItem);
1152 if (tvItem->mask & TVIF_CHILDREN)
1154 wineItem->cChildren = tvItem->cChildren;
1156 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1157 callbackSet |= TVIF_CHILDREN;
1158 else
1159 callbackClear |= TVIF_CHILDREN;
1162 if (tvItem->mask & TVIF_IMAGE)
1164 wineItem->iImage = tvItem->iImage;
1166 if (wineItem->iImage == I_IMAGECALLBACK)
1167 callbackSet |= TVIF_IMAGE;
1168 else
1169 callbackClear |= TVIF_IMAGE;
1172 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1174 wineItem->iSelectedImage = tvItem->iSelectedImage;
1176 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1177 callbackSet |= TVIF_SELECTEDIMAGE;
1178 else
1179 callbackClear |= TVIF_SELECTEDIMAGE;
1182 if (tvItem->mask & TVIF_PARAM)
1183 wineItem->lParam = tvItem->lParam;
1185 /* If the application sets TVIF_INTEGRAL without
1186 * supplying a TVITEMEX structure, it's toast. */
1187 if (tvItem->mask & TVIF_INTEGRAL)
1188 wineItem->iIntegral = tvItem->iIntegral;
1190 if (tvItem->mask & TVIF_STATE)
1192 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1193 tvItem->stateMask);
1194 wineItem->state &= ~tvItem->stateMask;
1195 wineItem->state |= (tvItem->state & tvItem->stateMask);
1198 wineItem->callbackMask |= callbackSet;
1199 wineItem->callbackMask &= ~callbackClear;
1201 return TRUE;
1204 /* Note that the new item is pre-zeroed. */
1205 static LRESULT
1206 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1208 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1209 HTREEITEM insertAfter;
1210 TREEVIEW_ITEM *newItem, *parentItem;
1211 BOOL bTextUpdated = FALSE;
1213 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1215 parentItem = infoPtr->root;
1217 else
1219 parentItem = ptdi->hParent;
1221 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1223 WARN("invalid parent %p\n", parentItem);
1224 return (LRESULT)(HTREEITEM)NULL;
1228 insertAfter = ptdi->hInsertAfter;
1230 /* Validate this now for convenience. */
1231 switch ((DWORD_PTR)insertAfter)
1233 case (DWORD_PTR)TVI_FIRST:
1234 case (DWORD_PTR)TVI_LAST:
1235 case (DWORD_PTR)TVI_SORT:
1236 break;
1238 default:
1239 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1240 insertAfter->parent != parentItem)
1242 WARN("invalid insert after %p\n", insertAfter);
1243 insertAfter = TVI_LAST;
1247 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1248 (tvItem->mask & TVIF_TEXT)
1249 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1250 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1251 : "<no label>");
1253 newItem = TREEVIEW_AllocateItem(infoPtr);
1254 if (newItem == NULL)
1255 return (LRESULT)(HTREEITEM)NULL;
1257 newItem->parent = parentItem;
1258 newItem->iIntegral = 1;
1260 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1261 return (LRESULT)(HTREEITEM)NULL;
1263 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1265 infoPtr->uNumItems++;
1267 switch ((DWORD_PTR)insertAfter)
1269 case (DWORD_PTR)TVI_FIRST:
1271 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1272 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1273 if (infoPtr->firstVisible == originalFirst)
1274 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1276 break;
1278 case (DWORD_PTR)TVI_LAST:
1279 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1280 break;
1282 /* hInsertAfter names a specific item we want to insert after */
1283 default:
1284 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1285 break;
1287 case (DWORD_PTR)TVI_SORT:
1289 TREEVIEW_ITEM *aChild;
1290 TREEVIEW_ITEM *previousChild = NULL;
1291 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1292 BOOL bItemInserted = FALSE;
1294 aChild = parentItem->firstChild;
1296 bTextUpdated = TRUE;
1297 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1299 /* Iterate the parent children to see where we fit in */
1300 while (aChild != NULL)
1302 INT comp;
1304 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1305 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1307 if (comp < 0) /* we are smaller than the current one */
1309 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1310 if (infoPtr->firstVisible == originalFirst &&
1311 aChild == originalFirst)
1312 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1313 bItemInserted = TRUE;
1314 break;
1316 else if (comp > 0) /* we are bigger than the current one */
1318 previousChild = aChild;
1320 /* This will help us to exit if there is no more sibling */
1321 aChild = (aChild->nextSibling == 0)
1322 ? NULL
1323 : aChild->nextSibling;
1325 /* Look at the next item */
1326 continue;
1328 else if (comp == 0)
1331 * An item with this name is already existing, therefore,
1332 * we add after the one we found
1334 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1335 bItemInserted = TRUE;
1336 break;
1341 * we reach the end of the child list and the item has not
1342 * yet been inserted, therefore, insert it after the last child.
1344 if ((!bItemInserted) && (aChild == NULL))
1345 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1347 break;
1352 TRACE("new item %p; parent %p, mask %x\n", newItem,
1353 newItem->parent, tvItem->mask);
1355 newItem->iLevel = newItem->parent->iLevel + 1;
1357 if (newItem->parent->cChildren == 0)
1358 newItem->parent->cChildren = 1;
1360 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1362 if (STATEIMAGEINDEX(newItem->state) == 0)
1363 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1366 if (infoPtr->firstVisible == NULL)
1367 infoPtr->firstVisible = newItem;
1369 TREEVIEW_VerifyTree(infoPtr);
1371 if (parentItem == infoPtr->root ||
1372 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1374 TREEVIEW_ITEM *item;
1375 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1377 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1378 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1380 if (!bTextUpdated)
1381 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1383 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1384 TREEVIEW_UpdateScrollBars(infoPtr);
1386 * if the item was inserted in a visible part of the tree,
1387 * invalidate it, as well as those after it
1389 for (item = newItem;
1390 item != NULL;
1391 item = TREEVIEW_GetNextListItem(infoPtr, item))
1392 TREEVIEW_Invalidate(infoPtr, item);
1394 else
1396 newItem->visibleOrder = -1;
1398 /* refresh treeview if newItem is the first item inserted under parentItem */
1399 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1401 /* parent got '+' - update it */
1402 TREEVIEW_Invalidate(infoPtr, parentItem);
1406 return (LRESULT)newItem;
1409 /* Item Deletion ************************************************************/
1410 static void
1411 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1413 static void
1414 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1416 TREEVIEW_ITEM *kill = parentItem->firstChild;
1418 while (kill != NULL)
1420 TREEVIEW_ITEM *next = kill->nextSibling;
1422 TREEVIEW_RemoveItem(infoPtr, kill);
1424 kill = next;
1427 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1428 assert(parentItem->firstChild == NULL);
1429 assert(parentItem->lastChild == NULL);
1432 static void
1433 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1435 TREEVIEW_ITEM *parentItem = item->parent;
1437 assert(item != NULL);
1438 assert(item->parent != NULL); /* i.e. it must not be the root */
1440 if (parentItem->firstChild == item)
1441 parentItem->firstChild = item->nextSibling;
1443 if (parentItem->lastChild == item)
1444 parentItem->lastChild = item->prevSibling;
1446 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1447 && parentItem->cChildren > 0)
1448 parentItem->cChildren = 0;
1450 if (item->prevSibling)
1451 item->prevSibling->nextSibling = item->nextSibling;
1453 if (item->nextSibling)
1454 item->nextSibling->prevSibling = item->prevSibling;
1457 static void
1458 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1460 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1462 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1463 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1465 if (wineItem->firstChild)
1466 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1468 TREEVIEW_UnlinkItem(wineItem);
1470 infoPtr->uNumItems--;
1472 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1473 Free(wineItem->pszText);
1475 TREEVIEW_FreeItem(infoPtr, wineItem);
1479 /* Empty out the tree. */
1480 static void
1481 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1483 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1485 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1488 static LRESULT
1489 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1491 TREEVIEW_ITEM *newSelection = NULL;
1492 TREEVIEW_ITEM *newFirstVisible = NULL;
1493 TREEVIEW_ITEM *parent, *prev = NULL;
1494 BOOL visible = FALSE;
1496 if (wineItem == TVI_ROOT)
1498 TRACE("TVI_ROOT\n");
1499 parent = infoPtr->root;
1500 newSelection = NULL;
1501 visible = TRUE;
1502 TREEVIEW_RemoveTree(infoPtr);
1504 else
1506 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1507 return FALSE;
1509 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1510 parent = wineItem->parent;
1512 if (ISVISIBLE(wineItem))
1514 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1515 visible = TRUE;
1518 if (infoPtr->selectedItem != NULL
1519 && (wineItem == infoPtr->selectedItem
1520 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1522 if (wineItem->nextSibling)
1523 newSelection = wineItem->nextSibling;
1524 else if (wineItem->parent != infoPtr->root)
1525 newSelection = wineItem->parent;
1526 else
1527 newSelection = wineItem->prevSibling;
1528 TRACE("newSelection = %p\n", newSelection);
1531 if (infoPtr->firstVisible == wineItem)
1533 if (wineItem->nextSibling)
1534 newFirstVisible = wineItem->nextSibling;
1535 else if (wineItem->prevSibling)
1536 newFirstVisible = wineItem->prevSibling;
1537 else if (wineItem->parent != infoPtr->root)
1538 newFirstVisible = wineItem->parent;
1539 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1541 else
1542 newFirstVisible = infoPtr->firstVisible;
1544 TREEVIEW_RemoveItem(infoPtr, wineItem);
1547 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1548 if (!infoPtr->selectedItem && newSelection)
1550 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1551 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1554 /* Validate insertMark dropItem.
1555 * hotItem ??? - used for comparison only.
1557 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1558 infoPtr->insertMarkItem = 0;
1560 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1561 infoPtr->dropItem = 0;
1563 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1564 newFirstVisible = infoPtr->root->firstChild;
1566 TREEVIEW_VerifyTree(infoPtr);
1569 if (visible)
1571 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1572 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1573 TREEVIEW_UpdateScrollBars(infoPtr);
1574 TREEVIEW_Invalidate(infoPtr, NULL);
1576 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1578 /* parent lost '+/-' - update it */
1579 TREEVIEW_Invalidate(infoPtr, parent);
1582 return TRUE;
1586 /* Get/Set Messages *********************************************************/
1587 static LRESULT
1588 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1590 if(wParam)
1591 infoPtr->bRedraw = TRUE;
1592 else
1593 infoPtr->bRedraw = FALSE;
1595 return 0;
1598 static LRESULT
1599 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1601 TRACE("\n");
1602 return infoPtr->uIndent;
1605 static LRESULT
1606 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1608 TRACE("\n");
1610 if (newIndent < MINIMUM_INDENT)
1611 newIndent = MINIMUM_INDENT;
1613 if (infoPtr->uIndent != newIndent)
1615 infoPtr->uIndent = newIndent;
1616 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1617 TREEVIEW_UpdateScrollBars(infoPtr);
1618 TREEVIEW_Invalidate(infoPtr, NULL);
1621 return 0;
1625 static LRESULT
1626 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1628 TRACE("\n");
1629 return (LRESULT)infoPtr->hwndToolTip;
1632 static LRESULT
1633 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1635 HWND prevToolTip;
1637 TRACE("\n");
1638 prevToolTip = infoPtr->hwndToolTip;
1639 infoPtr->hwndToolTip = hwndTT;
1641 return (LRESULT)prevToolTip;
1644 static LRESULT
1645 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1647 BOOL rc = infoPtr->bNtfUnicode;
1648 infoPtr->bNtfUnicode = fUnicode;
1649 return rc;
1652 static LRESULT
1653 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1655 return infoPtr->bNtfUnicode;
1658 static LRESULT
1659 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1661 return infoPtr->uScrollTime;
1664 static LRESULT
1665 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1667 UINT uOldScrollTime = infoPtr->uScrollTime;
1669 infoPtr->uScrollTime = min(uScrollTime, 100);
1671 return uOldScrollTime;
1675 static LRESULT
1676 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1678 TRACE("\n");
1680 switch (wParam)
1682 case (WPARAM)TVSIL_NORMAL:
1683 return (LRESULT)infoPtr->himlNormal;
1685 case (WPARAM)TVSIL_STATE:
1686 return (LRESULT)infoPtr->himlState;
1688 default:
1689 return 0;
1693 #define TVHEIGHT_MIN 16
1694 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1696 /* Compute the natural height for items. */
1697 static UINT
1698 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1700 TEXTMETRICW tm;
1701 HDC hdc = GetDC(0);
1702 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1703 UINT height;
1705 /* Height is the maximum of:
1706 * 16 (a hack because our fonts are tiny), and
1707 * The text height + border & margin, and
1708 * The size of the normal image list
1710 GetTextMetricsW(hdc, &tm);
1711 SelectObject(hdc, hOldFont);
1712 ReleaseDC(0, hdc);
1714 height = TVHEIGHT_MIN;
1715 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1716 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1717 if (height < infoPtr->normalImageHeight)
1718 height = infoPtr->normalImageHeight;
1720 /* Round down, unless we support odd ("non even") heights. */
1721 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1722 height &= ~1;
1724 return height;
1727 static LRESULT
1728 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1730 HIMAGELIST himlOld = 0;
1731 int oldWidth = infoPtr->normalImageWidth;
1732 int oldHeight = infoPtr->normalImageHeight;
1735 TRACE("%lx,%p\n", wParam, himlNew);
1737 switch (wParam)
1739 case (WPARAM)TVSIL_NORMAL:
1740 himlOld = infoPtr->himlNormal;
1741 infoPtr->himlNormal = himlNew;
1743 if (himlNew != NULL)
1744 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1745 &infoPtr->normalImageHeight);
1746 else
1748 infoPtr->normalImageWidth = 0;
1749 infoPtr->normalImageHeight = 0;
1752 break;
1754 case (WPARAM)TVSIL_STATE:
1755 himlOld = infoPtr->himlState;
1756 infoPtr->himlState = himlNew;
1758 if (himlNew != NULL)
1759 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1760 &infoPtr->stateImageHeight);
1761 else
1763 infoPtr->stateImageWidth = 0;
1764 infoPtr->stateImageHeight = 0;
1767 break;
1770 if (oldWidth != infoPtr->normalImageWidth ||
1771 oldHeight != infoPtr->normalImageHeight)
1773 BOOL bRecalcVisible = FALSE;
1775 if (oldHeight != infoPtr->normalImageHeight &&
1776 !infoPtr->bHeightSet)
1778 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1779 bRecalcVisible = TRUE;
1782 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1783 infoPtr->normalImageWidth != infoPtr->uIndent)
1785 infoPtr->uIndent = infoPtr->normalImageWidth;
1786 bRecalcVisible = TRUE;
1789 if (bRecalcVisible)
1790 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1792 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1793 TREEVIEW_UpdateScrollBars(infoPtr);
1796 TREEVIEW_Invalidate(infoPtr, NULL);
1798 return (LRESULT)himlOld;
1801 static LRESULT
1802 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1804 INT prevHeight = infoPtr->uItemHeight;
1806 TRACE("%d\n", newHeight);
1807 if (newHeight == -1)
1809 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1810 infoPtr->bHeightSet = FALSE;
1812 else
1814 infoPtr->uItemHeight = newHeight;
1815 infoPtr->bHeightSet = TRUE;
1818 /* Round down, unless we support odd ("non even") heights. */
1819 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1820 infoPtr->uItemHeight &= ~1;
1822 if (infoPtr->uItemHeight != prevHeight)
1824 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1825 TREEVIEW_UpdateScrollBars(infoPtr);
1826 TREEVIEW_Invalidate(infoPtr, NULL);
1829 return prevHeight;
1832 static LRESULT
1833 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1835 TRACE("\n");
1836 return infoPtr->uItemHeight;
1840 static LRESULT
1841 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1843 TRACE("%p\n", infoPtr->hFont);
1844 return (LRESULT)infoPtr->hFont;
1848 static INT CALLBACK
1849 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1851 (void)unused;
1853 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1855 return 1;
1858 static LRESULT
1859 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1861 UINT uHeight = infoPtr->uItemHeight;
1863 TRACE("%p %i\n", hFont, bRedraw);
1865 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1867 DeleteObject(infoPtr->hBoldFont);
1868 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1869 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1871 if (!infoPtr->bHeightSet)
1872 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1874 if (uHeight != infoPtr->uItemHeight)
1875 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1877 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1879 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1880 TREEVIEW_UpdateScrollBars(infoPtr);
1882 if (bRedraw)
1883 TREEVIEW_Invalidate(infoPtr, NULL);
1885 return 0;
1889 static LRESULT
1890 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1892 TRACE("\n");
1893 return (LRESULT)infoPtr->clrLine;
1896 static LRESULT
1897 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1899 COLORREF prevColor = infoPtr->clrLine;
1901 TRACE("\n");
1902 infoPtr->clrLine = color;
1903 return (LRESULT)prevColor;
1907 static LRESULT
1908 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1910 TRACE("\n");
1911 return (LRESULT)infoPtr->clrText;
1914 static LRESULT
1915 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1917 COLORREF prevColor = infoPtr->clrText;
1919 TRACE("\n");
1920 infoPtr->clrText = color;
1922 if (infoPtr->clrText != prevColor)
1923 TREEVIEW_Invalidate(infoPtr, NULL);
1925 return (LRESULT)prevColor;
1929 static LRESULT
1930 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1932 TRACE("\n");
1933 return (LRESULT)infoPtr->clrBk;
1936 static LRESULT
1937 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1939 COLORREF prevColor = infoPtr->clrBk;
1941 TRACE("\n");
1942 infoPtr->clrBk = newColor;
1944 if (newColor != prevColor)
1945 TREEVIEW_Invalidate(infoPtr, NULL);
1947 return (LRESULT)prevColor;
1951 static LRESULT
1952 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1954 TRACE("\n");
1955 return (LRESULT)infoPtr->clrInsertMark;
1958 static LRESULT
1959 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1961 COLORREF prevColor = infoPtr->clrInsertMark;
1963 TRACE("%x\n", color);
1964 infoPtr->clrInsertMark = color;
1966 return (LRESULT)prevColor;
1970 static LRESULT
1971 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1973 TRACE("%d %p\n", wParam, item);
1975 if (!TREEVIEW_ValidItem(infoPtr, item))
1976 return 0;
1978 infoPtr->insertBeforeorAfter = wParam;
1979 infoPtr->insertMarkItem = item;
1981 TREEVIEW_Invalidate(infoPtr, NULL);
1983 return 1;
1987 /************************************************************************
1988 * Some serious braindamage here. lParam is a pointer to both the
1989 * input HTREEITEM and the output RECT.
1991 static LRESULT
1992 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1994 TREEVIEW_ITEM *wineItem;
1995 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1997 TRACE("\n");
1999 * validate parameters
2001 if (pItem == NULL)
2002 return FALSE;
2004 wineItem = *pItem;
2005 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2006 return FALSE;
2009 * If wParam is TRUE return the text size otherwise return
2010 * the whole item size
2012 if (fTextRect)
2014 /* Windows does not send TVN_GETDISPINFO here. */
2016 lpRect->top = wineItem->rect.top;
2017 lpRect->bottom = wineItem->rect.bottom;
2019 lpRect->left = wineItem->textOffset;
2020 if (!wineItem->textWidth)
2021 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2023 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2025 else
2027 *lpRect = wineItem->rect;
2030 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2032 return TRUE;
2035 static inline LRESULT
2036 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2038 /* Surprise! This does not take integral height into account. */
2039 return infoPtr->clientHeight / infoPtr->uItemHeight;
2043 static LRESULT
2044 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2046 TREEVIEW_ITEM *wineItem;
2048 wineItem = tvItem->hItem;
2049 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2050 return FALSE;
2052 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2054 if (tvItem->mask & TVIF_CHILDREN)
2056 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2057 FIXME("I_CHILDRENCALLBACK not supported\n");
2058 tvItem->cChildren = wineItem->cChildren;
2061 if (tvItem->mask & TVIF_HANDLE)
2062 tvItem->hItem = wineItem;
2064 if (tvItem->mask & TVIF_IMAGE)
2065 tvItem->iImage = wineItem->iImage;
2067 if (tvItem->mask & TVIF_INTEGRAL)
2068 tvItem->iIntegral = wineItem->iIntegral;
2070 /* undocumented: windows ignores TVIF_PARAM and
2071 * * always sets lParam
2073 tvItem->lParam = wineItem->lParam;
2075 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2076 tvItem->iSelectedImage = wineItem->iSelectedImage;
2078 if (tvItem->mask & TVIF_STATE)
2079 /* Careful here - Windows ignores the stateMask when you get the state
2080 That contradicts the documentation, but makes more common sense, masking
2081 retrieval in this way seems overkill */
2082 tvItem->state = wineItem->state;
2084 if (tvItem->mask & TVIF_TEXT)
2086 if (isW)
2088 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2090 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2091 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2093 else
2095 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2098 else
2100 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2102 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2103 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2105 else
2107 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2108 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2112 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2113 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2115 return TRUE;
2118 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2119 * which is wrong. */
2120 static LRESULT
2121 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2123 TREEVIEW_ITEM *wineItem;
2124 TREEVIEW_ITEM originalItem;
2126 wineItem = tvItem->hItem;
2128 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2129 tvItem->mask);
2131 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2132 return FALSE;
2134 /* store the original item values */
2135 originalItem = *wineItem;
2137 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2138 return FALSE;
2140 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2141 if ((tvItem->mask & TVIF_TEXT
2142 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2143 && ISVISIBLE(wineItem))
2145 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2146 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2149 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2151 /* The refresh updates everything, but we can't wait until then. */
2152 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2154 /* if any of the item's values changed and it's not a callback, redraw the item */
2155 if (item_changed(&originalItem, wineItem, tvItem))
2157 if (tvItem->mask & TVIF_INTEGRAL)
2159 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2160 TREEVIEW_UpdateScrollBars(infoPtr);
2162 TREEVIEW_Invalidate(infoPtr, NULL);
2164 else
2166 TREEVIEW_UpdateScrollBars(infoPtr);
2167 TREEVIEW_Invalidate(infoPtr, wineItem);
2172 return TRUE;
2175 static LRESULT
2176 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2178 TRACE("\n");
2180 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2181 return 0;
2183 return (wineItem->state & mask);
2186 static LRESULT
2187 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2189 TREEVIEW_ITEM *retval;
2191 retval = 0;
2193 /* handle all the global data here */
2194 switch (which)
2196 case TVGN_CHILD: /* Special case: child of 0 is root */
2197 if (wineItem)
2198 break;
2199 /* fall through */
2200 case TVGN_ROOT:
2201 retval = infoPtr->root->firstChild;
2202 break;
2204 case TVGN_CARET:
2205 retval = infoPtr->selectedItem;
2206 break;
2208 case TVGN_FIRSTVISIBLE:
2209 retval = infoPtr->firstVisible;
2210 break;
2212 case TVGN_DROPHILITE:
2213 retval = infoPtr->dropItem;
2214 break;
2216 case TVGN_LASTVISIBLE:
2217 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2218 break;
2221 if (retval)
2223 TRACE("flags:%x, returns %p\n", which, retval);
2224 return (LRESULT)retval;
2227 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2229 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2230 return FALSE;
2232 switch (which)
2234 case TVGN_NEXT:
2235 retval = wineItem->nextSibling;
2236 break;
2237 case TVGN_PREVIOUS:
2238 retval = wineItem->prevSibling;
2239 break;
2240 case TVGN_PARENT:
2241 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2242 break;
2243 case TVGN_CHILD:
2244 retval = wineItem->firstChild;
2245 break;
2246 case TVGN_NEXTVISIBLE:
2247 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2248 break;
2249 case TVGN_PREVIOUSVISIBLE:
2250 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2251 break;
2252 default:
2253 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2254 break;
2257 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2258 return (LRESULT)retval;
2262 static LRESULT
2263 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2265 TRACE(" %d\n", infoPtr->uNumItems);
2266 return (LRESULT)infoPtr->uNumItems;
2269 static VOID
2270 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2272 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2274 static const unsigned int state_table[] = { 0, 2, 1 };
2276 unsigned int state;
2278 state = STATEIMAGEINDEX(item->state);
2279 TRACE("state:%x\n", state);
2280 item->state &= ~TVIS_STATEIMAGEMASK;
2282 if (state < 3)
2283 state = state_table[state];
2285 item->state |= INDEXTOSTATEIMAGEMASK(state);
2287 TRACE("state:%x\n", state);
2288 TREEVIEW_Invalidate(infoPtr, item);
2293 /* Painting *************************************************************/
2295 /* Draw the lines and expand button for an item. Also draws one section
2296 * of the line from item's parent to item's parent's next sibling. */
2297 static void
2298 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2300 LONG centerx, centery;
2301 BOOL lar = ((infoPtr->dwStyle
2302 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2303 > TVS_LINESATROOT);
2304 HBRUSH hbr, hbrOld;
2306 if (!lar && item->iLevel == 0)
2307 return;
2309 hbr = CreateSolidBrush(infoPtr->clrBk);
2310 hbrOld = SelectObject(hdc, hbr);
2312 centerx = (item->linesOffset + item->stateOffset) / 2;
2313 centery = (item->rect.top + item->rect.bottom) / 2;
2315 if (infoPtr->dwStyle & TVS_HASLINES)
2317 HPEN hOldPen, hNewPen;
2318 HTREEITEM parent;
2319 LOGBRUSH lb;
2321 /* Get a dotted grey pen */
2322 lb.lbStyle = BS_SOLID;
2323 lb.lbColor = infoPtr->clrLine;
2324 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2325 hOldPen = SelectObject(hdc, hNewPen);
2327 /* Make sure the center is on a dot (using +2 instead
2328 * of +1 gives us pixel-by-pixel compat with native) */
2329 centery = (centery + 2) & ~1;
2331 MoveToEx(hdc, item->stateOffset, centery, NULL);
2332 LineTo(hdc, centerx - 1, centery);
2334 if (item->prevSibling || item->parent != infoPtr->root)
2336 MoveToEx(hdc, centerx, item->rect.top, NULL);
2337 LineTo(hdc, centerx, centery);
2340 if (item->nextSibling)
2342 MoveToEx(hdc, centerx, centery, NULL);
2343 LineTo(hdc, centerx, item->rect.bottom + 1);
2346 /* Draw the line from our parent to its next sibling. */
2347 parent = item->parent;
2348 while (parent != infoPtr->root)
2350 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2352 if (parent->nextSibling
2353 /* skip top-levels unless TVS_LINESATROOT */
2354 && parent->stateOffset > parent->linesOffset)
2356 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2357 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2360 parent = parent->parent;
2363 SelectObject(hdc, hOldPen);
2364 DeleteObject(hNewPen);
2368 * Display the (+/-) signs
2371 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2373 if (item->cChildren)
2375 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2376 if (theme)
2378 RECT glyphRect = item->rect;
2379 glyphRect.left = item->linesOffset;
2380 glyphRect.right = item->stateOffset;
2381 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2382 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2383 &glyphRect, NULL);
2385 else
2387 LONG height = item->rect.bottom - item->rect.top;
2388 LONG width = item->stateOffset - item->linesOffset;
2389 LONG rectsize = min(height, width) / 4;
2390 /* plussize = ceil(rectsize * 3/4) */
2391 LONG plussize = (rectsize + 1) * 3 / 4;
2393 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2394 HPEN hOldPen = SelectObject(hdc, hNewPen);
2396 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2397 centerx + rectsize + 2, centery + rectsize + 2);
2399 SelectObject(hdc, hOldPen);
2400 DeleteObject(hNewPen);
2402 if (height < 18 || width < 18)
2404 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2405 LineTo(hdc, centerx + plussize, centery);
2407 if (!(item->state & TVIS_EXPANDED))
2409 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2410 LineTo(hdc, centerx, centery + plussize);
2413 else
2415 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2416 centerx + plussize, centery + 2);
2418 if (!(item->state & TVIS_EXPANDED))
2420 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2421 centerx + 2, centery + plussize);
2422 SetPixel(hdc, centerx - 1, centery, infoPtr->clrBk);
2423 SetPixel(hdc, centerx + 1, centery, infoPtr->clrBk);
2429 SelectObject(hdc, hbrOld);
2430 DeleteObject(hbr);
2433 static void
2434 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2436 INT cditem;
2437 HFONT hOldFont;
2438 COLORREF oldTextColor, oldTextBkColor;
2439 int centery;
2440 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2441 NMTVCUSTOMDRAW nmcdhdr;
2443 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2445 /* - If item is drop target or it is selected and window is in focus -
2446 * use blue background (COLOR_HIGHLIGHT).
2447 * - If item is selected, window is not in focus, but it has style
2448 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2449 * - Otherwise - use background color
2451 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2452 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2453 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2455 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2457 nmcdhdr.clrTextBk = GetSysColor(COLOR_HIGHLIGHT);
2458 nmcdhdr.clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
2460 else
2462 nmcdhdr.clrTextBk = GetSysColor(COLOR_BTNFACE);
2463 if (infoPtr->clrText == -1)
2464 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2465 else
2466 nmcdhdr.clrText = infoPtr->clrText;
2469 else
2471 nmcdhdr.clrTextBk = infoPtr->clrBk;
2472 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2473 nmcdhdr.clrText = comctl32_color.clrHighlight;
2474 else if (infoPtr->clrText == -1)
2475 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2476 else
2477 nmcdhdr.clrText = infoPtr->clrText;
2480 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2482 /* The custom draw handler can query the text rectangle,
2483 * so get ready. */
2484 /* should already be known, set to 0 when changed */
2485 if (!wineItem->textWidth)
2486 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2488 cditem = 0;
2490 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2492 cditem = TREEVIEW_SendCustomDrawItemNotify
2493 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2494 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2496 if (cditem & CDRF_SKIPDEFAULT)
2498 SelectObject(hdc, hOldFont);
2499 return;
2503 if (cditem & CDRF_NEWFONT)
2504 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2506 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2508 /* Set colors. Custom draw handler can change these so we do this after it. */
2509 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2510 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2512 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2515 * Display the images associated with this item
2518 INT imageIndex;
2520 /* State images are displayed to the left of the Normal image
2521 * image number is in state; zero should be `display no image'.
2523 imageIndex = STATEIMAGEINDEX(wineItem->state);
2525 if (infoPtr->himlState && imageIndex)
2527 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2528 wineItem->stateOffset,
2529 centery - infoPtr->stateImageHeight / 2,
2530 ILD_NORMAL);
2533 /* Now, draw the normal image; can be either selected or
2534 * non-selected image.
2537 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2539 /* The item is currently selected */
2540 imageIndex = wineItem->iSelectedImage;
2542 else
2544 /* The item is not selected */
2545 imageIndex = wineItem->iImage;
2548 if (infoPtr->himlNormal)
2550 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2552 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2553 wineItem->imageOffset,
2554 centery - infoPtr->normalImageHeight / 2,
2555 ILD_NORMAL | ovlIdx);
2561 * Display the text associated with this item
2564 /* Don't paint item's text if it's being edited */
2565 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2567 if (wineItem->pszText)
2569 RECT rcText;
2571 rcText.top = wineItem->rect.top;
2572 rcText.bottom = wineItem->rect.bottom;
2573 rcText.left = wineItem->textOffset;
2574 rcText.right = rcText.left + wineItem->textWidth + 4;
2576 TRACE("drawing text %s at (%s)\n",
2577 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2579 /* Draw it */
2580 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2581 ETO_CLIPPED | ETO_OPAQUE,
2582 &rcText,
2583 wineItem->pszText,
2584 lstrlenW(wineItem->pszText),
2585 NULL);
2587 /* Draw the box around the selected item */
2588 if ((wineItem == infoPtr->selectedItem) && inFocus)
2590 DrawFocusRect(hdc,&rcText);
2596 /* Draw insertion mark if necessary */
2598 if (infoPtr->insertMarkItem)
2599 TRACE("item:%d,mark:%p\n",
2600 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2601 infoPtr->insertMarkItem);
2603 if (wineItem == infoPtr->insertMarkItem)
2605 HPEN hNewPen, hOldPen;
2606 int offset;
2607 int left, right;
2609 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2610 hOldPen = SelectObject(hdc, hNewPen);
2612 if (infoPtr->insertBeforeorAfter)
2613 offset = wineItem->rect.bottom - 1;
2614 else
2615 offset = wineItem->rect.top + 1;
2617 left = wineItem->textOffset - 2;
2618 right = wineItem->textOffset + wineItem->textWidth + 2;
2620 MoveToEx(hdc, left, offset - 3, NULL);
2621 LineTo(hdc, left, offset + 4);
2623 MoveToEx(hdc, left, offset, NULL);
2624 LineTo(hdc, right + 1, offset);
2626 MoveToEx(hdc, right, offset + 3, NULL);
2627 LineTo(hdc, right, offset - 4);
2629 SelectObject(hdc, hOldPen);
2630 DeleteObject(hNewPen);
2633 if (cditem & CDRF_NOTIFYPOSTPAINT)
2635 cditem = TREEVIEW_SendCustomDrawItemNotify
2636 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2637 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2640 /* Restore the hdc state */
2641 SetTextColor(hdc, oldTextColor);
2642 SetBkColor(hdc, oldTextBkColor);
2643 SelectObject(hdc, hOldFont);
2646 /* Computes treeHeight and treeWidth and updates the scroll bars.
2648 static void
2649 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2651 TREEVIEW_ITEM *wineItem;
2652 HWND hwnd = infoPtr->hwnd;
2653 BOOL vert = FALSE;
2654 BOOL horz = FALSE;
2655 SCROLLINFO si;
2656 LONG scrollX = infoPtr->scrollX;
2658 infoPtr->treeWidth = 0;
2659 infoPtr->treeHeight = 0;
2661 /* We iterate through all visible items in order to get the tree height
2662 * and width */
2663 wineItem = infoPtr->root->firstChild;
2665 while (wineItem != NULL)
2667 if (ISVISIBLE(wineItem))
2669 /* actually we draw text at textOffset + 2 */
2670 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2671 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2673 /* This is scroll-adjusted, but we fix this below. */
2674 infoPtr->treeHeight = wineItem->rect.bottom;
2677 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2680 /* Fix the scroll adjusted treeHeight and treeWidth. */
2681 if (infoPtr->root->firstChild)
2682 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2684 infoPtr->treeWidth += infoPtr->scrollX;
2686 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2688 /* Adding one scroll bar may take up enough space that it forces us
2689 * to add the other as well. */
2690 if (infoPtr->treeHeight > infoPtr->clientHeight)
2692 vert = TRUE;
2694 if (infoPtr->treeWidth
2695 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2696 horz = TRUE;
2698 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2699 horz = TRUE;
2701 if (!vert && horz && infoPtr->treeHeight
2702 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2703 vert = TRUE;
2705 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2707 si.cbSize = sizeof(SCROLLINFO);
2708 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2709 si.nMin = 0;
2711 if (vert)
2713 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2714 if ( si.nPage && NULL != infoPtr->firstVisible)
2716 si.nPos = infoPtr->firstVisible->visibleOrder;
2717 si.nMax = infoPtr->maxVisibleOrder - 1;
2719 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2721 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2722 ShowScrollBar(hwnd, SB_VERT, TRUE);
2723 infoPtr->uInternalStatus |= TV_VSCROLL;
2725 else
2727 if (infoPtr->uInternalStatus & TV_VSCROLL)
2728 ShowScrollBar(hwnd, SB_VERT, FALSE);
2729 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2732 else
2734 if (infoPtr->uInternalStatus & TV_VSCROLL)
2735 ShowScrollBar(hwnd, SB_VERT, FALSE);
2736 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2739 if (horz)
2741 si.nPage = infoPtr->clientWidth;
2742 si.nPos = infoPtr->scrollX;
2743 si.nMax = infoPtr->treeWidth - 1;
2745 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2747 si.nPos = si.nMax - max( si.nPage-1, 0 );
2748 scrollX = si.nPos;
2751 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2752 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2753 infoPtr->uInternalStatus |= TV_HSCROLL;
2755 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2756 TREEVIEW_HScroll(infoPtr,
2757 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2759 else
2761 if (infoPtr->uInternalStatus & TV_HSCROLL)
2762 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2763 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2765 scrollX = 0;
2766 if (infoPtr->scrollX != 0)
2768 TREEVIEW_HScroll(infoPtr,
2769 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2773 if (!horz)
2774 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2777 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2778 static LRESULT
2779 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hDC)
2781 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2782 RECT rect;
2784 GetClientRect(infoPtr->hwnd, &rect);
2785 FillRect(hDC, &rect, hBrush);
2786 DeleteObject(hBrush);
2788 return 1;
2791 static void
2792 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2794 HWND hwnd = infoPtr->hwnd;
2795 RECT rect = *rc;
2796 TREEVIEW_ITEM *wineItem;
2798 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2800 TRACE("empty window\n");
2801 return;
2804 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2805 hdc, rect);
2807 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2809 ReleaseDC(hwnd, hdc);
2810 return;
2813 for (wineItem = infoPtr->root->firstChild;
2814 wineItem != NULL;
2815 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2817 if (ISVISIBLE(wineItem))
2819 /* Avoid unneeded calculations */
2820 if (wineItem->rect.top > rect.bottom)
2821 break;
2822 if (wineItem->rect.bottom < rect.top)
2823 continue;
2825 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2829 TREEVIEW_UpdateScrollBars(infoPtr);
2831 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2832 infoPtr->cdmode =
2833 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2836 static void
2837 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2839 if (item != NULL)
2840 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2841 else
2842 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2845 static LRESULT
2846 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2848 HDC hdc;
2849 PAINTSTRUCT ps;
2850 RECT rc;
2852 TRACE("\n");
2854 if (wParam)
2856 hdc = (HDC)wParam;
2857 GetClientRect(infoPtr->hwnd, &rc);
2858 TREEVIEW_EraseBackground(infoPtr, hdc);
2860 else
2862 hdc = BeginPaint(infoPtr->hwnd, &ps);
2863 rc = ps.rcPaint;
2866 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2867 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2869 if (!wParam)
2870 EndPaint(infoPtr->hwnd, &ps);
2872 return 0;
2876 /* Sorting **************************************************************/
2878 /***************************************************************************
2879 * Forward the DPA local callback to the treeview owner callback
2881 static INT WINAPI
2882 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2883 const TVSORTCB *pCallBackSort)
2885 /* Forward the call to the client-defined callback */
2886 return pCallBackSort->lpfnCompare(first->lParam,
2887 second->lParam,
2888 pCallBackSort->lParam);
2891 /***************************************************************************
2892 * Treeview native sort routine: sort on item text.
2894 static INT WINAPI
2895 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2896 const TREEVIEW_INFO *infoPtr)
2898 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2899 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2901 if(first->pszText && second->pszText)
2902 return lstrcmpiW(first->pszText, second->pszText);
2903 else if(first->pszText)
2904 return -1;
2905 else if(second->pszText)
2906 return 1;
2907 else
2908 return 0;
2911 /* Returns the number of physical children belonging to item. */
2912 static INT
2913 TREEVIEW_CountChildren(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2915 INT cChildren = 0;
2916 HTREEITEM hti;
2918 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2919 cChildren++;
2921 return cChildren;
2924 /* Returns a DPA containing a pointer to each physical child of item in
2925 * sibling order. If item has no children, an empty DPA is returned. */
2926 static HDPA
2927 TREEVIEW_BuildChildDPA(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2929 HTREEITEM child = item->firstChild;
2931 HDPA list = DPA_Create(8);
2932 if (list == 0) return NULL;
2934 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2936 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2938 DPA_Destroy(list);
2939 return NULL;
2943 return list;
2946 /***************************************************************************
2947 * Setup the treeview structure with regards of the sort method
2948 * and sort the children of the TV item specified in lParam
2949 * fRecurse: currently unused. Should be zero.
2950 * parent: if pSort!=NULL, should equal pSort->hParent.
2951 * otherwise, item which child items are to be sorted.
2952 * pSort: sort method info. if NULL, sort on item text.
2953 * if non-NULL, sort on item's lParam content, and let the
2954 * application decide what that means. See also TVM_SORTCHILDRENCB.
2957 static LRESULT
2958 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2959 LPTVSORTCB pSort)
2961 INT cChildren;
2962 PFNDPACOMPARE pfnCompare;
2963 LPARAM lpCompare;
2965 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2966 if (parent == TVI_ROOT || parent == NULL)
2967 parent = infoPtr->root;
2969 /* Check for a valid handle to the parent item */
2970 if (!TREEVIEW_ValidItem(infoPtr, parent))
2972 ERR("invalid item hParent=%p\n", parent);
2973 return FALSE;
2976 if (pSort)
2978 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2979 lpCompare = (LPARAM)pSort;
2981 else
2983 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2984 lpCompare = (LPARAM)infoPtr;
2987 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2989 /* Make sure there is something to sort */
2990 if (cChildren > 1)
2992 /* TREEVIEW_ITEM rechaining */
2993 INT count = 0;
2994 HTREEITEM item = 0;
2995 HTREEITEM nextItem = 0;
2996 HTREEITEM prevItem = 0;
2998 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
3000 if (sortList == NULL)
3001 return FALSE;
3003 /* let DPA sort the list */
3004 DPA_Sort(sortList, pfnCompare, lpCompare);
3006 /* The order of DPA entries has been changed, so fixup the
3007 * nextSibling and prevSibling pointers. */
3009 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
3010 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
3012 /* link the two current item together */
3013 item->nextSibling = nextItem;
3014 nextItem->prevSibling = item;
3016 if (prevItem == NULL)
3018 /* this is the first item, update the parent */
3019 parent->firstChild = item;
3020 item->prevSibling = NULL;
3022 else
3024 /* fix the back chaining */
3025 item->prevSibling = prevItem;
3028 /* get ready for the next one */
3029 prevItem = item;
3030 item = nextItem;
3033 /* the last item is pointed to by item and never has a sibling */
3034 item->nextSibling = NULL;
3035 parent->lastChild = item;
3037 DPA_Destroy(sortList);
3039 TREEVIEW_VerifyTree(infoPtr);
3041 if (parent->state & TVIS_EXPANDED)
3043 int visOrder = infoPtr->firstVisible->visibleOrder;
3045 if (parent == infoPtr->root)
3046 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3047 else
3048 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3050 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3052 TREEVIEW_ITEM *item;
3054 for (item = infoPtr->root->firstChild; item != NULL;
3055 item = TREEVIEW_GetNextListItem(infoPtr, item))
3057 if (item->visibleOrder == visOrder)
3058 break;
3061 if (!item) item = parent->firstChild;
3062 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3065 TREEVIEW_Invalidate(infoPtr, NULL);
3068 return TRUE;
3070 return FALSE;
3074 /***************************************************************************
3075 * Setup the treeview structure with regards of the sort method
3076 * and sort the children of the TV item specified in lParam
3078 static LRESULT
3079 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3081 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3085 /***************************************************************************
3086 * Sort the children of the TV item specified in lParam.
3088 static LRESULT
3089 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3091 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3095 /* Expansion/Collapse ***************************************************/
3097 static BOOL
3098 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3099 UINT action)
3101 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3102 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3103 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3104 0, wineItem);
3107 static VOID
3108 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3109 UINT action)
3111 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3112 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3113 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3114 0, wineItem);
3118 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3119 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3120 static BOOL
3121 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3122 BOOL bRemoveChildren, BOOL bUser)
3124 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3125 BOOL bSetSelection, bSetFirstVisible;
3126 RECT scrollRect;
3127 LONG scrollDist = 0;
3128 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3130 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3132 if (!(wineItem->state & TVIS_EXPANDED))
3133 return FALSE;
3135 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3136 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3138 if (wineItem->firstChild == NULL)
3139 return FALSE;
3141 wineItem->state &= ~TVIS_EXPANDED;
3143 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3144 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3146 bSetSelection = (infoPtr->selectedItem != NULL
3147 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3149 bSetFirstVisible = (infoPtr->firstVisible != NULL
3150 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3152 tmpItem = wineItem;
3153 while (tmpItem)
3155 if (tmpItem->nextSibling)
3157 nextItem = tmpItem->nextSibling;
3158 break;
3160 tmpItem = tmpItem->parent;
3163 if (nextItem)
3164 scrollDist = nextItem->rect.top;
3166 if (bRemoveChildren)
3168 INT old_cChildren = wineItem->cChildren;
3169 TRACE("TVE_COLLAPSERESET\n");
3170 wineItem->state &= ~TVIS_EXPANDEDONCE;
3171 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3172 wineItem->cChildren = old_cChildren;
3175 if (wineItem->firstChild)
3177 TREEVIEW_ITEM *item, *sibling;
3179 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3181 for (item = wineItem->firstChild; item != sibling;
3182 item = TREEVIEW_GetNextListItem(infoPtr, item))
3184 item->visibleOrder = -1;
3188 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3190 if (nextItem)
3191 scrollDist = -(scrollDist - nextItem->rect.top);
3193 if (bSetSelection)
3195 /* Don't call DoSelectItem, it sends notifications. */
3196 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3197 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3198 wineItem->state |= TVIS_SELECTED;
3199 infoPtr->selectedItem = wineItem;
3202 TREEVIEW_UpdateScrollBars(infoPtr);
3204 scrollRect.left = 0;
3205 scrollRect.right = infoPtr->clientWidth;
3206 scrollRect.bottom = infoPtr->clientHeight;
3208 if (nextItem)
3210 scrollRect.top = nextItem->rect.top;
3212 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3213 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3214 TREEVIEW_Invalidate(infoPtr, wineItem);
3215 } else {
3216 scrollRect.top = wineItem->rect.top;
3217 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3220 TREEVIEW_SetFirstVisible(infoPtr,
3221 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3222 TRUE);
3224 return TRUE;
3227 static BOOL
3228 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3229 BOOL bExpandPartial, BOOL bUser)
3231 LONG scrollDist;
3232 LONG orgNextTop = 0;
3233 RECT scrollRect;
3234 TREEVIEW_ITEM *nextItem, *tmpItem;
3236 TRACE("\n");
3238 if (wineItem->state & TVIS_EXPANDED)
3239 return TRUE;
3241 tmpItem = wineItem; nextItem = NULL;
3242 while (tmpItem)
3244 if (tmpItem->nextSibling)
3246 nextItem = tmpItem->nextSibling;
3247 break;
3249 tmpItem = tmpItem->parent;
3252 if (nextItem)
3253 orgNextTop = nextItem->rect.top;
3255 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3257 if (bUser || ((wineItem->cChildren != 0) &&
3258 !(wineItem->state & TVIS_EXPANDEDONCE)))
3260 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3262 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3263 return FALSE;
3266 if (!wineItem->firstChild)
3267 return FALSE;
3269 wineItem->state |= TVIS_EXPANDED;
3270 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3271 wineItem->state |= TVIS_EXPANDEDONCE;
3273 else
3275 if (!wineItem->firstChild)
3276 return FALSE;
3278 /* this item has already been expanded */
3279 wineItem->state |= TVIS_EXPANDED;
3282 if (bExpandPartial)
3283 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3285 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3286 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3287 TREEVIEW_UpdateScrollBars(infoPtr);
3289 scrollRect.left = 0;
3290 scrollRect.bottom = infoPtr->treeHeight;
3291 scrollRect.right = infoPtr->clientWidth;
3292 if (nextItem)
3294 scrollDist = nextItem->rect.top - orgNextTop;
3295 scrollRect.top = orgNextTop;
3297 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3298 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3299 TREEVIEW_Invalidate (infoPtr, wineItem);
3300 } else {
3301 scrollRect.top = wineItem->rect.top;
3302 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3305 /* Scroll up so that as many children as possible are visible.
3306 * This fails when expanding causes an HScroll bar to appear, but we
3307 * don't know that yet, so the last item is obscured. */
3308 if (wineItem->firstChild != NULL)
3310 int nChildren = wineItem->lastChild->visibleOrder
3311 - wineItem->firstChild->visibleOrder + 1;
3313 int visible_pos = wineItem->visibleOrder
3314 - infoPtr->firstVisible->visibleOrder;
3316 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3318 if (visible_pos > 0 && nChildren > rows_below)
3320 int scroll = nChildren - rows_below;
3322 if (scroll > visible_pos)
3323 scroll = visible_pos;
3325 if (scroll > 0)
3327 TREEVIEW_ITEM *newFirstVisible
3328 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3329 scroll);
3332 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3337 return TRUE;
3340 static BOOL
3341 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3343 TRACE("\n");
3345 if (wineItem->state & TVIS_EXPANDED)
3346 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3347 else
3348 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3351 static VOID
3352 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3354 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3356 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3358 if (TREEVIEW_HasChildren(infoPtr, item))
3359 TREEVIEW_ExpandAll(infoPtr, item);
3363 /* Note:If the specified item is the child of a collapsed parent item,
3364 the parent's list of child items is (recursively) expanded to reveal the
3365 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3366 know if it also applies here.
3369 static LRESULT
3370 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3372 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3373 return 0;
3375 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3376 TREEVIEW_ItemName(wineItem), flag,
3377 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3379 switch (flag & TVE_TOGGLE)
3381 case TVE_COLLAPSE:
3382 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3383 FALSE);
3385 case TVE_EXPAND:
3386 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3387 FALSE);
3389 case TVE_TOGGLE:
3390 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3392 default:
3393 return 0;
3396 #if 0
3397 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3398 #endif
3401 /* Hit-Testing **********************************************************/
3403 static TREEVIEW_ITEM *
3404 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3406 TREEVIEW_ITEM *wineItem;
3407 LONG row;
3409 if (!infoPtr->firstVisible)
3410 return NULL;
3412 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3414 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3415 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3417 if (row >= wineItem->visibleOrder
3418 && row < wineItem->visibleOrder + wineItem->iIntegral)
3419 break;
3422 return wineItem;
3425 static LRESULT
3426 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3428 TREEVIEW_ITEM *wineItem;
3429 RECT rect;
3430 UINT status;
3431 LONG x, y;
3433 lpht->hItem = 0;
3434 GetClientRect(infoPtr->hwnd, &rect);
3435 status = 0;
3436 x = lpht->pt.x;
3437 y = lpht->pt.y;
3439 if (x < rect.left)
3441 status |= TVHT_TOLEFT;
3443 else if (x > rect.right)
3445 status |= TVHT_TORIGHT;
3448 if (y < rect.top)
3450 status |= TVHT_ABOVE;
3452 else if (y > rect.bottom)
3454 status |= TVHT_BELOW;
3457 if (status)
3459 lpht->flags = status;
3460 return (LRESULT)(HTREEITEM)NULL;
3463 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3464 if (!wineItem)
3466 lpht->flags = TVHT_NOWHERE;
3467 return (LRESULT)(HTREEITEM)NULL;
3470 if (x >= wineItem->textOffset + wineItem->textWidth)
3472 lpht->flags = TVHT_ONITEMRIGHT;
3474 else if (x >= wineItem->textOffset)
3476 lpht->flags = TVHT_ONITEMLABEL;
3478 else if (x >= wineItem->imageOffset)
3480 lpht->flags = TVHT_ONITEMICON;
3482 else if (x >= wineItem->stateOffset)
3484 lpht->flags = TVHT_ONITEMSTATEICON;
3486 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3488 lpht->flags = TVHT_ONITEMBUTTON;
3490 else
3492 lpht->flags = TVHT_ONITEMINDENT;
3495 lpht->hItem = wineItem;
3496 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3498 return (LRESULT)wineItem;
3501 /* Item Label Editing ***************************************************/
3503 static LRESULT
3504 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3506 return (LRESULT)infoPtr->hwndEdit;
3509 static LRESULT CALLBACK
3510 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3512 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3513 BOOL bCancel = FALSE;
3514 LRESULT rc;
3516 switch (uMsg)
3518 case WM_PAINT:
3519 TRACE("WM_PAINT start\n");
3520 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3521 lParam);
3522 TRACE("WM_PAINT done\n");
3523 return rc;
3525 case WM_KILLFOCUS:
3526 if (infoPtr->bIgnoreEditKillFocus)
3527 return TRUE;
3528 break;
3530 case WM_GETDLGCODE:
3531 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3533 case WM_KEYDOWN:
3534 if (wParam == (WPARAM)VK_ESCAPE)
3536 bCancel = TRUE;
3537 break;
3539 else if (wParam == (WPARAM)VK_RETURN)
3541 break;
3544 /* fall through */
3545 default:
3546 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3549 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3550 /* eg. Using a messagebox */
3552 infoPtr->bIgnoreEditKillFocus = TRUE;
3553 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3554 infoPtr->bIgnoreEditKillFocus = FALSE;
3556 return 0;
3560 /* should handle edit control messages here */
3562 static LRESULT
3563 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3565 TRACE("%lx %ld\n", wParam, lParam);
3567 switch (HIWORD(wParam))
3569 case EN_UPDATE:
3572 * Adjust the edit window size
3574 WCHAR buffer[1024];
3575 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3576 HDC hdc = GetDC(infoPtr->hwndEdit);
3577 SIZE sz;
3578 HFONT hFont, hOldFont = 0;
3580 infoPtr->bLabelChanged = TRUE;
3582 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3584 /* Select font to get the right dimension of the string */
3585 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3587 if (hFont != 0)
3589 hOldFont = SelectObject(hdc, hFont);
3592 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3594 TEXTMETRICW textMetric;
3596 /* Add Extra spacing for the next character */
3597 GetTextMetricsW(hdc, &textMetric);
3598 sz.cx += (textMetric.tmMaxCharWidth * 2);
3600 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3601 sz.cx = min(sz.cx,
3602 infoPtr->clientWidth - editItem->textOffset + 2);
3604 SetWindowPos(infoPtr->hwndEdit,
3605 HWND_TOP,
3608 sz.cx,
3609 editItem->rect.bottom - editItem->rect.top + 3,
3610 SWP_NOMOVE | SWP_DRAWFRAME);
3613 if (hFont != 0)
3615 SelectObject(hdc, hOldFont);
3618 ReleaseDC(infoPtr->hwnd, hdc);
3619 break;
3622 default:
3623 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3626 return 0;
3629 static HWND
3630 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3632 HWND hwnd = infoPtr->hwnd;
3633 HWND hwndEdit;
3634 SIZE sz;
3635 TREEVIEW_ITEM *editItem = hItem;
3636 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3637 HDC hdc;
3638 HFONT hOldFont=0;
3639 TEXTMETRICW textMetric;
3640 static const WCHAR EditW[] = {'E','d','i','t',0};
3642 TRACE("%p %p\n", hwnd, hItem);
3643 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3644 return NULL;
3646 if (infoPtr->hwndEdit)
3647 return infoPtr->hwndEdit;
3649 infoPtr->bLabelChanged = FALSE;
3651 /* Make sure that edit item is selected */
3652 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3653 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3655 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3657 hdc = GetDC(hwnd);
3658 /* Select the font to get appropriate metric dimensions */
3659 if (infoPtr->hFont != 0)
3661 hOldFont = SelectObject(hdc, infoPtr->hFont);
3664 /* Get string length in pixels */
3665 GetTextExtentPoint32W(hdc, editItem->pszText, strlenW(editItem->pszText),
3666 &sz);
3668 /* Add Extra spacing for the next character */
3669 GetTextMetricsW(hdc, &textMetric);
3670 sz.cx += (textMetric.tmMaxCharWidth * 2);
3672 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3673 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3675 if (infoPtr->hFont != 0)
3677 SelectObject(hdc, hOldFont);
3680 ReleaseDC(hwnd, hdc);
3681 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3682 EditW,
3684 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3685 WS_CLIPSIBLINGS | ES_WANTRETURN |
3686 ES_LEFT, editItem->textOffset - 2,
3687 editItem->rect.top - 1, sz.cx + 3,
3688 editItem->rect.bottom -
3689 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3690 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3692 infoPtr->hwndEdit = hwndEdit;
3694 /* Get a 2D border. */
3695 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3696 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3697 SetWindowLongW(hwndEdit, GWL_STYLE,
3698 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3700 SendMessageW(hwndEdit, WM_SETFONT,
3701 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3703 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3704 (DWORD_PTR)
3705 TREEVIEW_Edit_SubclassProc);
3707 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3709 DestroyWindow(hwndEdit);
3710 infoPtr->hwndEdit = 0;
3711 return NULL;
3714 infoPtr->selectedItem = hItem;
3715 SetWindowTextW(hwndEdit, editItem->pszText);
3716 SetFocus(hwndEdit);
3717 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3718 ShowWindow(hwndEdit, SW_SHOW);
3720 return hwndEdit;
3724 static LRESULT
3725 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3727 HWND hwnd = infoPtr->hwnd;
3728 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3729 NMTVDISPINFOW tvdi;
3730 BOOL bCommit;
3731 WCHAR tmpText[1024] = { '\0' };
3732 WCHAR *newText = tmpText;
3733 int iLength = 0;
3735 if (!infoPtr->hwndEdit)
3736 return FALSE;
3738 tvdi.hdr.hwndFrom = hwnd;
3739 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3740 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3741 tvdi.item.mask = 0;
3742 tvdi.item.hItem = editedItem;
3743 tvdi.item.state = editedItem->state;
3744 tvdi.item.lParam = editedItem->lParam;
3746 if (!bCancel)
3748 if (!infoPtr->bNtfUnicode)
3749 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3750 else
3751 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3753 if (iLength >= 1023)
3755 ERR("Insufficient space to retrieve new item label\n");
3758 tvdi.item.mask = TVIF_TEXT;
3759 tvdi.item.pszText = tmpText;
3760 tvdi.item.cchTextMax = iLength + 1;
3762 else
3764 tvdi.item.pszText = NULL;
3765 tvdi.item.cchTextMax = 0;
3768 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3770 if (!bCancel && bCommit) /* Apply the changes */
3772 if (!infoPtr->bNtfUnicode)
3774 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3775 newText = Alloc(len * sizeof(WCHAR));
3776 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3777 iLength = len - 1;
3780 if (strcmpW(newText, editedItem->pszText) != 0)
3782 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3783 if (ptr == NULL)
3785 ERR("OutOfMemory, cannot allocate space for label\n");
3786 if(newText != tmpText) Free(newText);
3787 DestroyWindow(infoPtr->hwndEdit);
3788 infoPtr->hwndEdit = 0;
3789 return FALSE;
3791 else
3793 editedItem->pszText = ptr;
3794 editedItem->cchTextMax = iLength + 1;
3795 strcpyW(editedItem->pszText, newText);
3796 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3799 if(newText != tmpText) Free(newText);
3802 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3803 DestroyWindow(infoPtr->hwndEdit);
3804 infoPtr->hwndEdit = 0;
3805 return TRUE;
3808 static LRESULT
3809 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3811 if (wParam != TV_EDIT_TIMER)
3813 ERR("got unknown timer\n");
3814 return 1;
3817 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3818 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3820 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3822 return 0;
3826 /* Mouse Tracking/Drag **************************************************/
3828 /***************************************************************************
3829 * This is quite unusual piece of code, but that's how it's implemented in
3830 * Windows.
3832 static LRESULT
3833 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3835 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3836 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3837 RECT r;
3838 MSG msg;
3840 r.top = pt.y - cyDrag;
3841 r.left = pt.x - cxDrag;
3842 r.bottom = pt.y + cyDrag;
3843 r.right = pt.x + cxDrag;
3845 SetCapture(infoPtr->hwnd);
3847 while (1)
3849 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3851 if (msg.message == WM_MOUSEMOVE)
3853 pt.x = (short)LOWORD(msg.lParam);
3854 pt.y = (short)HIWORD(msg.lParam);
3855 if (PtInRect(&r, pt))
3856 continue;
3857 else
3859 ReleaseCapture();
3860 return 1;
3863 else if (msg.message >= WM_LBUTTONDOWN &&
3864 msg.message <= WM_RBUTTONDBLCLK)
3866 if (msg.message == WM_RBUTTONUP)
3867 TREEVIEW_RButtonUp(infoPtr, &pt);
3868 break;
3871 DispatchMessageW(&msg);
3874 if (GetCapture() != infoPtr->hwnd)
3875 return 0;
3878 ReleaseCapture();
3879 return 0;
3883 static LRESULT
3884 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3886 TREEVIEW_ITEM *wineItem;
3887 TVHITTESTINFO hit;
3889 TRACE("\n");
3890 SetFocus(infoPtr->hwnd);
3892 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3894 /* If there is pending 'edit label' event - kill it now */
3895 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3898 hit.pt.x = (short)LOWORD(lParam);
3899 hit.pt.y = (short)HIWORD(lParam);
3901 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3902 if (!wineItem)
3903 return 0;
3904 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3906 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3907 { /* FIXME! */
3908 switch (hit.flags)
3910 case TVHT_ONITEMRIGHT:
3911 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3912 break;
3914 case TVHT_ONITEMINDENT:
3915 if (!(infoPtr->dwStyle & TVS_HASLINES))
3917 break;
3919 else
3921 int level = hit.pt.x / infoPtr->uIndent;
3922 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3924 while (wineItem->iLevel > level)
3926 wineItem = wineItem->parent;
3929 /* fall through */
3932 case TVHT_ONITEMLABEL:
3933 case TVHT_ONITEMICON:
3934 case TVHT_ONITEMBUTTON:
3935 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3936 break;
3938 case TVHT_ONITEMSTATEICON:
3939 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3940 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3941 else
3942 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3943 break;
3946 return TRUE;
3950 static LRESULT
3951 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3953 HWND hwnd = infoPtr->hwnd;
3954 TVHITTESTINFO ht;
3955 BOOL bTrack, bDoLabelEdit;
3956 HTREEITEM tempItem;
3958 /* If Edit control is active - kill it and return.
3959 * The best way to do it is to set focus to itself.
3960 * Edit control subclassed procedure will automatically call
3961 * EndEditLabelNow.
3963 if (infoPtr->hwndEdit)
3965 SetFocus(hwnd);
3966 return 0;
3969 ht.pt.x = (short)LOWORD(lParam);
3970 ht.pt.y = (short)HIWORD(lParam);
3972 TREEVIEW_HitTest(infoPtr, &ht);
3973 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3975 /* update focusedItem and redraw both items */
3976 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3978 infoPtr->focusedItem = ht.hItem;
3979 InvalidateRect(hwnd, &ht.hItem->rect, TRUE);
3981 if(infoPtr->selectedItem)
3982 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3985 bTrack = (ht.flags & TVHT_ONITEM)
3986 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3989 * If the style allows editing and the node is already selected
3990 * and the click occurred on the item label...
3992 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
3993 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
3995 /* Send NM_CLICK right away */
3996 if (!bTrack)
3997 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3998 goto setfocus;
4000 if (ht.flags & TVHT_ONITEMBUTTON)
4002 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4003 goto setfocus;
4005 else if (bTrack)
4006 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4007 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4009 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4010 infoPtr->dropItem = ht.hItem;
4012 /* clean up focusedItem as we dragged and won't select this item */
4013 if(infoPtr->focusedItem)
4015 /* refresh the item that was focused */
4016 tempItem = infoPtr->focusedItem;
4017 infoPtr->focusedItem = 0;
4018 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
4020 /* refresh the selected item to return the filled background */
4021 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
4024 return 0;
4028 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4029 goto setfocus;
4031 if (bDoLabelEdit)
4033 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4034 KillTimer(hwnd, TV_EDIT_TIMER);
4036 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4037 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4039 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4042 * if we are TVS_SINGLEEXPAND then we want this single click to
4043 * do a bunch of things.
4045 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4046 (infoPtr->hwndEdit == 0))
4048 TREEVIEW_ITEM *SelItem;
4051 * Send the notification
4053 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4056 * Close the previous selection all the way to the root
4057 * as long as the new selection is not a child
4059 if((infoPtr->selectedItem)
4060 && (infoPtr->selectedItem != ht.hItem))
4062 BOOL closeit = TRUE;
4063 SelItem = ht.hItem;
4065 /* determine if the hitItem is a child of the currently selected item */
4066 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4068 closeit = (SelItem != infoPtr->selectedItem);
4069 SelItem = SelItem->parent;
4072 if(closeit)
4074 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4075 SelItem = infoPtr->selectedItem;
4077 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4079 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4080 SelItem = SelItem->parent;
4086 * Expand the current item
4088 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4091 /* Select the current item */
4092 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4094 else if (ht.flags & TVHT_ONITEMSTATEICON)
4096 /* TVS_CHECKBOXES requires us to toggle the current state */
4097 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4098 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4101 setfocus:
4102 SetFocus(hwnd);
4103 return 0;
4107 static LRESULT
4108 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4110 TVHITTESTINFO ht;
4112 if (infoPtr->hwndEdit)
4114 SetFocus(infoPtr->hwnd);
4115 return 0;
4118 ht.pt.x = (short)LOWORD(lParam);
4119 ht.pt.y = (short)HIWORD(lParam);
4121 TREEVIEW_HitTest(infoPtr, &ht);
4123 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4125 if (ht.hItem)
4127 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4128 infoPtr->dropItem = ht.hItem;
4131 else
4133 SetFocus(infoPtr->hwnd);
4134 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4137 return 0;
4140 static LRESULT
4141 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4143 TVHITTESTINFO ht;
4145 ht.pt = *pPt;
4147 TREEVIEW_HitTest(infoPtr, &ht);
4149 if (ht.hItem)
4151 /* Change to screen coordinate for WM_CONTEXTMENU */
4152 ClientToScreen(infoPtr->hwnd, &ht.pt);
4154 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4155 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4156 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4158 return 0;
4162 static LRESULT
4163 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4165 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4166 INT cx, cy;
4167 HDC hdc, htopdc;
4168 HWND hwtop;
4169 HBITMAP hbmp, hOldbmp;
4170 SIZE size;
4171 RECT rc;
4172 HFONT hOldFont;
4174 TRACE("\n");
4176 if (!(infoPtr->himlNormal))
4177 return 0;
4179 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4180 return 0;
4182 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4184 hwtop = GetDesktopWindow();
4185 htopdc = GetDC(hwtop);
4186 hdc = CreateCompatibleDC(htopdc);
4188 hOldFont = SelectObject(hdc, infoPtr->hFont);
4189 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4190 &size);
4191 TRACE("%d %d %s %d\n", size.cx, size.cy, debugstr_w(dragItem->pszText),
4192 strlenW(dragItem->pszText));
4193 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4194 hOldbmp = SelectObject(hdc, hbmp);
4196 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4197 size.cx += cx;
4198 if (cy > size.cy)
4199 size.cy = cy;
4201 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4202 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4203 ILD_NORMAL);
4206 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4207 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4210 /* draw item text */
4212 SetRect(&rc, cx, 0, size.cx, size.cy);
4213 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4214 DT_LEFT);
4215 SelectObject(hdc, hOldFont);
4216 SelectObject(hdc, hOldbmp);
4218 ImageList_Add(infoPtr->dragList, hbmp, 0);
4220 DeleteDC(hdc);
4221 DeleteObject(hbmp);
4222 ReleaseDC(hwtop, htopdc);
4224 return (LRESULT)infoPtr->dragList;
4227 /* Selection ************************************************************/
4229 static LRESULT
4230 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4231 INT cause)
4233 TREEVIEW_ITEM *prevSelect;
4234 RECT rcFocused;
4236 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4238 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4239 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4240 newSelect ? newSelect->state : 0);
4242 /* reset and redraw focusedItem if focusedItem was set so we don't */
4243 /* have to worry about the previously focused item when we set a new one */
4244 if(infoPtr->focusedItem)
4246 rcFocused = (infoPtr->focusedItem)->rect;
4247 infoPtr->focusedItem = 0;
4248 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4251 switch (action)
4253 case TVGN_CARET:
4254 prevSelect = infoPtr->selectedItem;
4256 if (prevSelect == newSelect) {
4257 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4258 break;
4261 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4262 TVN_SELCHANGINGW,
4263 cause,
4264 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4265 prevSelect,
4266 newSelect))
4267 return FALSE;
4269 if (prevSelect)
4270 prevSelect->state &= ~TVIS_SELECTED;
4271 if (newSelect)
4272 newSelect->state |= TVIS_SELECTED;
4274 infoPtr->selectedItem = newSelect;
4276 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4278 if (prevSelect)
4279 TREEVIEW_Invalidate(infoPtr, prevSelect);
4280 if (newSelect)
4281 TREEVIEW_Invalidate(infoPtr, newSelect);
4283 TREEVIEW_SendTreeviewNotify(infoPtr,
4284 TVN_SELCHANGEDW,
4285 cause,
4286 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4287 prevSelect,
4288 newSelect);
4289 break;
4291 case TVGN_DROPHILITE:
4292 prevSelect = infoPtr->dropItem;
4294 if (prevSelect)
4295 prevSelect->state &= ~TVIS_DROPHILITED;
4297 infoPtr->dropItem = newSelect;
4299 if (newSelect)
4300 newSelect->state |= TVIS_DROPHILITED;
4302 TREEVIEW_Invalidate(infoPtr, prevSelect);
4303 TREEVIEW_Invalidate(infoPtr, newSelect);
4304 break;
4306 case TVGN_FIRSTVISIBLE:
4307 if (newSelect != NULL)
4309 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4310 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4311 TREEVIEW_Invalidate(infoPtr, NULL);
4313 break;
4316 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4317 return TRUE;
4320 /* FIXME: handle NM_KILLFOCUS etc */
4321 static LRESULT
4322 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4324 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4325 return FALSE;
4327 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4329 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4330 return FALSE;
4332 return TRUE;
4335 /*************************************************************************
4336 * TREEVIEW_ProcessLetterKeys
4338 * Processes keyboard messages generated by pressing the letter keys
4339 * on the keyboard.
4340 * What this does is perform a case insensitive search from the
4341 * current position with the following quirks:
4342 * - If two chars or more are pressed in quick succession we search
4343 * for the corresponding string (e.g. 'abc').
4344 * - If there is a delay we wipe away the current search string and
4345 * restart with just that char.
4346 * - If the user keeps pressing the same character, whether slowly or
4347 * fast, so that the search string is entirely composed of this
4348 * character ('aaaaa' for instance), then we search for first item
4349 * that starting with that character.
4350 * - If the user types the above character in quick succession, then
4351 * we must also search for the corresponding string ('aaaaa'), and
4352 * go to that string if there is a match.
4354 * RETURNS
4356 * Zero.
4358 * BUGS
4360 * - The current implementation has a list of characters it will
4361 * accept and it ignores everything else. In particular it will
4362 * ignore accentuated characters which seems to match what
4363 * Windows does. But I'm not sure it makes sense to follow
4364 * Windows there.
4365 * - We don't sound a beep when the search fails.
4366 * - The search should start from the focused item, not from the selected
4367 * item. One reason for this is to allow for multiple selections in trees.
4368 * But currently infoPtr->focusedItem does not seem very usable.
4370 * SEE ALSO
4372 * TREEVIEW_ProcessLetterKeys
4374 static INT TREEVIEW_ProcessLetterKeys(
4375 HWND hwnd, /* handle to the window */
4376 WPARAM charCode, /* the character code, the actual character */
4377 LPARAM keyData /* key data */
4380 TREEVIEW_INFO *infoPtr;
4381 HTREEITEM nItem;
4382 HTREEITEM endidx,idx;
4383 TVITEMEXW item;
4384 WCHAR buffer[MAX_PATH];
4385 DWORD timestamp,elapsed;
4387 /* simple parameter checking */
4388 if (!hwnd || !charCode || !keyData)
4389 return 0;
4391 infoPtr=(TREEVIEW_INFO*)GetWindowLongPtrW(hwnd, 0);
4392 if (!infoPtr)
4393 return 0;
4395 /* only allow the valid WM_CHARs through */
4396 if (!isalnum(charCode) &&
4397 charCode != '.' && charCode != '`' && charCode != '!' &&
4398 charCode != '@' && charCode != '#' && charCode != '$' &&
4399 charCode != '%' && charCode != '^' && charCode != '&' &&
4400 charCode != '*' && charCode != '(' && charCode != ')' &&
4401 charCode != '-' && charCode != '_' && charCode != '+' &&
4402 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4403 charCode != '}' && charCode != '[' && charCode != '{' &&
4404 charCode != '/' && charCode != '?' && charCode != '>' &&
4405 charCode != '<' && charCode != ',' && charCode != '~')
4406 return 0;
4408 /* compute how much time elapsed since last keypress */
4409 timestamp = GetTickCount();
4410 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4411 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4412 } else {
4413 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4416 /* update the search parameters */
4417 infoPtr->lastKeyPressTimestamp=timestamp;
4418 if (elapsed < KEY_DELAY) {
4419 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4420 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4422 if (infoPtr->charCode != charCode) {
4423 infoPtr->charCode=charCode=0;
4425 } else {
4426 infoPtr->charCode=charCode;
4427 infoPtr->szSearchParam[0]=charCode;
4428 infoPtr->nSearchParamLength=1;
4429 /* Redundant with the 1 char string */
4430 charCode=0;
4433 /* and search from the current position */
4434 nItem=NULL;
4435 if (infoPtr->selectedItem != NULL) {
4436 endidx=infoPtr->selectedItem;
4437 /* if looking for single character match,
4438 * then we must always move forward
4440 if (infoPtr->nSearchParamLength == 1)
4441 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4442 else
4443 idx=endidx;
4444 } else {
4445 endidx=NULL;
4446 idx=infoPtr->root->firstChild;
4448 do {
4449 /* At the end point, sort out wrapping */
4450 if (idx == NULL) {
4452 /* If endidx is null, stop at the last item (ie top to bottom) */
4453 if (endidx == NULL)
4454 break;
4456 /* Otherwise, start again at the very beginning */
4457 idx=infoPtr->root->firstChild;
4459 /* But if we are stopping on the first child, end now! */
4460 if (idx == endidx) break;
4463 /* get item */
4464 ZeroMemory(&item, sizeof(item));
4465 item.mask = TVIF_TEXT;
4466 item.hItem = idx;
4467 item.pszText = buffer;
4468 item.cchTextMax = sizeof(buffer);
4469 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4471 /* check for a match */
4472 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4473 nItem=idx;
4474 break;
4475 } else if ( (charCode != 0) && (nItem == NULL) &&
4476 (nItem != infoPtr->selectedItem) &&
4477 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4478 /* This would work but we must keep looking for a longer match */
4479 nItem=idx;
4481 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4482 } while (idx != endidx);
4484 if (nItem != NULL) {
4485 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4486 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4490 return 0;
4493 /* Scrolling ************************************************************/
4495 static LRESULT
4496 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4498 int viscount;
4499 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4500 HTREEITEM newFirstVisible = NULL;
4501 int visible_pos = -1;
4503 if (!TREEVIEW_ValidItem(infoPtr, item))
4504 return FALSE;
4506 if (!ISVISIBLE(item))
4508 /* Expand parents as necessary. */
4509 HTREEITEM parent;
4511 /* see if we are trying to ensure that root is visible */
4512 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4513 parent = item->parent;
4514 else
4515 parent = item; /* this item is the topmost item */
4517 while (parent != infoPtr->root)
4519 if (!(parent->state & TVIS_EXPANDED))
4520 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4522 parent = parent->parent;
4526 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4528 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4529 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4531 if (hasFirstVisible)
4532 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4534 if (visible_pos < 0)
4536 /* item is before the start of the list: put it at the top. */
4537 newFirstVisible = item;
4539 else if (visible_pos >= viscount
4540 /* Sometimes, before we are displayed, GVC is 0, causing us to
4541 * spuriously scroll up. */
4542 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4544 /* item is past the end of the list. */
4545 int scroll = visible_pos - viscount;
4547 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4548 scroll + 1);
4551 if (bHScroll)
4553 /* Scroll window so item's text is visible as much as possible */
4554 /* Calculation of amount of extra space is taken from EditLabel code */
4555 INT pos, x;
4556 TEXTMETRICW textMetric;
4557 HDC hdc = GetWindowDC(infoPtr->hwnd);
4559 x = item->textWidth;
4561 GetTextMetricsW(hdc, &textMetric);
4562 ReleaseDC(infoPtr->hwnd, hdc);
4564 x += (textMetric.tmMaxCharWidth * 2);
4565 x = max(x, textMetric.tmMaxCharWidth * 3);
4567 if (item->textOffset < 0)
4568 pos = item->textOffset;
4569 else if (item->textOffset + x > infoPtr->clientWidth)
4571 if (x > infoPtr->clientWidth)
4572 pos = item->textOffset;
4573 else
4574 pos = item->textOffset + x - infoPtr->clientWidth;
4576 else
4577 pos = 0;
4579 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4582 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4584 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4586 return TRUE;
4589 return FALSE;
4592 static VOID
4593 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4594 TREEVIEW_ITEM *newFirstVisible,
4595 BOOL bUpdateScrollPos)
4597 int gap_size;
4599 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4601 if (newFirstVisible != NULL)
4603 /* Prevent an empty gap from appearing at the bottom... */
4604 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4605 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4607 if (gap_size > 0)
4609 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4610 -gap_size);
4612 /* ... unless we just don't have enough items. */
4613 if (newFirstVisible == NULL)
4614 newFirstVisible = infoPtr->root->firstChild;
4618 if (infoPtr->firstVisible != newFirstVisible)
4620 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4622 infoPtr->firstVisible = newFirstVisible;
4623 TREEVIEW_Invalidate(infoPtr, NULL);
4625 else
4627 TREEVIEW_ITEM *item;
4628 int scroll = infoPtr->uItemHeight *
4629 (infoPtr->firstVisible->visibleOrder
4630 - newFirstVisible->visibleOrder);
4632 infoPtr->firstVisible = newFirstVisible;
4634 for (item = infoPtr->root->firstChild; item != NULL;
4635 item = TREEVIEW_GetNextListItem(infoPtr, item))
4637 item->rect.top += scroll;
4638 item->rect.bottom += scroll;
4641 if (bUpdateScrollPos)
4642 SetScrollPos(infoPtr->hwnd, SB_VERT,
4643 newFirstVisible->visibleOrder, TRUE);
4645 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4650 /************************************************************************
4651 * VScroll is always in units of visible items. i.e. we always have a
4652 * visible item aligned to the top of the control. (Unless we have no
4653 * items at all.)
4655 static LRESULT
4656 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4658 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4659 TREEVIEW_ITEM *newFirstVisible = NULL;
4661 int nScrollCode = LOWORD(wParam);
4663 TRACE("wp %lx\n", wParam);
4665 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4666 return 0;
4668 if (!oldFirstVisible)
4670 assert(infoPtr->root->firstChild == NULL);
4671 return 0;
4674 switch (nScrollCode)
4676 case SB_TOP:
4677 newFirstVisible = infoPtr->root->firstChild;
4678 break;
4680 case SB_BOTTOM:
4681 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4682 break;
4684 case SB_LINEUP:
4685 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4686 break;
4688 case SB_LINEDOWN:
4689 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4690 break;
4692 case SB_PAGEUP:
4693 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4694 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4695 break;
4697 case SB_PAGEDOWN:
4698 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4699 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4700 break;
4702 case SB_THUMBTRACK:
4703 case SB_THUMBPOSITION:
4704 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4705 infoPtr->root->firstChild,
4706 (LONG)(SHORT)HIWORD(wParam));
4707 break;
4709 case SB_ENDSCROLL:
4710 return 0;
4713 if (newFirstVisible != NULL)
4715 if (newFirstVisible != oldFirstVisible)
4716 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4717 nScrollCode != SB_THUMBTRACK);
4718 else if (nScrollCode == SB_THUMBPOSITION)
4719 SetScrollPos(infoPtr->hwnd, SB_VERT,
4720 newFirstVisible->visibleOrder, TRUE);
4723 return 0;
4726 static LRESULT
4727 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4729 int maxWidth;
4730 int scrollX = infoPtr->scrollX;
4731 int nScrollCode = LOWORD(wParam);
4733 TRACE("wp %lx\n", wParam);
4735 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4736 return FALSE;
4738 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4739 /* shall never occur */
4740 if (maxWidth <= 0)
4742 scrollX = 0;
4743 goto scroll;
4746 switch (nScrollCode)
4748 case SB_LINELEFT:
4749 scrollX -= infoPtr->uItemHeight;
4750 break;
4751 case SB_LINERIGHT:
4752 scrollX += infoPtr->uItemHeight;
4753 break;
4754 case SB_PAGELEFT:
4755 scrollX -= infoPtr->clientWidth;
4756 break;
4757 case SB_PAGERIGHT:
4758 scrollX += infoPtr->clientWidth;
4759 break;
4761 case SB_THUMBTRACK:
4762 case SB_THUMBPOSITION:
4763 scrollX = (int)(SHORT)HIWORD(wParam);
4764 break;
4766 case SB_ENDSCROLL:
4767 return 0;
4770 if (scrollX > maxWidth)
4771 scrollX = maxWidth;
4772 else if (scrollX < 0)
4773 scrollX = 0;
4775 scroll:
4776 if (scrollX != infoPtr->scrollX)
4778 TREEVIEW_ITEM *item;
4779 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4781 for (item = infoPtr->root->firstChild; item != NULL;
4782 item = TREEVIEW_GetNextListItem(infoPtr, item))
4784 item->linesOffset += scroll_pixels;
4785 item->stateOffset += scroll_pixels;
4786 item->imageOffset += scroll_pixels;
4787 item->textOffset += scroll_pixels;
4790 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4791 infoPtr->scrollX = scrollX;
4792 UpdateWindow(infoPtr->hwnd);
4795 if (nScrollCode != SB_THUMBTRACK)
4796 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4798 return 0;
4801 static LRESULT
4802 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4804 short gcWheelDelta;
4805 UINT pulScrollLines = 3;
4807 if (infoPtr->firstVisible == NULL)
4808 return TRUE;
4810 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4812 gcWheelDelta = -(short)HIWORD(wParam);
4813 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4815 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4817 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4818 int maxDy = infoPtr->maxVisibleOrder;
4820 if (newDy > maxDy)
4821 newDy = maxDy;
4823 if (newDy < 0)
4824 newDy = 0;
4826 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4828 return TRUE;
4831 /* Create/Destroy *******************************************************/
4833 static void
4834 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4836 RECT rc;
4837 HBITMAP hbm, hbmOld;
4838 HDC hdc, hdcScreen;
4839 int nIndex;
4841 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4843 hdcScreen = GetDC(0);
4845 hdc = CreateCompatibleDC(hdcScreen);
4846 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4847 hbmOld = SelectObject(hdc, hbm);
4849 SetRect(&rc, 0, 0, 48, 16);
4850 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4852 SetRect(&rc, 18, 2, 30, 14);
4853 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4854 DFCS_BUTTONCHECK|DFCS_FLAT);
4856 SetRect(&rc, 34, 2, 46, 14);
4857 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4858 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4860 SelectObject(hdc, hbmOld);
4861 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4862 GetSysColor(COLOR_WINDOW));
4863 TRACE("checkbox index %d\n", nIndex);
4865 DeleteObject(hbm);
4866 DeleteDC(hdc);
4867 ReleaseDC(0, hdcScreen);
4869 infoPtr->stateImageWidth = 16;
4870 infoPtr->stateImageHeight = 16;
4873 static LRESULT
4874 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4876 RECT rcClient;
4877 TREEVIEW_INFO *infoPtr;
4878 LOGFONTW lf;
4880 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4882 infoPtr = (TREEVIEW_INFO *)Alloc(sizeof(TREEVIEW_INFO));
4884 if (infoPtr == NULL)
4886 ERR("could not allocate info memory!\n");
4887 return 0;
4890 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4892 infoPtr->hwnd = hwnd;
4893 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4894 infoPtr->Timer = 0;
4895 infoPtr->uNumItems = 0;
4896 infoPtr->cdmode = 0;
4897 infoPtr->uScrollTime = 300; /* milliseconds */
4898 infoPtr->bRedraw = TRUE;
4900 GetClientRect(hwnd, &rcClient);
4902 /* No scroll bars yet. */
4903 infoPtr->clientWidth = rcClient.right;
4904 infoPtr->clientHeight = rcClient.bottom;
4905 infoPtr->uInternalStatus = 0;
4907 infoPtr->treeWidth = 0;
4908 infoPtr->treeHeight = 0;
4910 infoPtr->uIndent = MINIMUM_INDENT;
4911 infoPtr->selectedItem = 0;
4912 infoPtr->focusedItem = 0;
4913 infoPtr->hotItem = 0;
4914 infoPtr->firstVisible = 0;
4915 infoPtr->maxVisibleOrder = 0;
4916 infoPtr->dropItem = 0;
4917 infoPtr->insertMarkItem = 0;
4918 infoPtr->insertBeforeorAfter = 0;
4919 /* dragList */
4921 infoPtr->scrollX = 0;
4923 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4924 infoPtr->clrText = -1; /* use system color */
4925 infoPtr->clrLine = RGB(128, 128, 128);
4926 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4928 /* hwndToolTip */
4930 infoPtr->hwndEdit = 0;
4931 infoPtr->wpEditOrig = NULL;
4932 infoPtr->bIgnoreEditKillFocus = FALSE;
4933 infoPtr->bLabelChanged = FALSE;
4935 infoPtr->himlNormal = NULL;
4936 infoPtr->himlState = NULL;
4937 infoPtr->normalImageWidth = 0;
4938 infoPtr->normalImageHeight = 0;
4939 infoPtr->stateImageWidth = 0;
4940 infoPtr->stateImageHeight = 0;
4942 infoPtr->items = DPA_Create(16);
4944 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
4945 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
4946 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4947 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
4948 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
4950 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4952 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4953 infoPtr->root->state = TVIS_EXPANDED;
4954 infoPtr->root->iLevel = -1;
4955 infoPtr->root->visibleOrder = -1;
4957 infoPtr->hwndNotify = lpcs->hwndParent;
4958 #if 0
4959 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4960 #endif
4962 infoPtr->hwndToolTip = 0;
4964 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
4966 /* Determine what type of notify should be issued */
4967 /* sets infoPtr->bNtfUnicode */
4968 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
4970 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4971 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
4972 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4973 hwnd, 0, 0, 0);
4975 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4976 initialize_checkboxes(infoPtr);
4978 /* Make sure actual scrollbar state is consistent with uInternalStatus */
4979 ShowScrollBar(hwnd, SB_VERT, FALSE);
4980 ShowScrollBar(hwnd, SB_HORZ, FALSE);
4982 OpenThemeData (hwnd, themeClass);
4984 return 0;
4988 static LRESULT
4989 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4991 TRACE("\n");
4993 TREEVIEW_RemoveTree(infoPtr);
4995 /* tool tip is automatically destroyed: we are its owner */
4997 /* Restore original wndproc */
4998 if (infoPtr->hwndEdit)
4999 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5000 (DWORD_PTR)infoPtr->wpEditOrig);
5002 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5004 /* Deassociate treeview from the window before doing anything drastic. */
5005 SetWindowLongPtrW(infoPtr->hwnd, 0, (DWORD_PTR)NULL);
5008 DeleteObject(infoPtr->hDefaultFont);
5009 DeleteObject(infoPtr->hBoldFont);
5010 DeleteObject(infoPtr->hUnderlineFont);
5011 Free(infoPtr);
5013 return 0;
5016 /* Miscellaneous Messages ***********************************************/
5018 static LRESULT
5019 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5021 static const struct
5023 unsigned char code;
5025 scroll[] =
5027 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5028 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5029 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5030 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5031 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5032 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5033 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5034 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5035 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5036 #undef SCROLL_ENTRY
5039 if (key >= VK_PRIOR && key <= VK_DOWN)
5041 unsigned char code = scroll[key - VK_PRIOR].code;
5043 (((code & (1 << 7)) == (SB_HORZ << 7))
5044 ? TREEVIEW_HScroll
5045 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5048 return 0;
5051 /************************************************************************
5052 * TREEVIEW_KeyDown
5054 * VK_UP Move selection to the previous non-hidden item.
5055 * VK_DOWN Move selection to the next non-hidden item.
5056 * VK_HOME Move selection to the first item.
5057 * VK_END Move selection to the last item.
5058 * VK_LEFT If expanded then collapse, otherwise move to parent.
5059 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5060 * VK_ADD Expand.
5061 * VK_SUBTRACT Collapse.
5062 * VK_MULTIPLY Expand all.
5063 * VK_PRIOR Move up GetVisibleCount items.
5064 * VK_NEXT Move down GetVisibleCount items.
5065 * VK_BACK Move to parent.
5066 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5068 static LRESULT
5069 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5071 /* If it is non-NULL and different, it will be selected and visible. */
5072 TREEVIEW_ITEM *newSelection = NULL;
5074 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5076 TRACE("%lx\n", wParam);
5078 if (prevItem == NULL)
5079 return FALSE;
5081 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5082 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5084 switch (wParam)
5086 case VK_UP:
5087 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5088 if (!newSelection)
5089 newSelection = infoPtr->root->firstChild;
5090 break;
5092 case VK_DOWN:
5093 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5094 break;
5096 case VK_HOME:
5097 newSelection = infoPtr->root->firstChild;
5098 break;
5100 case VK_END:
5101 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5102 break;
5104 case VK_LEFT:
5105 if (prevItem->state & TVIS_EXPANDED)
5107 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5109 else if (prevItem->parent != infoPtr->root)
5111 newSelection = prevItem->parent;
5113 break;
5115 case VK_RIGHT:
5116 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5118 if (!(prevItem->state & TVIS_EXPANDED))
5119 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5120 else
5122 newSelection = prevItem->firstChild;
5126 break;
5128 case VK_MULTIPLY:
5129 TREEVIEW_ExpandAll(infoPtr, prevItem);
5130 break;
5132 case VK_ADD:
5133 if (!(prevItem->state & TVIS_EXPANDED))
5134 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5135 break;
5137 case VK_SUBTRACT:
5138 if (prevItem->state & TVIS_EXPANDED)
5139 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5140 break;
5142 case VK_PRIOR:
5143 newSelection
5144 = TREEVIEW_GetListItem(infoPtr, prevItem,
5145 -TREEVIEW_GetVisibleCount(infoPtr));
5146 break;
5148 case VK_NEXT:
5149 newSelection
5150 = TREEVIEW_GetListItem(infoPtr, prevItem,
5151 TREEVIEW_GetVisibleCount(infoPtr));
5152 break;
5154 case VK_BACK:
5155 newSelection = prevItem->parent;
5156 if (newSelection == infoPtr->root)
5157 newSelection = NULL;
5158 break;
5160 case VK_SPACE:
5161 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5162 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5163 break;
5166 if (newSelection && newSelection != prevItem)
5168 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5169 TVC_BYKEYBOARD))
5171 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5175 return FALSE;
5178 static LRESULT
5179 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5181 if (infoPtr->hotItem)
5183 /* remove hot effect from item */
5184 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5185 infoPtr->hotItem = NULL;
5187 return 0;
5190 static LRESULT
5191 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, WPARAM wParam, LPARAM lParam)
5193 POINT pt;
5194 TRACKMOUSEEVENT trackinfo;
5195 TREEVIEW_ITEM * item;
5197 /* fill in the TRACKMOUSEEVENT struct */
5198 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5199 trackinfo.dwFlags = TME_QUERY;
5200 trackinfo.hwndTrack = infoPtr->hwnd;
5201 trackinfo.dwHoverTime = HOVER_DEFAULT;
5203 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5204 _TrackMouseEvent(&trackinfo);
5206 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5207 if(!(trackinfo.dwFlags & TME_LEAVE))
5209 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5211 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5212 /* and can properly deactivate the hot item */
5213 _TrackMouseEvent(&trackinfo);
5216 pt.x = (short)LOWORD(lParam);
5217 pt.y = (short)HIWORD(lParam);
5219 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5221 if (item != infoPtr->hotItem)
5223 /* redraw old hot item */
5224 if (infoPtr->hotItem)
5225 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5226 infoPtr->hotItem = item;
5227 /* redraw new hot item */
5228 if (infoPtr->hotItem)
5229 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5232 return 0;
5235 /* Draw themed border */
5236 static BOOL nc_paint (const TREEVIEW_INFO *infoPtr, HRGN region)
5238 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5239 HDC dc;
5240 RECT r;
5241 HRGN cliprgn;
5242 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5243 cyEdge = GetSystemMetrics (SM_CYEDGE);
5245 if (!theme) return FALSE;
5247 GetWindowRect(infoPtr->hwnd, &r);
5249 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5250 r.right - cxEdge, r.bottom - cyEdge);
5251 if (region != (HRGN)1)
5252 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5253 OffsetRect(&r, -r.left, -r.top);
5255 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5256 OffsetRect(&r, -r.left, -r.top);
5258 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5259 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5260 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5261 ReleaseDC(infoPtr->hwnd, dc);
5263 /* Call default proc to get the scrollbars etc. painted */
5264 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5266 return TRUE;
5269 static LRESULT
5270 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5272 LPNMHDR lpnmh = (LPNMHDR)lParam;
5274 if (lpnmh->code == PGN_CALCSIZE) {
5275 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5277 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5278 lppgc->iWidth = infoPtr->treeWidth;
5279 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5280 infoPtr->treeWidth, infoPtr->clientWidth);
5282 else {
5283 lppgc->iHeight = infoPtr->treeHeight;
5284 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5285 infoPtr->treeHeight, infoPtr->clientHeight);
5287 return 0;
5289 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5292 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5294 INT format;
5296 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5298 if (nCommand != NF_REQUERY) return 0;
5300 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5301 TRACE("format=%d\n", format);
5303 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5305 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5307 return format;
5310 static LRESULT
5311 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5313 if (wParam == SIZE_RESTORED)
5315 infoPtr->clientWidth = (short)LOWORD(lParam);
5316 infoPtr->clientHeight = (short)HIWORD(lParam);
5318 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5319 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5320 TREEVIEW_UpdateScrollBars(infoPtr);
5322 else
5324 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5327 TREEVIEW_Invalidate(infoPtr, NULL);
5328 return 0;
5331 static LRESULT
5332 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5334 TRACE("(%lx %lx)\n", wParam, lParam);
5336 if (wParam == GWL_STYLE)
5338 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5340 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5342 if (dwNewStyle & TVS_CHECKBOXES)
5344 initialize_checkboxes(infoPtr);
5345 TRACE("checkboxes enabled\n");
5347 else
5349 FIXME("tried to disable checkboxes\n");
5353 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5355 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5357 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5358 TRACE("tooltips enabled\n");
5360 else
5362 DestroyWindow(infoPtr->hwndToolTip);
5363 infoPtr->hwndToolTip = 0;
5364 TRACE("tooltips disabled\n");
5368 infoPtr->dwStyle = dwNewStyle;
5371 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5372 TREEVIEW_UpdateScrollBars(infoPtr);
5373 TREEVIEW_Invalidate(infoPtr, NULL);
5375 return 0;
5378 static LRESULT
5379 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5381 POINT pt;
5382 TREEVIEW_ITEM * item;
5384 GetCursorPos(&pt);
5385 ScreenToClient(infoPtr->hwnd, &pt);
5387 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5389 /* FIXME: send NM_SETCURSOR */
5391 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5393 SetCursor(infoPtr->hcurHand);
5394 return 0;
5396 else
5397 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5400 static LRESULT
5401 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5403 TRACE("\n");
5405 if (!infoPtr->selectedItem)
5407 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5408 TVC_UNKNOWN);
5411 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5412 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5413 return 0;
5416 static LRESULT
5417 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5419 TRACE("\n");
5421 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5422 UpdateWindow(infoPtr->hwnd);
5423 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5424 return 0;
5427 /* update theme after a WM_THEMECHANGED message */
5428 static LRESULT theme_changed(const TREEVIEW_INFO *infoPtr)
5430 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5431 CloseThemeData (theme);
5432 OpenThemeData (infoPtr->hwnd, themeClass);
5433 return 0;
5437 static LRESULT WINAPI
5438 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5440 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5442 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5444 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5445 else
5447 if (uMsg == WM_CREATE)
5448 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5449 else
5450 goto def;
5453 switch (uMsg)
5455 case TVM_CREATEDRAGIMAGE:
5456 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5458 case TVM_DELETEITEM:
5459 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5461 case TVM_EDITLABELA:
5462 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5464 case TVM_EDITLABELW:
5465 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5467 case TVM_ENDEDITLABELNOW:
5468 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5470 case TVM_ENSUREVISIBLE:
5471 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5473 case TVM_EXPAND:
5474 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5476 case TVM_GETBKCOLOR:
5477 return TREEVIEW_GetBkColor(infoPtr);
5479 case TVM_GETCOUNT:
5480 return TREEVIEW_GetCount(infoPtr);
5482 case TVM_GETEDITCONTROL:
5483 return TREEVIEW_GetEditControl(infoPtr);
5485 case TVM_GETIMAGELIST:
5486 return TREEVIEW_GetImageList(infoPtr, wParam);
5488 case TVM_GETINDENT:
5489 return TREEVIEW_GetIndent(infoPtr);
5491 case TVM_GETINSERTMARKCOLOR:
5492 return TREEVIEW_GetInsertMarkColor(infoPtr);
5494 case TVM_GETISEARCHSTRINGA:
5495 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5496 return 0;
5498 case TVM_GETISEARCHSTRINGW:
5499 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5500 return 0;
5502 case TVM_GETITEMA:
5503 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5505 case TVM_GETITEMW:
5506 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5508 case TVM_GETITEMHEIGHT:
5509 return TREEVIEW_GetItemHeight(infoPtr);
5511 case TVM_GETITEMRECT:
5512 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5514 case TVM_GETITEMSTATE:
5515 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5517 case TVM_GETLINECOLOR:
5518 return TREEVIEW_GetLineColor(infoPtr);
5520 case TVM_GETNEXTITEM:
5521 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5523 case TVM_GETSCROLLTIME:
5524 return TREEVIEW_GetScrollTime(infoPtr);
5526 case TVM_GETTEXTCOLOR:
5527 return TREEVIEW_GetTextColor(infoPtr);
5529 case TVM_GETTOOLTIPS:
5530 return TREEVIEW_GetToolTips(infoPtr);
5532 case TVM_GETUNICODEFORMAT:
5533 return TREEVIEW_GetUnicodeFormat(infoPtr);
5535 case TVM_GETVISIBLECOUNT:
5536 return TREEVIEW_GetVisibleCount(infoPtr);
5538 case TVM_HITTEST:
5539 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5541 case TVM_INSERTITEMA:
5542 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, FALSE);
5544 case TVM_INSERTITEMW:
5545 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, TRUE);
5547 case TVM_SELECTITEM:
5548 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5550 case TVM_SETBKCOLOR:
5551 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5553 case TVM_SETIMAGELIST:
5554 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5556 case TVM_SETINDENT:
5557 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5559 case TVM_SETINSERTMARK:
5560 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5562 case TVM_SETINSERTMARKCOLOR:
5563 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5565 case TVM_SETITEMA:
5566 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5568 case TVM_SETITEMW:
5569 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5571 case TVM_SETLINECOLOR:
5572 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5574 case TVM_SETITEMHEIGHT:
5575 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5577 case TVM_SETSCROLLTIME:
5578 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5580 case TVM_SETTEXTCOLOR:
5581 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5583 case TVM_SETTOOLTIPS:
5584 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5586 case TVM_SETUNICODEFORMAT:
5587 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5589 case TVM_SORTCHILDREN:
5590 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5592 case TVM_SORTCHILDRENCB:
5593 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5595 case WM_CHAR:
5596 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5598 case WM_COMMAND:
5599 return TREEVIEW_Command(infoPtr, wParam, lParam);
5601 case WM_DESTROY:
5602 return TREEVIEW_Destroy(infoPtr);
5604 /* WM_ENABLE */
5606 case WM_ERASEBKGND:
5607 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5609 case WM_GETDLGCODE:
5610 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5612 case WM_GETFONT:
5613 return TREEVIEW_GetFont(infoPtr);
5615 case WM_HSCROLL:
5616 return TREEVIEW_HScroll(infoPtr, wParam);
5618 case WM_KEYDOWN:
5619 return TREEVIEW_KeyDown(infoPtr, wParam);
5621 case WM_KILLFOCUS:
5622 return TREEVIEW_KillFocus(infoPtr);
5624 case WM_LBUTTONDBLCLK:
5625 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5627 case WM_LBUTTONDOWN:
5628 return TREEVIEW_LButtonDown(infoPtr, lParam);
5630 /* WM_MBUTTONDOWN */
5632 case WM_MOUSELEAVE:
5633 return TREEVIEW_MouseLeave(infoPtr);
5635 case WM_MOUSEMOVE:
5636 if (infoPtr->dwStyle & TVS_TRACKSELECT)
5637 return TREEVIEW_MouseMove(infoPtr, wParam, lParam);
5638 else
5639 return 0;
5641 case WM_NCLBUTTONDOWN:
5642 if (infoPtr->hwndEdit)
5643 SetFocus(infoPtr->hwnd);
5644 goto def;
5646 case WM_NCPAINT:
5647 if (nc_paint (infoPtr, (HRGN)wParam))
5648 return 0;
5649 goto def;
5651 case WM_NOTIFY:
5652 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5654 case WM_NOTIFYFORMAT:
5655 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5657 case WM_PRINTCLIENT:
5658 case WM_PAINT:
5659 return TREEVIEW_Paint(infoPtr, wParam);
5661 case WM_RBUTTONDOWN:
5662 return TREEVIEW_RButtonDown(infoPtr, lParam);
5664 case WM_SETCURSOR:
5665 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5667 case WM_SETFOCUS:
5668 return TREEVIEW_SetFocus(infoPtr);
5670 case WM_SETFONT:
5671 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5673 case WM_SETREDRAW:
5674 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5676 case WM_SIZE:
5677 return TREEVIEW_Size(infoPtr, wParam, lParam);
5679 case WM_STYLECHANGED:
5680 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5682 /* WM_SYSCOLORCHANGE */
5684 /* WM_SYSKEYDOWN */
5686 case WM_TIMER:
5687 return TREEVIEW_HandleTimer(infoPtr, wParam);
5689 case WM_THEMECHANGED:
5690 return theme_changed (infoPtr);
5692 case WM_VSCROLL:
5693 return TREEVIEW_VScroll(infoPtr, wParam);
5695 /* WM_WININICHANGE */
5697 case WM_MOUSEWHEEL:
5698 if (wParam & (MK_SHIFT | MK_CONTROL))
5699 goto def;
5700 return TREEVIEW_MouseWheel(infoPtr, wParam);
5702 case WM_DRAWITEM:
5703 TRACE("drawItem\n");
5704 goto def;
5706 default:
5707 /* This mostly catches MFC and Delphi messages. :( */
5708 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5709 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5710 def:
5711 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5716 /* Class Registration ***************************************************/
5718 VOID
5719 TREEVIEW_Register(void)
5721 WNDCLASSW wndClass;
5723 TRACE("\n");
5725 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5726 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5727 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5728 wndClass.cbClsExtra = 0;
5729 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5731 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5732 wndClass.hbrBackground = 0;
5733 wndClass.lpszClassName = WC_TREEVIEWW;
5735 RegisterClassW(&wndClass);
5739 VOID
5740 TREEVIEW_Unregister(void)
5742 UnregisterClassW(WC_TREEVIEWW, NULL);
5746 /* Tree Verification ****************************************************/
5748 #ifdef NDEBUG
5749 static inline void
5750 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5752 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5753 TREEVIEW_ITEM *item)
5755 assert(infoPtr != NULL);
5756 assert(item != NULL);
5758 /* both NULL, or both non-null */
5759 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5761 assert(item->firstChild != item);
5762 assert(item->lastChild != item);
5764 if (item->firstChild)
5766 assert(item->firstChild->parent == item);
5767 assert(item->firstChild->prevSibling == NULL);
5770 if (item->lastChild)
5772 assert(item->lastChild->parent == item);
5773 assert(item->lastChild->nextSibling == NULL);
5776 assert(item->nextSibling != item);
5777 if (item->nextSibling)
5779 assert(item->nextSibling->parent == item->parent);
5780 assert(item->nextSibling->prevSibling == item);
5783 assert(item->prevSibling != item);
5784 if (item->prevSibling)
5786 assert(item->prevSibling->parent == item->parent);
5787 assert(item->prevSibling->nextSibling == item);
5791 static inline void
5792 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5794 assert(item != NULL);
5796 assert(item->parent != NULL);
5797 assert(item->parent != item);
5798 assert(item->iLevel == item->parent->iLevel + 1);
5800 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5802 TREEVIEW_VerifyItemCommon(infoPtr, item);
5804 TREEVIEW_VerifyChildren(infoPtr, item);
5807 static inline void
5808 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5810 TREEVIEW_ITEM *child;
5811 assert(item != NULL);
5813 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5814 TREEVIEW_VerifyItem(infoPtr, child);
5817 static inline void
5818 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5820 TREEVIEW_ITEM *root = infoPtr->root;
5822 assert(root != NULL);
5823 assert(root->iLevel == -1);
5824 assert(root->parent == NULL);
5825 assert(root->prevSibling == NULL);
5827 TREEVIEW_VerifyItemCommon(infoPtr, root);
5829 TREEVIEW_VerifyChildren(infoPtr, root);
5832 static void
5833 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5835 assert(infoPtr != NULL);
5837 TREEVIEW_VerifyRoot(infoPtr);
5839 #endif