wine.inf: Fix incorrect entry for hh.exe.
[wine/multimedia.git] / dlls / comctl32 / treeview.c
blob8bea9738a2b466b25e3c82dcc069af61e75d1a83
1 /* Treeview control
3 * Copyright 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de>
4 * Copyright 1998,1999 Alex Priem <alexp@sci.kun.nl>
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTES
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
26 * Note2: If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
27 * of size TEXT_CALLBACK_SIZE in DoSetItem.
28 * We use callbackMask to keep track of fields to be updated.
30 * TODO:
31 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
32 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
38 * Make the insertion mark look right.
39 * Scroll (instead of repaint) as much as possible.
42 #include "config.h"
43 #include "wine/port.h"
45 #include <assert.h>
46 #include <ctype.h>
47 #include <stdarg.h>
48 #include <string.h>
49 #include <limits.h>
50 #include <stdlib.h>
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
54 #include "windef.h"
55 #include "winbase.h"
56 #include "wingdi.h"
57 #include "winuser.h"
58 #include "winnls.h"
59 #include "commctrl.h"
60 #include "comctl32.h"
61 #include "uxtheme.h"
62 #include "tmschema.h"
63 #include "wine/unicode.h"
64 #include "wine/debug.h"
66 /* internal structures */
68 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
70 UINT callbackMask;
71 UINT state;
72 UINT stateMask;
73 LPWSTR pszText;
74 int cchTextMax;
75 int iImage;
76 int iSelectedImage;
77 int cChildren;
78 LPARAM lParam;
79 int iIntegral; /* item height multiplier (1 is normal) */
80 int iLevel; /* indentation level:0=root level */
81 HTREEITEM parent; /* handle to parent or 0 if at root */
82 HTREEITEM firstChild; /* handle to first child or 0 if no child */
83 HTREEITEM lastChild;
84 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
85 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
86 RECT rect;
87 LONG linesOffset;
88 LONG stateOffset;
89 LONG imageOffset;
90 LONG textOffset;
91 LONG textWidth; /* horizontal text extent for pszText */
92 LONG visibleOrder; /* visible ordering, 0 is first visible item */
93 } TREEVIEW_ITEM;
96 typedef struct tagTREEVIEW_INFO
98 HWND hwnd;
99 HWND hwndNotify; /* Owner window to send notifications to */
100 DWORD dwStyle;
101 HTREEITEM root;
102 UINT uInternalStatus;
103 INT Timer;
104 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
105 INT cdmode; /* last custom draw setting */
106 UINT uScrollTime; /* max. time for scrolling in milliseconds */
107 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
109 UINT uItemHeight; /* item height */
110 BOOL bHeightSet;
112 LONG clientWidth; /* width of control window */
113 LONG clientHeight; /* height of control window */
115 LONG treeWidth; /* width of visible tree items */
116 LONG treeHeight; /* height of visible tree items */
118 UINT uIndent; /* indentation in pixels */
119 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
120 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
121 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
123 HTREEITEM firstVisible; /* handle to first visible item */
124 LONG maxVisibleOrder;
125 HTREEITEM dropItem; /* handle to item selected by drag cursor */
126 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
127 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
128 HIMAGELIST dragList; /* Bitmap of dragged item */
129 LONG scrollX;
130 COLORREF clrBk;
131 COLORREF clrText;
132 COLORREF clrLine;
133 COLORREF clrInsertMark;
134 HFONT hFont;
135 HFONT hDefaultFont;
136 HFONT hBoldFont;
137 HFONT hUnderlineFont;
138 HCURSOR hcurHand;
139 HWND hwndToolTip;
141 HWND hwndEdit;
142 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
143 BOOL bIgnoreEditKillFocus;
144 BOOL bLabelChanged;
146 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
147 HIMAGELIST himlNormal;
148 int normalImageHeight;
149 int normalImageWidth;
150 HIMAGELIST himlState;
151 int stateImageHeight;
152 int stateImageWidth;
153 HDPA items;
155 DWORD lastKeyPressTimestamp; /* Added */
156 WPARAM charCode; /* Added */
157 INT nSearchParamLength; /* Added */
158 WCHAR szSearchParam[ MAX_PATH ]; /* Added */
159 } TREEVIEW_INFO;
162 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
163 #define KEY_DELAY 450
165 /* bitflags for infoPtr->uInternalStatus */
167 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
168 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
169 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
170 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
171 #define TV_RDRAG 0x10 /* ditto Rbutton */
172 #define TV_RDRAGGING 0x20
174 /* bitflags for infoPtr->timer */
176 #define TV_EDIT_TIMER 2
177 #define TV_EDIT_TIMER_SET 2
180 VOID TREEVIEW_Register (VOID);
181 VOID TREEVIEW_Unregister (VOID);
184 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
187 #define TEXT_CALLBACK_SIZE 260
189 #define TREEVIEW_LEFT_MARGIN 8
191 #define MINIMUM_INDENT 19
193 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
195 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
196 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
197 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
200 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
203 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
206 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
208 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
209 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
210 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
211 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
212 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
213 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
214 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
215 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND wParam, UINT lParam);
218 /* Random Utilities *****************************************************/
220 #ifndef NDEBUG
221 static inline void
222 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
224 (void)infoPtr;
226 #else
227 /* The definition is at the end of the file. */
228 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
229 #endif
231 /* Returns the treeview private data if hwnd is a treeview.
232 * Otherwise returns an undefined value. */
233 static TREEVIEW_INFO *
234 TREEVIEW_GetInfoPtr(HWND hwnd)
236 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
239 /* Don't call this. Nothing wants an item index. */
240 static inline int
241 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
243 return DPA_GetPtrIndex(infoPtr->items, handle);
246 /* Checks if item has changed and needs to be redrawn */
247 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
248 const TVITEMEXW *tvChange)
250 /* Number of children has changed */
251 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
252 return TRUE;
254 /* Image has changed and it's not a callback */
255 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
256 tiNew->iImage != I_IMAGECALLBACK)
257 return TRUE;
259 /* Selected image has changed and it's not a callback */
260 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
261 tiNew->iSelectedImage != I_IMAGECALLBACK)
262 return TRUE;
264 /* Text has changed and it's not a callback */
265 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
266 tiNew->pszText != LPSTR_TEXTCALLBACKW)
267 return TRUE;
269 /* Indent has changed */
270 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
271 return TRUE;
273 /* Item state has changed */
274 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
275 return TRUE;
277 return FALSE;
280 /***************************************************************************
281 * This method checks that handle is an item for this tree.
283 static BOOL
284 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
286 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
288 TRACE("invalid item %p\n", handle);
289 return FALSE;
291 else
292 return TRUE;
295 static HFONT
296 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
298 LOGFONTW font;
300 GetObjectW(hOrigFont, sizeof(font), &font);
301 font.lfWeight = FW_BOLD;
302 return CreateFontIndirectW(&font);
305 static HFONT
306 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
308 LOGFONTW font;
310 GetObjectW(hOrigFont, sizeof(font), &font);
311 font.lfUnderline = TRUE;
312 return CreateFontIndirectW(&font);
315 static inline HFONT
316 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
318 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
319 return infoPtr->hUnderlineFont;
320 if (item->state & TVIS_BOLD)
321 return infoPtr->hBoldFont;
322 return infoPtr->hFont;
325 /* for trace/debugging purposes only */
326 static const char *
327 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
329 if (item == NULL) return "<null item>";
330 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
331 if (item->pszText == NULL) return "<null>";
332 return debugstr_w(item->pszText);
335 /* An item is not a child of itself. */
336 static BOOL
337 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
341 child = child->parent;
342 if (child == parent) return TRUE;
343 } while (child != NULL);
345 return FALSE;
349 /* Tree Traversal *******************************************************/
351 /***************************************************************************
352 * This method returns the last expanded sibling or child child item
353 * of a tree node
355 static TREEVIEW_ITEM *
356 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
358 if (!wineItem)
359 return NULL;
361 while (wineItem->lastChild)
363 if (wineItem->state & TVIS_EXPANDED)
364 wineItem = wineItem->lastChild;
365 else
366 break;
369 if (wineItem == infoPtr->root)
370 return NULL;
372 return wineItem;
375 /***************************************************************************
376 * This method returns the previous non-hidden item in the list not
377 * considering the tree hierarchy.
379 static TREEVIEW_ITEM *
380 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
382 if (tvItem->prevSibling)
384 /* This item has a prevSibling, get the last item in the sibling's tree. */
385 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
387 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
388 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
389 else
390 return upItem;
392 else
394 /* this item does not have a prevSibling, get the parent */
395 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
400 /***************************************************************************
401 * This method returns the next physical item in the treeview not
402 * considering the tree hierarchy.
404 static TREEVIEW_ITEM *
405 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
408 * If this item has children and is expanded, return the first child
410 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
412 return tvItem->firstChild;
417 * try to get the sibling
419 if (tvItem->nextSibling)
420 return tvItem->nextSibling;
423 * Otherwise, get the parent's sibling.
425 while (tvItem->parent)
427 tvItem = tvItem->parent;
429 if (tvItem->nextSibling)
430 return tvItem->nextSibling;
433 return NULL;
436 /***************************************************************************
437 * This method returns the nth item starting at the given item. It returns
438 * the last item (or first) we we run out of items.
440 * Will scroll backward if count is <0.
441 * forward if count is >0.
443 static TREEVIEW_ITEM *
444 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
445 LONG count)
447 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
448 TREEVIEW_ITEM *previousItem;
450 assert(wineItem != NULL);
452 if (count > 0)
454 next_item = TREEVIEW_GetNextListItem;
456 else if (count < 0)
458 count = -count;
459 next_item = TREEVIEW_GetPrevListItem;
461 else
462 return wineItem;
466 previousItem = wineItem;
467 wineItem = next_item(infoPtr, wineItem);
469 } while (--count && wineItem != NULL);
472 return wineItem ? wineItem : previousItem;
475 /* Notifications ************************************************************/
477 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
479 if (!infoPtr->bNtfUnicode) {
480 switch (code) {
481 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
482 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
483 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
484 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
485 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
486 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
487 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
488 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
489 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
490 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
491 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
492 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
495 return code;
498 static LRESULT
499 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
501 TRACE("wParam=%ld, lParam=%ld\n", wParam, lParam);
502 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
505 static BOOL
506 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
508 NMHDR nmhdr;
509 HWND hwnd = infoPtr->hwnd;
511 TRACE("%d\n", code);
512 nmhdr.hwndFrom = hwnd;
513 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
514 nmhdr.code = get_notifycode(infoPtr, code);
516 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, (LPARAM)&nmhdr);
519 static VOID
520 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
522 tvItem->mask = mask;
523 tvItem->hItem = item;
524 tvItem->state = item->state;
525 tvItem->stateMask = 0;
526 tvItem->iImage = item->iImage;
527 tvItem->iSelectedImage = item->iSelectedImage;
528 tvItem->cChildren = item->cChildren;
529 tvItem->lParam = item->lParam;
531 if(mask & TVIF_TEXT)
533 if (!infoPtr->bNtfUnicode)
535 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
536 tvItem->pszText = Alloc (tvItem->cchTextMax);
537 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
539 else
541 tvItem->cchTextMax = item->cchTextMax;
542 tvItem->pszText = item->pszText;
545 else
547 tvItem->cchTextMax = 0;
548 tvItem->pszText = NULL;
552 static BOOL
553 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
554 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
556 HWND hwnd = infoPtr->hwnd;
557 NMTREEVIEWW nmhdr;
558 BOOL ret;
560 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
561 code, action, oldItem, newItem);
563 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
565 nmhdr.hdr.hwndFrom = hwnd;
566 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
567 nmhdr.hdr.code = get_notifycode(infoPtr, code);
568 nmhdr.action = action;
570 if (oldItem)
571 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
573 if (newItem)
574 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
576 nmhdr.ptDrag.x = 0;
577 nmhdr.ptDrag.y = 0;
579 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
580 if (!infoPtr->bNtfUnicode)
582 Free(nmhdr.itemOld.pszText);
583 Free(nmhdr.itemNew.pszText);
585 return ret;
588 static BOOL
589 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
590 HTREEITEM dragItem, POINT pt)
592 HWND hwnd = infoPtr->hwnd;
593 NMTREEVIEWW nmhdr;
595 TRACE("code:%d dragitem:%p\n", code, dragItem);
597 nmhdr.hdr.hwndFrom = hwnd;
598 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
599 nmhdr.hdr.code = get_notifycode(infoPtr, code);
600 nmhdr.action = 0;
601 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
602 nmhdr.itemNew.hItem = dragItem;
603 nmhdr.itemNew.state = dragItem->state;
604 nmhdr.itemNew.lParam = dragItem->lParam;
606 nmhdr.ptDrag.x = pt.x;
607 nmhdr.ptDrag.y = pt.y;
609 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
613 static BOOL
614 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
615 HDC hdc, RECT rc)
617 HWND hwnd = infoPtr->hwnd;
618 NMTVCUSTOMDRAW nmcdhdr;
619 LPNMCUSTOMDRAW nmcd;
621 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
623 nmcd = &nmcdhdr.nmcd;
624 nmcd->hdr.hwndFrom = hwnd;
625 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
626 nmcd->hdr.code = NM_CUSTOMDRAW;
627 nmcd->dwDrawStage = dwDrawStage;
628 nmcd->hdc = hdc;
629 nmcd->rc = rc;
630 nmcd->dwItemSpec = 0;
631 nmcd->uItemState = 0;
632 nmcd->lItemlParam = 0;
633 nmcdhdr.clrText = infoPtr->clrText;
634 nmcdhdr.clrTextBk = infoPtr->clrBk;
635 nmcdhdr.iLevel = 0;
637 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)&nmcdhdr);
642 /* FIXME: need to find out when the flags in uItemState need to be set */
644 static BOOL
645 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
646 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
647 NMTVCUSTOMDRAW *nmcdhdr)
649 HWND hwnd = infoPtr->hwnd;
650 LPNMCUSTOMDRAW nmcd;
651 DWORD dwDrawStage;
652 DWORD_PTR dwItemSpec;
653 UINT uItemState;
654 INT retval;
656 dwDrawStage = CDDS_ITEM | uItemDrawState;
657 dwItemSpec = (DWORD_PTR)wineItem;
658 uItemState = 0;
659 if (wineItem->state & TVIS_SELECTED)
660 uItemState |= CDIS_SELECTED;
661 if (wineItem == infoPtr->selectedItem)
662 uItemState |= CDIS_FOCUS;
663 if (wineItem == infoPtr->hotItem)
664 uItemState |= CDIS_HOT;
666 nmcd = &nmcdhdr->nmcd;
667 nmcd->hdr.hwndFrom = hwnd;
668 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
669 nmcd->hdr.code = NM_CUSTOMDRAW;
670 nmcd->dwDrawStage = dwDrawStage;
671 nmcd->hdc = hdc;
672 nmcd->rc = wineItem->rect;
673 nmcd->dwItemSpec = dwItemSpec;
674 nmcd->uItemState = uItemState;
675 nmcd->lItemlParam = wineItem->lParam;
676 nmcdhdr->iLevel = wineItem->iLevel;
678 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
679 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
680 nmcd->uItemState, nmcd->lItemlParam);
682 retval = TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)nmcdhdr);
684 return retval;
687 static BOOL
688 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
690 HWND hwnd = infoPtr->hwnd;
691 NMTVDISPINFOW tvdi;
692 BOOL ret;
694 tvdi.hdr.hwndFrom = hwnd;
695 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
696 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
698 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
699 &tvdi.item, editItem);
701 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
703 if (!infoPtr->bNtfUnicode)
704 Free(tvdi.item.pszText);
706 return ret;
709 static void
710 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
711 UINT mask)
713 NMTVDISPINFOEXW callback;
714 HWND hwnd = infoPtr->hwnd;
716 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
717 mask &= wineItem->callbackMask;
719 if (mask == 0) return;
721 callback.hdr.hwndFrom = hwnd;
722 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
723 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
725 /* 'state' always contains valid value, as well as 'lParam'.
726 * All other parameters are uninitialized.
728 callback.item.pszText = wineItem->pszText;
729 callback.item.cchTextMax = wineItem->cchTextMax;
730 callback.item.mask = mask;
731 callback.item.hItem = wineItem;
732 callback.item.state = wineItem->state;
733 callback.item.lParam = wineItem->lParam;
735 /* If text is changed we need to recalculate textWidth */
736 if (mask & TVIF_TEXT)
737 wineItem->textWidth = 0;
739 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, (LPARAM)&callback);
741 /* It may have changed due to a call to SetItem. */
742 mask &= wineItem->callbackMask;
744 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
746 /* Instead of copying text into our buffer user specified its own */
747 if (!infoPtr->bNtfUnicode) {
748 LPWSTR newText;
749 int buflen;
750 int len = MultiByteToWideChar( CP_ACP, 0,
751 (LPSTR)callback.item.pszText, -1,
752 NULL, 0);
753 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
754 newText = ReAlloc(wineItem->pszText, buflen);
756 TRACE("returned str %s, len=%d, buflen=%d\n",
757 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
759 if (newText)
761 wineItem->pszText = newText;
762 MultiByteToWideChar( CP_ACP, 0,
763 (LPSTR)callback.item.pszText, -1,
764 wineItem->pszText, buflen/sizeof(WCHAR));
765 wineItem->cchTextMax = buflen/sizeof(WCHAR);
767 /* If ReAlloc fails we have nothing to do, but keep original text */
769 else {
770 int len = max(lstrlenW(callback.item.pszText) + 1,
771 TEXT_CALLBACK_SIZE);
772 LPWSTR newText = ReAlloc(wineItem->pszText, len);
774 TRACE("returned wstr %s, len=%d\n",
775 debugstr_w(callback.item.pszText), len);
777 if (newText)
779 wineItem->pszText = newText;
780 strcpyW(wineItem->pszText, callback.item.pszText);
781 wineItem->cchTextMax = len;
783 /* If ReAlloc fails we have nothing to do, but keep original text */
786 else if (mask & TVIF_TEXT) {
787 /* User put text into our buffer, that is ok unless A string */
788 if (!infoPtr->bNtfUnicode) {
789 LPWSTR newText;
790 LPWSTR oldText = NULL;
791 int buflen;
792 int len = MultiByteToWideChar( CP_ACP, 0,
793 (LPSTR)callback.item.pszText, -1,
794 NULL, 0);
795 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
796 newText = Alloc(buflen);
798 TRACE("same buffer str %s, len=%d, buflen=%d\n",
799 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
801 if (newText)
803 oldText = wineItem->pszText;
804 wineItem->pszText = newText;
805 MultiByteToWideChar( CP_ACP, 0,
806 (LPSTR)callback.item.pszText, -1,
807 wineItem->pszText, buflen/sizeof(WCHAR));
808 wineItem->cchTextMax = buflen/sizeof(WCHAR);
809 Free(oldText);
814 if (mask & TVIF_IMAGE)
815 wineItem->iImage = callback.item.iImage;
817 if (mask & TVIF_SELECTEDIMAGE)
818 wineItem->iSelectedImage = callback.item.iSelectedImage;
820 if (mask & TVIF_CHILDREN)
821 wineItem->cChildren = callback.item.cChildren;
823 /* These members are now permanently set. */
824 if (callback.item.mask & TVIF_DI_SETITEM)
825 wineItem->callbackMask &= ~callback.item.mask;
828 /***************************************************************************
829 * This function uses cChildren field to decide whether the item has
830 * children or not.
831 * Note: if this returns TRUE, the child items may not actually exist,
832 * they could be virtual.
834 * Just use wineItem->firstChild to check for physical children.
836 static BOOL
837 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
839 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
841 return wineItem->cChildren > 0;
845 /* Item Position ********************************************************/
847 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
848 static VOID
849 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
851 /* Same effect, different optimisation. */
852 #if 0
853 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
854 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
855 #else
856 BOOL lar = ((infoPtr->dwStyle
857 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
858 > TVS_LINESATROOT);
859 #endif
861 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
862 - infoPtr->scrollX;
863 item->stateOffset = item->linesOffset + infoPtr->uIndent;
864 item->imageOffset = item->stateOffset
865 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
866 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
869 static VOID
870 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
872 HDC hdc;
873 HFONT hOldFont=0;
874 SIZE sz;
876 /* DRAW's OM docker creates items like this */
877 if (item->pszText == NULL)
879 item->textWidth = 0;
880 return;
883 if (hDC != 0)
885 hdc = hDC;
887 else
889 hdc = GetDC(infoPtr->hwnd);
890 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
893 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
894 item->textWidth = sz.cx;
896 if (hDC == 0)
898 SelectObject(hdc, hOldFont);
899 ReleaseDC(0, hdc);
903 static VOID
904 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
906 item->rect.top = infoPtr->uItemHeight *
907 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
909 item->rect.bottom = item->rect.top
910 + infoPtr->uItemHeight * item->iIntegral - 1;
912 item->rect.left = 0;
913 item->rect.right = infoPtr->clientWidth;
916 /* We know that only items after start need their order updated. */
917 static void
918 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
920 TREEVIEW_ITEM *item;
921 int order;
923 if (!start)
925 start = infoPtr->root->firstChild;
926 order = 0;
928 else
929 order = start->visibleOrder;
931 for (item = start; item != NULL;
932 item = TREEVIEW_GetNextListItem(infoPtr, item))
934 if (!ISVISIBLE(item) && order > 0)
935 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
936 item->visibleOrder = order;
937 order += item->iIntegral;
940 infoPtr->maxVisibleOrder = order;
942 for (item = start; item != NULL;
943 item = TREEVIEW_GetNextListItem(infoPtr, item))
945 TREEVIEW_ComputeItemRect(infoPtr, item);
950 /* Update metrics of all items in selected subtree.
951 * root must be expanded
953 static VOID
954 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
956 TREEVIEW_ITEM *sibling;
957 HDC hdc;
958 HFONT hOldFont;
960 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
961 return;
963 root->state &= ~TVIS_EXPANDED;
964 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
965 root->state |= TVIS_EXPANDED;
967 hdc = GetDC(infoPtr->hwnd);
968 hOldFont = SelectObject(hdc, infoPtr->hFont);
970 for (; root != sibling;
971 root = TREEVIEW_GetNextListItem(infoPtr, root))
973 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
975 if (root->callbackMask & TVIF_TEXT)
976 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
978 if (root->textWidth == 0)
980 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
981 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
985 SelectObject(hdc, hOldFont);
986 ReleaseDC(infoPtr->hwnd, hdc);
989 /* Item Allocation **********************************************************/
991 static TREEVIEW_ITEM *
992 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
994 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
996 if (!newItem)
997 return NULL;
999 /* I_IMAGENONE would make more sense but this is neither what is
1000 * documented (MSDN doesn't specify) nor what Windows actually does
1001 * (it sets it to zero)... and I can so imagine an application using
1002 * inc/dec to toggle the images. */
1003 newItem->iImage = 0;
1004 newItem->iSelectedImage = 0;
1006 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1008 Free(newItem);
1009 return NULL;
1012 return newItem;
1015 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1016 * free item->pszText. */
1017 static void
1018 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1020 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1021 if (infoPtr->selectedItem == item)
1022 infoPtr->selectedItem = NULL;
1023 if (infoPtr->hotItem == item)
1024 infoPtr->hotItem = NULL;
1025 if (infoPtr->focusedItem == item)
1026 infoPtr->focusedItem = NULL;
1027 if (infoPtr->firstVisible == item)
1028 infoPtr->firstVisible = NULL;
1029 if (infoPtr->dropItem == item)
1030 infoPtr->dropItem = NULL;
1031 if (infoPtr->insertMarkItem == item)
1032 infoPtr->insertMarkItem = NULL;
1033 Free(item);
1037 /* Item Insertion *******************************************************/
1039 /***************************************************************************
1040 * This method inserts newItem before sibling as a child of parent.
1041 * sibling can be NULL, but only if parent has no children.
1043 static void
1044 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1045 TREEVIEW_ITEM *parent)
1047 assert(parent != NULL);
1049 if (sibling != NULL)
1051 assert(sibling->parent == parent);
1053 if (sibling->prevSibling != NULL)
1054 sibling->prevSibling->nextSibling = newItem;
1056 newItem->prevSibling = sibling->prevSibling;
1057 sibling->prevSibling = newItem;
1059 else
1060 newItem->prevSibling = NULL;
1062 newItem->nextSibling = sibling;
1064 if (parent->firstChild == sibling)
1065 parent->firstChild = newItem;
1067 if (parent->lastChild == NULL)
1068 parent->lastChild = newItem;
1071 /***************************************************************************
1072 * This method inserts newItem after sibling as a child of parent.
1073 * sibling can be NULL, but only if parent has no children.
1075 static void
1076 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1077 TREEVIEW_ITEM *parent)
1079 assert(parent != NULL);
1081 if (sibling != NULL)
1083 assert(sibling->parent == parent);
1085 if (sibling->nextSibling != NULL)
1086 sibling->nextSibling->prevSibling = newItem;
1088 newItem->nextSibling = sibling->nextSibling;
1089 sibling->nextSibling = newItem;
1091 else
1092 newItem->nextSibling = NULL;
1094 newItem->prevSibling = sibling;
1096 if (parent->lastChild == sibling)
1097 parent->lastChild = newItem;
1099 if (parent->firstChild == NULL)
1100 parent->firstChild = newItem;
1103 static BOOL
1104 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1105 const TVITEMEXW *tvItem, BOOL isW)
1107 UINT callbackClear = 0;
1108 UINT callbackSet = 0;
1110 TRACE("item %p\n", wineItem);
1111 /* Do this first in case it fails. */
1112 if (tvItem->mask & TVIF_TEXT)
1114 wineItem->textWidth = 0; /* force width recalculation */
1115 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1117 int len;
1118 LPWSTR newText;
1119 if (isW)
1120 len = lstrlenW(tvItem->pszText) + 1;
1121 else
1122 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1124 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1126 if (newText == NULL) return FALSE;
1128 callbackClear |= TVIF_TEXT;
1130 wineItem->pszText = newText;
1131 wineItem->cchTextMax = len;
1132 if (isW)
1133 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1134 else
1135 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1136 wineItem->pszText, len);
1138 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1140 else
1142 callbackSet |= TVIF_TEXT;
1144 wineItem->pszText = ReAlloc(wineItem->pszText,
1145 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1146 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1147 TRACE("setting callback, item %p\n", wineItem);
1151 if (tvItem->mask & TVIF_CHILDREN)
1153 wineItem->cChildren = tvItem->cChildren;
1155 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1156 callbackSet |= TVIF_CHILDREN;
1157 else
1158 callbackClear |= TVIF_CHILDREN;
1161 if (tvItem->mask & TVIF_IMAGE)
1163 wineItem->iImage = tvItem->iImage;
1165 if (wineItem->iImage == I_IMAGECALLBACK)
1166 callbackSet |= TVIF_IMAGE;
1167 else
1168 callbackClear |= TVIF_IMAGE;
1171 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1173 wineItem->iSelectedImage = tvItem->iSelectedImage;
1175 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1176 callbackSet |= TVIF_SELECTEDIMAGE;
1177 else
1178 callbackClear |= TVIF_SELECTEDIMAGE;
1181 if (tvItem->mask & TVIF_PARAM)
1182 wineItem->lParam = tvItem->lParam;
1184 /* If the application sets TVIF_INTEGRAL without
1185 * supplying a TVITEMEX structure, it's toast. */
1186 if (tvItem->mask & TVIF_INTEGRAL)
1187 wineItem->iIntegral = tvItem->iIntegral;
1189 if (tvItem->mask & TVIF_STATE)
1191 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1192 tvItem->stateMask);
1193 wineItem->state &= ~tvItem->stateMask;
1194 wineItem->state |= (tvItem->state & tvItem->stateMask);
1197 wineItem->callbackMask |= callbackSet;
1198 wineItem->callbackMask &= ~callbackClear;
1200 return TRUE;
1203 /* Note that the new item is pre-zeroed. */
1204 static LRESULT
1205 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1207 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1208 HTREEITEM insertAfter;
1209 TREEVIEW_ITEM *newItem, *parentItem;
1210 BOOL bTextUpdated = FALSE;
1212 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1214 parentItem = infoPtr->root;
1216 else
1218 parentItem = ptdi->hParent;
1220 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1222 WARN("invalid parent %p\n", parentItem);
1223 return 0;
1227 insertAfter = ptdi->hInsertAfter;
1229 /* Validate this now for convenience. */
1230 switch ((DWORD_PTR)insertAfter)
1232 case (DWORD_PTR)TVI_FIRST:
1233 case (DWORD_PTR)TVI_LAST:
1234 case (DWORD_PTR)TVI_SORT:
1235 break;
1237 default:
1238 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1239 insertAfter->parent != parentItem)
1241 WARN("invalid insert after %p\n", insertAfter);
1242 insertAfter = TVI_LAST;
1246 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1247 (tvItem->mask & TVIF_TEXT)
1248 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1249 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1250 : "<no label>");
1252 newItem = TREEVIEW_AllocateItem(infoPtr);
1253 if (newItem == NULL)
1254 return 0;
1256 newItem->parent = parentItem;
1257 newItem->iIntegral = 1;
1259 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1260 return 0;
1262 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1264 infoPtr->uNumItems++;
1266 switch ((DWORD_PTR)insertAfter)
1268 case (DWORD_PTR)TVI_FIRST:
1270 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1271 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1272 if (infoPtr->firstVisible == originalFirst)
1273 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1275 break;
1277 case (DWORD_PTR)TVI_LAST:
1278 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1279 break;
1281 /* hInsertAfter names a specific item we want to insert after */
1282 default:
1283 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1284 break;
1286 case (DWORD_PTR)TVI_SORT:
1288 TREEVIEW_ITEM *aChild;
1289 TREEVIEW_ITEM *previousChild = NULL;
1290 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1291 BOOL bItemInserted = FALSE;
1293 aChild = parentItem->firstChild;
1295 bTextUpdated = TRUE;
1296 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1298 /* Iterate the parent children to see where we fit in */
1299 while (aChild != NULL)
1301 INT comp;
1303 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1304 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1306 if (comp < 0) /* we are smaller than the current one */
1308 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1309 if (infoPtr->firstVisible == originalFirst &&
1310 aChild == originalFirst)
1311 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1312 bItemInserted = TRUE;
1313 break;
1315 else if (comp > 0) /* we are bigger than the current one */
1317 previousChild = aChild;
1319 /* This will help us to exit if there is no more sibling */
1320 aChild = (aChild->nextSibling == 0)
1321 ? NULL
1322 : aChild->nextSibling;
1324 /* Look at the next item */
1325 continue;
1327 else if (comp == 0)
1330 * An item with this name is already existing, therefore,
1331 * we add after the one we found
1333 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1334 bItemInserted = TRUE;
1335 break;
1340 * we reach the end of the child list and the item has not
1341 * yet been inserted, therefore, insert it after the last child.
1343 if ((!bItemInserted) && (aChild == NULL))
1344 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1346 break;
1351 TRACE("new item %p; parent %p, mask %x\n", newItem,
1352 newItem->parent, tvItem->mask);
1354 newItem->iLevel = newItem->parent->iLevel + 1;
1356 if (newItem->parent->cChildren == 0)
1357 newItem->parent->cChildren = 1;
1359 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1361 if (STATEIMAGEINDEX(newItem->state) == 0)
1362 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1365 if (infoPtr->firstVisible == NULL)
1366 infoPtr->firstVisible = newItem;
1368 TREEVIEW_VerifyTree(infoPtr);
1370 if (parentItem == infoPtr->root ||
1371 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1373 TREEVIEW_ITEM *item;
1374 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1376 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1377 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1379 if (!bTextUpdated)
1380 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1382 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1383 TREEVIEW_UpdateScrollBars(infoPtr);
1385 * if the item was inserted in a visible part of the tree,
1386 * invalidate it, as well as those after it
1388 for (item = newItem;
1389 item != NULL;
1390 item = TREEVIEW_GetNextListItem(infoPtr, item))
1391 TREEVIEW_Invalidate(infoPtr, item);
1393 else
1395 newItem->visibleOrder = -1;
1397 /* refresh treeview if newItem is the first item inserted under parentItem */
1398 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1400 /* parent got '+' - update it */
1401 TREEVIEW_Invalidate(infoPtr, parentItem);
1405 return (LRESULT)newItem;
1408 /* Item Deletion ************************************************************/
1409 static void
1410 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1412 static void
1413 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1415 TREEVIEW_ITEM *kill = parentItem->firstChild;
1417 while (kill != NULL)
1419 TREEVIEW_ITEM *next = kill->nextSibling;
1421 TREEVIEW_RemoveItem(infoPtr, kill);
1423 kill = next;
1426 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1427 assert(parentItem->firstChild == NULL);
1428 assert(parentItem->lastChild == NULL);
1431 static void
1432 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1434 TREEVIEW_ITEM *parentItem = item->parent;
1436 assert(item != NULL);
1437 assert(item->parent != NULL); /* i.e. it must not be the root */
1439 if (parentItem->firstChild == item)
1440 parentItem->firstChild = item->nextSibling;
1442 if (parentItem->lastChild == item)
1443 parentItem->lastChild = item->prevSibling;
1445 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1446 && parentItem->cChildren > 0)
1447 parentItem->cChildren = 0;
1449 if (item->prevSibling)
1450 item->prevSibling->nextSibling = item->nextSibling;
1452 if (item->nextSibling)
1453 item->nextSibling->prevSibling = item->prevSibling;
1456 static void
1457 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1459 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1461 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1462 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1464 if (wineItem->firstChild)
1465 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1467 TREEVIEW_UnlinkItem(wineItem);
1469 infoPtr->uNumItems--;
1471 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1472 Free(wineItem->pszText);
1474 TREEVIEW_FreeItem(infoPtr, wineItem);
1478 /* Empty out the tree. */
1479 static void
1480 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1482 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1484 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1487 static LRESULT
1488 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1490 TREEVIEW_ITEM *newSelection = NULL;
1491 TREEVIEW_ITEM *newFirstVisible = NULL;
1492 TREEVIEW_ITEM *parent, *prev = NULL;
1493 BOOL visible = FALSE;
1495 if (wineItem == TVI_ROOT)
1497 TRACE("TVI_ROOT\n");
1498 parent = infoPtr->root;
1499 newSelection = NULL;
1500 visible = TRUE;
1501 TREEVIEW_RemoveTree(infoPtr);
1503 else
1505 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1506 return FALSE;
1508 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1509 parent = wineItem->parent;
1511 if (ISVISIBLE(wineItem))
1513 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1514 visible = TRUE;
1517 if (infoPtr->selectedItem != NULL
1518 && (wineItem == infoPtr->selectedItem
1519 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1521 if (wineItem->nextSibling)
1522 newSelection = wineItem->nextSibling;
1523 else if (wineItem->parent != infoPtr->root)
1524 newSelection = wineItem->parent;
1525 else
1526 newSelection = wineItem->prevSibling;
1527 TRACE("newSelection = %p\n", newSelection);
1530 if (infoPtr->firstVisible == wineItem)
1532 if (wineItem->nextSibling)
1533 newFirstVisible = wineItem->nextSibling;
1534 else if (wineItem->prevSibling)
1535 newFirstVisible = wineItem->prevSibling;
1536 else if (wineItem->parent != infoPtr->root)
1537 newFirstVisible = wineItem->parent;
1538 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1540 else
1541 newFirstVisible = infoPtr->firstVisible;
1543 TREEVIEW_RemoveItem(infoPtr, wineItem);
1546 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1547 if (!infoPtr->selectedItem && newSelection)
1549 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1550 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1553 /* Validate insertMark dropItem.
1554 * hotItem ??? - used for comparison only.
1556 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1557 infoPtr->insertMarkItem = 0;
1559 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1560 infoPtr->dropItem = 0;
1562 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1563 newFirstVisible = infoPtr->root->firstChild;
1565 TREEVIEW_VerifyTree(infoPtr);
1568 if (visible)
1570 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1571 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1572 TREEVIEW_UpdateScrollBars(infoPtr);
1573 TREEVIEW_Invalidate(infoPtr, NULL);
1575 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1577 /* parent lost '+/-' - update it */
1578 TREEVIEW_Invalidate(infoPtr, parent);
1581 return TRUE;
1585 /* Get/Set Messages *********************************************************/
1586 static LRESULT
1587 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1589 if(wParam)
1590 infoPtr->bRedraw = TRUE;
1591 else
1592 infoPtr->bRedraw = FALSE;
1594 return 0;
1597 static LRESULT
1598 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1600 TRACE("\n");
1601 return infoPtr->uIndent;
1604 static LRESULT
1605 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1607 TRACE("\n");
1609 if (newIndent < MINIMUM_INDENT)
1610 newIndent = MINIMUM_INDENT;
1612 if (infoPtr->uIndent != newIndent)
1614 infoPtr->uIndent = newIndent;
1615 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1616 TREEVIEW_UpdateScrollBars(infoPtr);
1617 TREEVIEW_Invalidate(infoPtr, NULL);
1620 return 0;
1624 static LRESULT
1625 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1627 TRACE("\n");
1628 return (LRESULT)infoPtr->hwndToolTip;
1631 static LRESULT
1632 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1634 HWND prevToolTip;
1636 TRACE("\n");
1637 prevToolTip = infoPtr->hwndToolTip;
1638 infoPtr->hwndToolTip = hwndTT;
1640 return (LRESULT)prevToolTip;
1643 static LRESULT
1644 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1646 BOOL rc = infoPtr->bNtfUnicode;
1647 infoPtr->bNtfUnicode = fUnicode;
1648 return rc;
1651 static LRESULT
1652 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1654 return infoPtr->bNtfUnicode;
1657 static LRESULT
1658 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1660 return infoPtr->uScrollTime;
1663 static LRESULT
1664 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1666 UINT uOldScrollTime = infoPtr->uScrollTime;
1668 infoPtr->uScrollTime = min(uScrollTime, 100);
1670 return uOldScrollTime;
1674 static LRESULT
1675 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1677 TRACE("\n");
1679 switch (wParam)
1681 case (WPARAM)TVSIL_NORMAL:
1682 return (LRESULT)infoPtr->himlNormal;
1684 case (WPARAM)TVSIL_STATE:
1685 return (LRESULT)infoPtr->himlState;
1687 default:
1688 return 0;
1692 #define TVHEIGHT_MIN 16
1693 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1695 /* Compute the natural height for items. */
1696 static UINT
1697 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1699 TEXTMETRICW tm;
1700 HDC hdc = GetDC(0);
1701 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1702 UINT height;
1704 /* Height is the maximum of:
1705 * 16 (a hack because our fonts are tiny), and
1706 * The text height + border & margin, and
1707 * The size of the normal image list
1709 GetTextMetricsW(hdc, &tm);
1710 SelectObject(hdc, hOldFont);
1711 ReleaseDC(0, hdc);
1713 height = TVHEIGHT_MIN;
1714 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1715 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1716 if (height < infoPtr->normalImageHeight)
1717 height = infoPtr->normalImageHeight;
1719 /* Round down, unless we support odd ("non even") heights. */
1720 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1721 height &= ~1;
1723 return height;
1726 static LRESULT
1727 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1729 HIMAGELIST himlOld = 0;
1730 int oldWidth = infoPtr->normalImageWidth;
1731 int oldHeight = infoPtr->normalImageHeight;
1734 TRACE("%lx,%p\n", wParam, himlNew);
1736 switch (wParam)
1738 case (WPARAM)TVSIL_NORMAL:
1739 himlOld = infoPtr->himlNormal;
1740 infoPtr->himlNormal = himlNew;
1742 if (himlNew != NULL)
1743 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1744 &infoPtr->normalImageHeight);
1745 else
1747 infoPtr->normalImageWidth = 0;
1748 infoPtr->normalImageHeight = 0;
1751 break;
1753 case (WPARAM)TVSIL_STATE:
1754 himlOld = infoPtr->himlState;
1755 infoPtr->himlState = himlNew;
1757 if (himlNew != NULL)
1758 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1759 &infoPtr->stateImageHeight);
1760 else
1762 infoPtr->stateImageWidth = 0;
1763 infoPtr->stateImageHeight = 0;
1766 break;
1769 if (oldWidth != infoPtr->normalImageWidth ||
1770 oldHeight != infoPtr->normalImageHeight)
1772 BOOL bRecalcVisible = FALSE;
1774 if (oldHeight != infoPtr->normalImageHeight &&
1775 !infoPtr->bHeightSet)
1777 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1778 bRecalcVisible = TRUE;
1781 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1782 infoPtr->normalImageWidth != infoPtr->uIndent)
1784 infoPtr->uIndent = infoPtr->normalImageWidth;
1785 bRecalcVisible = TRUE;
1788 if (bRecalcVisible)
1789 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1791 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1792 TREEVIEW_UpdateScrollBars(infoPtr);
1795 TREEVIEW_Invalidate(infoPtr, NULL);
1797 return (LRESULT)himlOld;
1800 static LRESULT
1801 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1803 INT prevHeight = infoPtr->uItemHeight;
1805 TRACE("%d\n", newHeight);
1806 if (newHeight == -1)
1808 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1809 infoPtr->bHeightSet = FALSE;
1811 else
1813 infoPtr->uItemHeight = newHeight;
1814 infoPtr->bHeightSet = TRUE;
1817 /* Round down, unless we support odd ("non even") heights. */
1818 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1819 infoPtr->uItemHeight &= ~1;
1821 if (infoPtr->uItemHeight != prevHeight)
1823 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1824 TREEVIEW_UpdateScrollBars(infoPtr);
1825 TREEVIEW_Invalidate(infoPtr, NULL);
1828 return prevHeight;
1831 static LRESULT
1832 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1834 TRACE("\n");
1835 return infoPtr->uItemHeight;
1839 static LRESULT
1840 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1842 TRACE("%p\n", infoPtr->hFont);
1843 return (LRESULT)infoPtr->hFont;
1847 static INT CALLBACK
1848 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1850 (void)unused;
1852 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1854 return 1;
1857 static LRESULT
1858 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1860 UINT uHeight = infoPtr->uItemHeight;
1862 TRACE("%p %i\n", hFont, bRedraw);
1864 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1866 DeleteObject(infoPtr->hBoldFont);
1867 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1868 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1870 if (!infoPtr->bHeightSet)
1871 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1873 if (uHeight != infoPtr->uItemHeight)
1874 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1876 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1878 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1879 TREEVIEW_UpdateScrollBars(infoPtr);
1881 if (bRedraw)
1882 TREEVIEW_Invalidate(infoPtr, NULL);
1884 return 0;
1888 static LRESULT
1889 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1891 TRACE("\n");
1892 return (LRESULT)infoPtr->clrLine;
1895 static LRESULT
1896 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1898 COLORREF prevColor = infoPtr->clrLine;
1900 TRACE("\n");
1901 infoPtr->clrLine = color;
1902 return (LRESULT)prevColor;
1906 static LRESULT
1907 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1909 TRACE("\n");
1910 return (LRESULT)infoPtr->clrText;
1913 static LRESULT
1914 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1916 COLORREF prevColor = infoPtr->clrText;
1918 TRACE("\n");
1919 infoPtr->clrText = color;
1921 if (infoPtr->clrText != prevColor)
1922 TREEVIEW_Invalidate(infoPtr, NULL);
1924 return (LRESULT)prevColor;
1928 static LRESULT
1929 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1931 TRACE("\n");
1932 return (LRESULT)infoPtr->clrBk;
1935 static LRESULT
1936 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1938 COLORREF prevColor = infoPtr->clrBk;
1940 TRACE("\n");
1941 infoPtr->clrBk = newColor;
1943 if (newColor != prevColor)
1944 TREEVIEW_Invalidate(infoPtr, NULL);
1946 return (LRESULT)prevColor;
1950 static LRESULT
1951 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1953 TRACE("\n");
1954 return (LRESULT)infoPtr->clrInsertMark;
1957 static LRESULT
1958 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1960 COLORREF prevColor = infoPtr->clrInsertMark;
1962 TRACE("%x\n", color);
1963 infoPtr->clrInsertMark = color;
1965 return (LRESULT)prevColor;
1969 static LRESULT
1970 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1972 TRACE("%d %p\n", wParam, item);
1974 if (!TREEVIEW_ValidItem(infoPtr, item))
1975 return 0;
1977 infoPtr->insertBeforeorAfter = wParam;
1978 infoPtr->insertMarkItem = item;
1980 TREEVIEW_Invalidate(infoPtr, NULL);
1982 return 1;
1986 /************************************************************************
1987 * Some serious braindamage here. lParam is a pointer to both the
1988 * input HTREEITEM and the output RECT.
1990 static LRESULT
1991 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1993 TREEVIEW_ITEM *wineItem;
1994 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1996 TRACE("\n");
1998 * validate parameters
2000 if (pItem == NULL)
2001 return FALSE;
2003 wineItem = *pItem;
2004 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2005 return FALSE;
2008 * If wParam is TRUE return the text size otherwise return
2009 * the whole item size
2011 if (fTextRect)
2013 /* Windows does not send TVN_GETDISPINFO here. */
2015 lpRect->top = wineItem->rect.top;
2016 lpRect->bottom = wineItem->rect.bottom;
2018 lpRect->left = wineItem->textOffset;
2019 if (!wineItem->textWidth)
2020 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2022 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2024 else
2026 *lpRect = wineItem->rect;
2029 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2031 return TRUE;
2034 static inline LRESULT
2035 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2037 /* Surprise! This does not take integral height into account. */
2038 return infoPtr->clientHeight / infoPtr->uItemHeight;
2042 static LRESULT
2043 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2045 TREEVIEW_ITEM *wineItem;
2047 wineItem = tvItem->hItem;
2048 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2049 return FALSE;
2051 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2053 if (tvItem->mask & TVIF_CHILDREN)
2055 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2056 FIXME("I_CHILDRENCALLBACK not supported\n");
2057 tvItem->cChildren = wineItem->cChildren;
2060 if (tvItem->mask & TVIF_HANDLE)
2061 tvItem->hItem = wineItem;
2063 if (tvItem->mask & TVIF_IMAGE)
2064 tvItem->iImage = wineItem->iImage;
2066 if (tvItem->mask & TVIF_INTEGRAL)
2067 tvItem->iIntegral = wineItem->iIntegral;
2069 /* undocumented: windows ignores TVIF_PARAM and
2070 * * always sets lParam
2072 tvItem->lParam = wineItem->lParam;
2074 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2075 tvItem->iSelectedImage = wineItem->iSelectedImage;
2077 if (tvItem->mask & TVIF_STATE)
2078 /* Careful here - Windows ignores the stateMask when you get the state
2079 That contradicts the documentation, but makes more common sense, masking
2080 retrieval in this way seems overkill */
2081 tvItem->state = wineItem->state;
2083 if (tvItem->mask & TVIF_TEXT)
2085 if (wineItem->pszText == NULL)
2087 if (tvItem->cchTextMax > 0)
2088 tvItem->pszText[0] = '\0';
2090 else if (isW)
2092 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2094 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2095 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2097 else
2099 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2102 else
2104 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2106 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2107 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2109 else
2111 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2112 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2116 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2117 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2119 return TRUE;
2122 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2123 * which is wrong. */
2124 static LRESULT
2125 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2127 TREEVIEW_ITEM *wineItem;
2128 TREEVIEW_ITEM originalItem;
2130 wineItem = tvItem->hItem;
2132 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2133 tvItem->mask);
2135 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2136 return FALSE;
2138 /* store the original item values */
2139 originalItem = *wineItem;
2141 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2142 return FALSE;
2144 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2145 if ((tvItem->mask & TVIF_TEXT
2146 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2147 && ISVISIBLE(wineItem))
2149 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2150 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2153 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2155 /* The refresh updates everything, but we can't wait until then. */
2156 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2158 /* if any of the item's values changed and it's not a callback, redraw the item */
2159 if (item_changed(&originalItem, wineItem, tvItem))
2161 if (tvItem->mask & TVIF_INTEGRAL)
2163 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2164 TREEVIEW_UpdateScrollBars(infoPtr);
2166 TREEVIEW_Invalidate(infoPtr, NULL);
2168 else
2170 TREEVIEW_UpdateScrollBars(infoPtr);
2171 TREEVIEW_Invalidate(infoPtr, wineItem);
2176 return TRUE;
2179 static LRESULT
2180 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2182 TRACE("\n");
2184 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2185 return 0;
2187 return (wineItem->state & mask);
2190 static LRESULT
2191 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2193 TREEVIEW_ITEM *retval;
2195 retval = 0;
2197 /* handle all the global data here */
2198 switch (which)
2200 case TVGN_CHILD: /* Special case: child of 0 is root */
2201 if (wineItem)
2202 break;
2203 /* fall through */
2204 case TVGN_ROOT:
2205 retval = infoPtr->root->firstChild;
2206 break;
2208 case TVGN_CARET:
2209 retval = infoPtr->selectedItem;
2210 break;
2212 case TVGN_FIRSTVISIBLE:
2213 retval = infoPtr->firstVisible;
2214 break;
2216 case TVGN_DROPHILITE:
2217 retval = infoPtr->dropItem;
2218 break;
2220 case TVGN_LASTVISIBLE:
2221 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2222 break;
2225 if (retval)
2227 TRACE("flags:%x, returns %p\n", which, retval);
2228 return (LRESULT)retval;
2231 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2233 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2234 return FALSE;
2236 switch (which)
2238 case TVGN_NEXT:
2239 retval = wineItem->nextSibling;
2240 break;
2241 case TVGN_PREVIOUS:
2242 retval = wineItem->prevSibling;
2243 break;
2244 case TVGN_PARENT:
2245 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2246 break;
2247 case TVGN_CHILD:
2248 retval = wineItem->firstChild;
2249 break;
2250 case TVGN_NEXTVISIBLE:
2251 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2252 break;
2253 case TVGN_PREVIOUSVISIBLE:
2254 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2255 break;
2256 default:
2257 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2258 break;
2261 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2262 return (LRESULT)retval;
2266 static LRESULT
2267 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2269 TRACE(" %d\n", infoPtr->uNumItems);
2270 return (LRESULT)infoPtr->uNumItems;
2273 static VOID
2274 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2276 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2278 static const unsigned int state_table[] = { 0, 2, 1 };
2280 unsigned int state;
2282 state = STATEIMAGEINDEX(item->state);
2283 TRACE("state:%x\n", state);
2284 item->state &= ~TVIS_STATEIMAGEMASK;
2286 if (state < 3)
2287 state = state_table[state];
2289 item->state |= INDEXTOSTATEIMAGEMASK(state);
2291 TRACE("state:%x\n", state);
2292 TREEVIEW_Invalidate(infoPtr, item);
2297 /* Painting *************************************************************/
2299 /* Draw the lines and expand button for an item. Also draws one section
2300 * of the line from item's parent to item's parent's next sibling. */
2301 static void
2302 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2304 LONG centerx, centery;
2305 BOOL lar = ((infoPtr->dwStyle
2306 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2307 > TVS_LINESATROOT);
2308 HBRUSH hbr, hbrOld;
2309 COLORREF clrBk = infoPtr->clrBk == -1 ? comctl32_color.clrWindow:
2310 infoPtr->clrBk;
2312 if (!lar && item->iLevel == 0)
2313 return;
2315 hbr = CreateSolidBrush(clrBk);
2316 hbrOld = SelectObject(hdc, hbr);
2318 centerx = (item->linesOffset + item->stateOffset) / 2;
2319 centery = (item->rect.top + item->rect.bottom) / 2;
2321 if (infoPtr->dwStyle & TVS_HASLINES)
2323 HPEN hOldPen, hNewPen;
2324 HTREEITEM parent;
2325 LOGBRUSH lb;
2327 /* Get a dotted grey pen */
2328 lb.lbStyle = BS_SOLID;
2329 lb.lbColor = infoPtr->clrLine;
2330 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2331 hOldPen = SelectObject(hdc, hNewPen);
2333 /* Make sure the center is on a dot (using +2 instead
2334 * of +1 gives us pixel-by-pixel compat with native) */
2335 centery = (centery + 2) & ~1;
2337 MoveToEx(hdc, item->stateOffset, centery, NULL);
2338 LineTo(hdc, centerx - 1, centery);
2340 if (item->prevSibling || item->parent != infoPtr->root)
2342 MoveToEx(hdc, centerx, item->rect.top, NULL);
2343 LineTo(hdc, centerx, centery);
2346 if (item->nextSibling)
2348 MoveToEx(hdc, centerx, centery, NULL);
2349 LineTo(hdc, centerx, item->rect.bottom + 1);
2352 /* Draw the line from our parent to its next sibling. */
2353 parent = item->parent;
2354 while (parent != infoPtr->root)
2356 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2358 if (parent->nextSibling
2359 /* skip top-levels unless TVS_LINESATROOT */
2360 && parent->stateOffset > parent->linesOffset)
2362 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2363 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2366 parent = parent->parent;
2369 SelectObject(hdc, hOldPen);
2370 DeleteObject(hNewPen);
2374 * Display the (+/-) signs
2377 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2379 if (item->cChildren)
2381 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2382 if (theme)
2384 RECT glyphRect = item->rect;
2385 glyphRect.left = item->linesOffset;
2386 glyphRect.right = item->stateOffset;
2387 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2388 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2389 &glyphRect, NULL);
2391 else
2393 LONG height = item->rect.bottom - item->rect.top;
2394 LONG width = item->stateOffset - item->linesOffset;
2395 LONG rectsize = min(height, width) / 4;
2396 /* plussize = ceil(rectsize * 3/4) */
2397 LONG plussize = (rectsize + 1) * 3 / 4;
2399 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2400 HPEN hOldPen = SelectObject(hdc, hNewPen);
2402 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2403 centerx + rectsize + 2, centery + rectsize + 2);
2405 SelectObject(hdc, hOldPen);
2406 DeleteObject(hNewPen);
2408 if (height < 18 || width < 18)
2410 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2411 LineTo(hdc, centerx + plussize, centery);
2413 if (!(item->state & TVIS_EXPANDED))
2415 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2416 LineTo(hdc, centerx, centery + plussize);
2419 else
2421 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2422 centerx + plussize, centery + 2);
2424 if (!(item->state & TVIS_EXPANDED))
2426 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2427 centerx + 2, centery + plussize);
2428 SetPixel(hdc, centerx - 1, centery, clrBk);
2429 SetPixel(hdc, centerx + 1, centery, clrBk);
2435 SelectObject(hdc, hbrOld);
2436 DeleteObject(hbr);
2439 static void
2440 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2442 INT cditem;
2443 HFONT hOldFont;
2444 COLORREF oldTextColor, oldTextBkColor;
2445 int centery;
2446 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2447 NMTVCUSTOMDRAW nmcdhdr;
2449 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2451 /* - If item is drop target or it is selected and window is in focus -
2452 * use blue background (COLOR_HIGHLIGHT).
2453 * - If item is selected, window is not in focus, but it has style
2454 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2455 * - Otherwise - use background color
2457 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2458 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2459 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2461 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2463 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2464 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2466 else
2468 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2469 if (infoPtr->clrText == -1)
2470 nmcdhdr.clrText = comctl32_color.clrWindowText;
2471 else
2472 nmcdhdr.clrText = infoPtr->clrText;
2475 else
2477 nmcdhdr.clrTextBk = infoPtr->clrBk == -1 ? comctl32_color.clrWindow:
2478 infoPtr->clrBk;
2479 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2480 nmcdhdr.clrText = comctl32_color.clrHighlight;
2481 else if (infoPtr->clrText == -1)
2482 nmcdhdr.clrText = comctl32_color.clrWindowText;
2483 else
2484 nmcdhdr.clrText = infoPtr->clrText;
2487 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2489 /* The custom draw handler can query the text rectangle,
2490 * so get ready. */
2491 /* should already be known, set to 0 when changed */
2492 if (!wineItem->textWidth)
2493 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2495 cditem = 0;
2497 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2499 cditem = TREEVIEW_SendCustomDrawItemNotify
2500 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2501 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2503 if (cditem & CDRF_SKIPDEFAULT)
2505 SelectObject(hdc, hOldFont);
2506 return;
2510 if (cditem & CDRF_NEWFONT)
2511 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2513 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2515 /* Set colors. Custom draw handler can change these so we do this after it. */
2516 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2517 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2519 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2522 * Display the images associated with this item
2525 INT imageIndex;
2527 /* State images are displayed to the left of the Normal image
2528 * image number is in state; zero should be `display no image'.
2530 imageIndex = STATEIMAGEINDEX(wineItem->state);
2532 if (infoPtr->himlState && imageIndex)
2534 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2535 wineItem->stateOffset,
2536 centery - infoPtr->stateImageHeight / 2,
2537 ILD_NORMAL);
2540 /* Now, draw the normal image; can be either selected or
2541 * non-selected image.
2544 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2546 /* The item is currently selected */
2547 imageIndex = wineItem->iSelectedImage;
2549 else
2551 /* The item is not selected */
2552 imageIndex = wineItem->iImage;
2555 if (infoPtr->himlNormal)
2557 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2559 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2560 wineItem->imageOffset,
2561 centery - infoPtr->normalImageHeight / 2,
2562 ILD_NORMAL | ovlIdx);
2568 * Display the text associated with this item
2571 /* Don't paint item's text if it's being edited */
2572 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2574 if (wineItem->pszText)
2576 RECT rcText;
2578 rcText.top = wineItem->rect.top;
2579 rcText.bottom = wineItem->rect.bottom;
2580 rcText.left = wineItem->textOffset;
2581 rcText.right = rcText.left + wineItem->textWidth + 4;
2583 TRACE("drawing text %s at (%s)\n",
2584 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2586 /* Draw it */
2587 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2588 ETO_CLIPPED | ETO_OPAQUE,
2589 &rcText,
2590 wineItem->pszText,
2591 lstrlenW(wineItem->pszText),
2592 NULL);
2594 /* Draw the box around the selected item */
2595 if ((wineItem == infoPtr->selectedItem) && inFocus)
2597 DrawFocusRect(hdc,&rcText);
2603 /* Draw insertion mark if necessary */
2605 if (infoPtr->insertMarkItem)
2606 TRACE("item:%d,mark:%p\n",
2607 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2608 infoPtr->insertMarkItem);
2610 if (wineItem == infoPtr->insertMarkItem)
2612 HPEN hNewPen, hOldPen;
2613 int offset;
2614 int left, right;
2616 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2617 hOldPen = SelectObject(hdc, hNewPen);
2619 if (infoPtr->insertBeforeorAfter)
2620 offset = wineItem->rect.bottom - 1;
2621 else
2622 offset = wineItem->rect.top + 1;
2624 left = wineItem->textOffset - 2;
2625 right = wineItem->textOffset + wineItem->textWidth + 2;
2627 MoveToEx(hdc, left, offset - 3, NULL);
2628 LineTo(hdc, left, offset + 4);
2630 MoveToEx(hdc, left, offset, NULL);
2631 LineTo(hdc, right + 1, offset);
2633 MoveToEx(hdc, right, offset + 3, NULL);
2634 LineTo(hdc, right, offset - 4);
2636 SelectObject(hdc, hOldPen);
2637 DeleteObject(hNewPen);
2640 if (cditem & CDRF_NOTIFYPOSTPAINT)
2642 cditem = TREEVIEW_SendCustomDrawItemNotify
2643 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2644 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2647 /* Restore the hdc state */
2648 SetTextColor(hdc, oldTextColor);
2649 SetBkColor(hdc, oldTextBkColor);
2650 SelectObject(hdc, hOldFont);
2653 /* Computes treeHeight and treeWidth and updates the scroll bars.
2655 static void
2656 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2658 TREEVIEW_ITEM *wineItem;
2659 HWND hwnd = infoPtr->hwnd;
2660 BOOL vert = FALSE;
2661 BOOL horz = FALSE;
2662 SCROLLINFO si;
2663 LONG scrollX = infoPtr->scrollX;
2665 infoPtr->treeWidth = 0;
2666 infoPtr->treeHeight = 0;
2668 /* We iterate through all visible items in order to get the tree height
2669 * and width */
2670 wineItem = infoPtr->root->firstChild;
2672 while (wineItem != NULL)
2674 if (ISVISIBLE(wineItem))
2676 /* actually we draw text at textOffset + 2 */
2677 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2678 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2680 /* This is scroll-adjusted, but we fix this below. */
2681 infoPtr->treeHeight = wineItem->rect.bottom;
2684 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2687 /* Fix the scroll adjusted treeHeight and treeWidth. */
2688 if (infoPtr->root->firstChild)
2689 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2691 infoPtr->treeWidth += infoPtr->scrollX;
2693 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2695 /* Adding one scroll bar may take up enough space that it forces us
2696 * to add the other as well. */
2697 if (infoPtr->treeHeight > infoPtr->clientHeight)
2699 vert = TRUE;
2701 if (infoPtr->treeWidth
2702 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2703 horz = TRUE;
2705 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2706 horz = TRUE;
2708 if (!vert && horz && infoPtr->treeHeight
2709 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2710 vert = TRUE;
2712 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2714 si.cbSize = sizeof(SCROLLINFO);
2715 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2716 si.nMin = 0;
2718 if (vert)
2720 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2721 if ( si.nPage && NULL != infoPtr->firstVisible)
2723 si.nPos = infoPtr->firstVisible->visibleOrder;
2724 si.nMax = infoPtr->maxVisibleOrder - 1;
2726 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2728 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2729 ShowScrollBar(hwnd, SB_VERT, TRUE);
2730 infoPtr->uInternalStatus |= TV_VSCROLL;
2732 else
2734 if (infoPtr->uInternalStatus & TV_VSCROLL)
2735 ShowScrollBar(hwnd, SB_VERT, FALSE);
2736 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2739 else
2741 if (infoPtr->uInternalStatus & TV_VSCROLL)
2742 ShowScrollBar(hwnd, SB_VERT, FALSE);
2743 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2746 if (horz)
2748 si.nPage = infoPtr->clientWidth;
2749 si.nPos = infoPtr->scrollX;
2750 si.nMax = infoPtr->treeWidth - 1;
2752 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2754 si.nPos = si.nMax - max( si.nPage-1, 0 );
2755 scrollX = si.nPos;
2758 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2759 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2760 infoPtr->uInternalStatus |= TV_HSCROLL;
2762 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2763 TREEVIEW_HScroll(infoPtr,
2764 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2766 else
2768 if (infoPtr->uInternalStatus & TV_HSCROLL)
2769 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2770 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2772 scrollX = 0;
2773 if (infoPtr->scrollX != 0)
2775 TREEVIEW_HScroll(infoPtr,
2776 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2780 if (!horz)
2781 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2784 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2785 static LRESULT
2786 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hDC)
2788 HBRUSH hBrush;
2789 COLORREF clrBk = infoPtr->clrBk == -1 ? comctl32_color.clrWindow:
2790 infoPtr->clrBk;
2791 RECT rect;
2793 hBrush = CreateSolidBrush(clrBk);
2794 GetClientRect(infoPtr->hwnd, &rect);
2795 FillRect(hDC, &rect, hBrush);
2796 DeleteObject(hBrush);
2798 return 1;
2801 static void
2802 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2804 HWND hwnd = infoPtr->hwnd;
2805 RECT rect = *rc;
2806 TREEVIEW_ITEM *wineItem;
2808 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2810 TRACE("empty window\n");
2811 return;
2814 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2815 hdc, rect);
2817 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2819 ReleaseDC(hwnd, hdc);
2820 return;
2823 for (wineItem = infoPtr->root->firstChild;
2824 wineItem != NULL;
2825 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2827 if (ISVISIBLE(wineItem))
2829 /* Avoid unneeded calculations */
2830 if (wineItem->rect.top > rect.bottom)
2831 break;
2832 if (wineItem->rect.bottom < rect.top)
2833 continue;
2835 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2839 TREEVIEW_UpdateScrollBars(infoPtr);
2841 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2842 infoPtr->cdmode =
2843 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2846 static void
2847 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2849 if (item != NULL)
2850 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2851 else
2852 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2855 static LRESULT
2856 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2858 HDC hdc;
2859 PAINTSTRUCT ps;
2860 RECT rc;
2862 TRACE("\n");
2864 if (wParam)
2866 hdc = (HDC)wParam;
2867 GetClientRect(infoPtr->hwnd, &rc);
2868 TREEVIEW_EraseBackground(infoPtr, hdc);
2870 else
2872 hdc = BeginPaint(infoPtr->hwnd, &ps);
2873 rc = ps.rcPaint;
2876 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2877 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2879 if (!wParam)
2880 EndPaint(infoPtr->hwnd, &ps);
2882 return 0;
2886 /* Sorting **************************************************************/
2888 /***************************************************************************
2889 * Forward the DPA local callback to the treeview owner callback
2891 static INT WINAPI
2892 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2893 const TVSORTCB *pCallBackSort)
2895 /* Forward the call to the client-defined callback */
2896 return pCallBackSort->lpfnCompare(first->lParam,
2897 second->lParam,
2898 pCallBackSort->lParam);
2901 /***************************************************************************
2902 * Treeview native sort routine: sort on item text.
2904 static INT WINAPI
2905 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2906 const TREEVIEW_INFO *infoPtr)
2908 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2909 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2911 if(first->pszText && second->pszText)
2912 return lstrcmpiW(first->pszText, second->pszText);
2913 else if(first->pszText)
2914 return -1;
2915 else if(second->pszText)
2916 return 1;
2917 else
2918 return 0;
2921 /* Returns the number of physical children belonging to item. */
2922 static INT
2923 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
2925 INT cChildren = 0;
2926 HTREEITEM hti;
2928 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2929 cChildren++;
2931 return cChildren;
2934 /* Returns a DPA containing a pointer to each physical child of item in
2935 * sibling order. If item has no children, an empty DPA is returned. */
2936 static HDPA
2937 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
2939 HTREEITEM child = item->firstChild;
2941 HDPA list = DPA_Create(8);
2942 if (list == 0) return NULL;
2944 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2946 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2948 DPA_Destroy(list);
2949 return NULL;
2953 return list;
2956 /***************************************************************************
2957 * Setup the treeview structure with regards of the sort method
2958 * and sort the children of the TV item specified in lParam
2959 * fRecurse: currently unused. Should be zero.
2960 * parent: if pSort!=NULL, should equal pSort->hParent.
2961 * otherwise, item which child items are to be sorted.
2962 * pSort: sort method info. if NULL, sort on item text.
2963 * if non-NULL, sort on item's lParam content, and let the
2964 * application decide what that means. See also TVM_SORTCHILDRENCB.
2967 static LRESULT
2968 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
2969 LPTVSORTCB pSort)
2971 INT cChildren;
2972 PFNDPACOMPARE pfnCompare;
2973 LPARAM lpCompare;
2975 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2976 if (parent == TVI_ROOT || parent == NULL)
2977 parent = infoPtr->root;
2979 /* Check for a valid handle to the parent item */
2980 if (!TREEVIEW_ValidItem(infoPtr, parent))
2982 ERR("invalid item hParent=%p\n", parent);
2983 return FALSE;
2986 if (pSort)
2988 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2989 lpCompare = (LPARAM)pSort;
2991 else
2993 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2994 lpCompare = (LPARAM)infoPtr;
2997 cChildren = TREEVIEW_CountChildren(parent);
2999 /* Make sure there is something to sort */
3000 if (cChildren > 1)
3002 /* TREEVIEW_ITEM rechaining */
3003 INT count = 0;
3004 HTREEITEM item = 0;
3005 HTREEITEM nextItem = 0;
3006 HTREEITEM prevItem = 0;
3008 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3010 if (sortList == NULL)
3011 return FALSE;
3013 /* let DPA sort the list */
3014 DPA_Sort(sortList, pfnCompare, lpCompare);
3016 /* The order of DPA entries has been changed, so fixup the
3017 * nextSibling and prevSibling pointers. */
3019 item = DPA_GetPtr(sortList, count++);
3020 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3022 /* link the two current item together */
3023 item->nextSibling = nextItem;
3024 nextItem->prevSibling = item;
3026 if (prevItem == NULL)
3028 /* this is the first item, update the parent */
3029 parent->firstChild = item;
3030 item->prevSibling = NULL;
3032 else
3034 /* fix the back chaining */
3035 item->prevSibling = prevItem;
3038 /* get ready for the next one */
3039 prevItem = item;
3040 item = nextItem;
3043 /* the last item is pointed to by item and never has a sibling */
3044 item->nextSibling = NULL;
3045 parent->lastChild = item;
3047 DPA_Destroy(sortList);
3049 TREEVIEW_VerifyTree(infoPtr);
3051 if (parent->state & TVIS_EXPANDED)
3053 int visOrder = infoPtr->firstVisible->visibleOrder;
3055 if (parent == infoPtr->root)
3056 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3057 else
3058 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3060 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3062 TREEVIEW_ITEM *item;
3064 for (item = infoPtr->root->firstChild; item != NULL;
3065 item = TREEVIEW_GetNextListItem(infoPtr, item))
3067 if (item->visibleOrder == visOrder)
3068 break;
3071 if (!item) item = parent->firstChild;
3072 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3075 TREEVIEW_Invalidate(infoPtr, NULL);
3078 return TRUE;
3080 return FALSE;
3084 /***************************************************************************
3085 * Setup the treeview structure with regards of the sort method
3086 * and sort the children of the TV item specified in lParam
3088 static LRESULT
3089 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3091 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3095 /***************************************************************************
3096 * Sort the children of the TV item specified in lParam.
3098 static LRESULT
3099 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3101 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3105 /* Expansion/Collapse ***************************************************/
3107 static BOOL
3108 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3109 UINT action)
3111 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3112 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3113 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3114 0, wineItem);
3117 static VOID
3118 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3119 UINT action)
3121 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3122 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3123 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3124 0, wineItem);
3128 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3129 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3130 static BOOL
3131 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3132 BOOL bRemoveChildren, BOOL bUser)
3134 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3135 BOOL bSetSelection, bSetFirstVisible;
3136 RECT scrollRect;
3137 LONG scrollDist = 0;
3138 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3140 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3142 if (!(wineItem->state & TVIS_EXPANDED))
3143 return FALSE;
3145 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3146 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3148 if (wineItem->firstChild == NULL)
3149 return FALSE;
3151 wineItem->state &= ~TVIS_EXPANDED;
3153 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3154 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3156 bSetSelection = (infoPtr->selectedItem != NULL
3157 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3159 bSetFirstVisible = (infoPtr->firstVisible != NULL
3160 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3162 tmpItem = wineItem;
3163 while (tmpItem)
3165 if (tmpItem->nextSibling)
3167 nextItem = tmpItem->nextSibling;
3168 break;
3170 tmpItem = tmpItem->parent;
3173 if (nextItem)
3174 scrollDist = nextItem->rect.top;
3176 if (bRemoveChildren)
3178 INT old_cChildren = wineItem->cChildren;
3179 TRACE("TVE_COLLAPSERESET\n");
3180 wineItem->state &= ~TVIS_EXPANDEDONCE;
3181 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3182 wineItem->cChildren = old_cChildren;
3185 if (wineItem->firstChild)
3187 TREEVIEW_ITEM *item, *sibling;
3189 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3191 for (item = wineItem->firstChild; item != sibling;
3192 item = TREEVIEW_GetNextListItem(infoPtr, item))
3194 item->visibleOrder = -1;
3198 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3200 if (nextItem)
3201 scrollDist = -(scrollDist - nextItem->rect.top);
3203 if (bSetSelection)
3205 /* Don't call DoSelectItem, it sends notifications. */
3206 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3207 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3208 wineItem->state |= TVIS_SELECTED;
3209 infoPtr->selectedItem = wineItem;
3212 TREEVIEW_UpdateScrollBars(infoPtr);
3214 scrollRect.left = 0;
3215 scrollRect.right = infoPtr->clientWidth;
3216 scrollRect.bottom = infoPtr->clientHeight;
3218 if (nextItem)
3220 scrollRect.top = nextItem->rect.top;
3222 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3223 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3224 TREEVIEW_Invalidate(infoPtr, wineItem);
3225 } else {
3226 scrollRect.top = wineItem->rect.top;
3227 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3230 TREEVIEW_SetFirstVisible(infoPtr,
3231 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3232 TRUE);
3234 return TRUE;
3237 static BOOL
3238 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3239 BOOL bExpandPartial, BOOL bUser)
3241 LONG scrollDist;
3242 LONG orgNextTop = 0;
3243 RECT scrollRect;
3244 TREEVIEW_ITEM *nextItem, *tmpItem;
3246 TRACE("\n");
3248 if (wineItem->state & TVIS_EXPANDED)
3249 return TRUE;
3251 tmpItem = wineItem; nextItem = NULL;
3252 while (tmpItem)
3254 if (tmpItem->nextSibling)
3256 nextItem = tmpItem->nextSibling;
3257 break;
3259 tmpItem = tmpItem->parent;
3262 if (nextItem)
3263 orgNextTop = nextItem->rect.top;
3265 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3267 if (bUser || ((wineItem->cChildren != 0) &&
3268 !(wineItem->state & TVIS_EXPANDEDONCE)))
3270 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3272 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3273 return FALSE;
3276 if (!wineItem->firstChild)
3277 return FALSE;
3279 wineItem->state |= TVIS_EXPANDED;
3280 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3281 wineItem->state |= TVIS_EXPANDEDONCE;
3283 else
3285 if (!wineItem->firstChild)
3286 return FALSE;
3288 /* this item has already been expanded */
3289 wineItem->state |= TVIS_EXPANDED;
3292 if (bExpandPartial)
3293 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3295 if (ISVISIBLE(wineItem))
3297 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3298 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3299 TREEVIEW_UpdateScrollBars(infoPtr);
3301 scrollRect.left = 0;
3302 scrollRect.bottom = infoPtr->treeHeight;
3303 scrollRect.right = infoPtr->clientWidth;
3304 if (nextItem)
3306 scrollDist = nextItem->rect.top - orgNextTop;
3307 scrollRect.top = orgNextTop;
3309 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3310 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3311 TREEVIEW_Invalidate (infoPtr, wineItem);
3312 } else {
3313 scrollRect.top = wineItem->rect.top;
3314 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3317 /* Scroll up so that as many children as possible are visible.
3318 * This fails when expanding causes an HScroll bar to appear, but we
3319 * don't know that yet, so the last item is obscured. */
3320 if (wineItem->firstChild != NULL)
3322 int nChildren = wineItem->lastChild->visibleOrder
3323 - wineItem->firstChild->visibleOrder + 1;
3325 int visible_pos = wineItem->visibleOrder
3326 - infoPtr->firstVisible->visibleOrder;
3328 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3330 if (visible_pos > 0 && nChildren > rows_below)
3332 int scroll = nChildren - rows_below;
3334 if (scroll > visible_pos)
3335 scroll = visible_pos;
3337 if (scroll > 0)
3339 TREEVIEW_ITEM *newFirstVisible
3340 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3341 scroll);
3344 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3350 return TRUE;
3353 static BOOL
3354 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3356 TRACE("\n");
3358 if (wineItem->state & TVIS_EXPANDED)
3359 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3360 else
3361 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3364 static VOID
3365 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3367 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3369 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3371 if (TREEVIEW_HasChildren(infoPtr, item))
3372 TREEVIEW_ExpandAll(infoPtr, item);
3376 /* Note:If the specified item is the child of a collapsed parent item,
3377 the parent's list of child items is (recursively) expanded to reveal the
3378 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3379 know if it also applies here.
3382 static LRESULT
3383 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3385 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3386 return 0;
3388 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3389 TREEVIEW_ItemName(wineItem), flag,
3390 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3392 switch (flag & TVE_TOGGLE)
3394 case TVE_COLLAPSE:
3395 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3396 FALSE);
3398 case TVE_EXPAND:
3399 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3400 FALSE);
3402 case TVE_TOGGLE:
3403 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3405 default:
3406 return 0;
3409 #if 0
3410 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3411 #endif
3414 /* Hit-Testing **********************************************************/
3416 static TREEVIEW_ITEM *
3417 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3419 TREEVIEW_ITEM *wineItem;
3420 LONG row;
3422 if (!infoPtr->firstVisible)
3423 return NULL;
3425 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3427 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3428 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3430 if (row >= wineItem->visibleOrder
3431 && row < wineItem->visibleOrder + wineItem->iIntegral)
3432 break;
3435 return wineItem;
3438 static LRESULT
3439 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3441 TREEVIEW_ITEM *wineItem;
3442 RECT rect;
3443 UINT status;
3444 LONG x, y;
3446 lpht->hItem = 0;
3447 GetClientRect(infoPtr->hwnd, &rect);
3448 status = 0;
3449 x = lpht->pt.x;
3450 y = lpht->pt.y;
3452 if (x < rect.left)
3454 status |= TVHT_TOLEFT;
3456 else if (x > rect.right)
3458 status |= TVHT_TORIGHT;
3461 if (y < rect.top)
3463 status |= TVHT_ABOVE;
3465 else if (y > rect.bottom)
3467 status |= TVHT_BELOW;
3470 if (status)
3472 lpht->flags = status;
3473 return 0;
3476 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3477 if (!wineItem)
3479 lpht->flags = TVHT_NOWHERE;
3480 return 0;
3483 if (x >= wineItem->textOffset + wineItem->textWidth)
3485 lpht->flags = TVHT_ONITEMRIGHT;
3487 else if (x >= wineItem->textOffset)
3489 lpht->flags = TVHT_ONITEMLABEL;
3491 else if (x >= wineItem->imageOffset)
3493 lpht->flags = TVHT_ONITEMICON;
3495 else if (x >= wineItem->stateOffset)
3497 lpht->flags = TVHT_ONITEMSTATEICON;
3499 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3501 lpht->flags = TVHT_ONITEMBUTTON;
3503 else
3505 lpht->flags = TVHT_ONITEMINDENT;
3508 lpht->hItem = wineItem;
3509 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3511 return (LRESULT)wineItem;
3514 /* Item Label Editing ***************************************************/
3516 static LRESULT
3517 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3519 return (LRESULT)infoPtr->hwndEdit;
3522 static LRESULT CALLBACK
3523 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3525 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3526 BOOL bCancel = FALSE;
3527 LRESULT rc;
3529 switch (uMsg)
3531 case WM_PAINT:
3532 TRACE("WM_PAINT start\n");
3533 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3534 lParam);
3535 TRACE("WM_PAINT done\n");
3536 return rc;
3538 case WM_KILLFOCUS:
3539 if (infoPtr->bIgnoreEditKillFocus)
3540 return TRUE;
3541 break;
3543 case WM_GETDLGCODE:
3544 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3546 case WM_KEYDOWN:
3547 if (wParam == (WPARAM)VK_ESCAPE)
3549 bCancel = TRUE;
3550 break;
3552 else if (wParam == (WPARAM)VK_RETURN)
3554 break;
3557 /* fall through */
3558 default:
3559 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3562 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3563 /* eg. Using a messagebox */
3565 infoPtr->bIgnoreEditKillFocus = TRUE;
3566 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3567 infoPtr->bIgnoreEditKillFocus = FALSE;
3569 return 0;
3573 /* should handle edit control messages here */
3575 static LRESULT
3576 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3578 TRACE("%lx %ld\n", wParam, lParam);
3580 switch (HIWORD(wParam))
3582 case EN_UPDATE:
3585 * Adjust the edit window size
3587 WCHAR buffer[1024];
3588 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3589 HDC hdc = GetDC(infoPtr->hwndEdit);
3590 SIZE sz;
3591 HFONT hFont, hOldFont = 0;
3593 infoPtr->bLabelChanged = TRUE;
3595 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3597 /* Select font to get the right dimension of the string */
3598 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3600 if (hFont != 0)
3602 hOldFont = SelectObject(hdc, hFont);
3605 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3607 TEXTMETRICW textMetric;
3609 /* Add Extra spacing for the next character */
3610 GetTextMetricsW(hdc, &textMetric);
3611 sz.cx += (textMetric.tmMaxCharWidth * 2);
3613 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3614 sz.cx = min(sz.cx,
3615 infoPtr->clientWidth - editItem->textOffset + 2);
3617 SetWindowPos(infoPtr->hwndEdit,
3618 HWND_TOP,
3621 sz.cx,
3622 editItem->rect.bottom - editItem->rect.top + 3,
3623 SWP_NOMOVE | SWP_DRAWFRAME);
3626 if (hFont != 0)
3628 SelectObject(hdc, hOldFont);
3631 ReleaseDC(infoPtr->hwnd, hdc);
3632 break;
3635 default:
3636 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3639 return 0;
3642 static HWND
3643 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3645 HWND hwnd = infoPtr->hwnd;
3646 HWND hwndEdit;
3647 SIZE sz;
3648 TREEVIEW_ITEM *editItem = hItem;
3649 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3650 HDC hdc;
3651 HFONT hOldFont=0;
3652 TEXTMETRICW textMetric;
3653 static const WCHAR EditW[] = {'E','d','i','t',0};
3655 TRACE("%p %p\n", hwnd, hItem);
3656 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3657 return NULL;
3659 if (infoPtr->hwndEdit)
3660 return infoPtr->hwndEdit;
3662 infoPtr->bLabelChanged = FALSE;
3664 /* Make sure that edit item is selected */
3665 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3666 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3668 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3670 hdc = GetDC(hwnd);
3671 /* Select the font to get appropriate metric dimensions */
3672 if (infoPtr->hFont != 0)
3674 hOldFont = SelectObject(hdc, infoPtr->hFont);
3677 /* Get string length in pixels */
3678 if (editItem->pszText)
3679 GetTextExtentPoint32W(hdc, editItem->pszText, strlenW(editItem->pszText),
3680 &sz);
3681 else
3682 GetTextExtentPoint32A(hdc, "", 0, &sz);
3684 /* Add Extra spacing for the next character */
3685 GetTextMetricsW(hdc, &textMetric);
3686 sz.cx += (textMetric.tmMaxCharWidth * 2);
3688 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3689 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3691 if (infoPtr->hFont != 0)
3693 SelectObject(hdc, hOldFont);
3696 ReleaseDC(hwnd, hdc);
3697 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3698 EditW,
3700 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3701 WS_CLIPSIBLINGS | ES_WANTRETURN |
3702 ES_LEFT, editItem->textOffset - 2,
3703 editItem->rect.top - 1, sz.cx + 3,
3704 editItem->rect.bottom -
3705 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3706 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3708 infoPtr->hwndEdit = hwndEdit;
3710 /* Get a 2D border. */
3711 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3712 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3713 SetWindowLongW(hwndEdit, GWL_STYLE,
3714 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3716 SendMessageW(hwndEdit, WM_SETFONT,
3717 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3719 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3720 (DWORD_PTR)
3721 TREEVIEW_Edit_SubclassProc);
3723 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3725 DestroyWindow(hwndEdit);
3726 infoPtr->hwndEdit = 0;
3727 return NULL;
3730 infoPtr->selectedItem = hItem;
3732 if (editItem->pszText)
3733 SetWindowTextW(hwndEdit, editItem->pszText);
3735 SetFocus(hwndEdit);
3736 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3737 ShowWindow(hwndEdit, SW_SHOW);
3739 return hwndEdit;
3743 static LRESULT
3744 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3746 HWND hwnd = infoPtr->hwnd;
3747 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3748 NMTVDISPINFOW tvdi;
3749 BOOL bCommit;
3750 WCHAR tmpText[1024] = { '\0' };
3751 WCHAR *newText = tmpText;
3752 int iLength = 0;
3754 if (!infoPtr->hwndEdit)
3755 return FALSE;
3757 tvdi.hdr.hwndFrom = hwnd;
3758 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3759 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3760 tvdi.item.mask = 0;
3761 tvdi.item.hItem = editedItem;
3762 tvdi.item.state = editedItem->state;
3763 tvdi.item.lParam = editedItem->lParam;
3765 if (!bCancel)
3767 if (!infoPtr->bNtfUnicode)
3768 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3769 else
3770 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3772 if (iLength >= 1023)
3774 ERR("Insufficient space to retrieve new item label\n");
3777 tvdi.item.mask = TVIF_TEXT;
3778 tvdi.item.pszText = tmpText;
3779 tvdi.item.cchTextMax = iLength + 1;
3781 else
3783 tvdi.item.pszText = NULL;
3784 tvdi.item.cchTextMax = 0;
3787 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3789 if (!bCancel && bCommit) /* Apply the changes */
3791 if (!infoPtr->bNtfUnicode)
3793 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3794 newText = Alloc(len * sizeof(WCHAR));
3795 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3796 iLength = len - 1;
3799 if (strcmpW(newText, editedItem->pszText) != 0)
3801 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3802 if (ptr == NULL)
3804 ERR("OutOfMemory, cannot allocate space for label\n");
3805 if(newText != tmpText) Free(newText);
3806 DestroyWindow(infoPtr->hwndEdit);
3807 infoPtr->hwndEdit = 0;
3808 return FALSE;
3810 else
3812 editedItem->pszText = ptr;
3813 editedItem->cchTextMax = iLength + 1;
3814 strcpyW(editedItem->pszText, newText);
3815 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3818 if(newText != tmpText) Free(newText);
3821 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3822 DestroyWindow(infoPtr->hwndEdit);
3823 infoPtr->hwndEdit = 0;
3824 return TRUE;
3827 static LRESULT
3828 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3830 if (wParam != TV_EDIT_TIMER)
3832 ERR("got unknown timer\n");
3833 return 1;
3836 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3837 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3839 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3841 return 0;
3845 /* Mouse Tracking/Drag **************************************************/
3847 /***************************************************************************
3848 * This is quite unusual piece of code, but that's how it's implemented in
3849 * Windows.
3851 static LRESULT
3852 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3854 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3855 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3856 RECT r;
3857 MSG msg;
3859 r.top = pt.y - cyDrag;
3860 r.left = pt.x - cxDrag;
3861 r.bottom = pt.y + cyDrag;
3862 r.right = pt.x + cxDrag;
3864 SetCapture(infoPtr->hwnd);
3866 while (1)
3868 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3870 if (msg.message == WM_MOUSEMOVE)
3872 pt.x = (short)LOWORD(msg.lParam);
3873 pt.y = (short)HIWORD(msg.lParam);
3874 if (PtInRect(&r, pt))
3875 continue;
3876 else
3878 ReleaseCapture();
3879 return 1;
3882 else if (msg.message >= WM_LBUTTONDOWN &&
3883 msg.message <= WM_RBUTTONDBLCLK)
3885 if (msg.message == WM_RBUTTONUP)
3886 TREEVIEW_RButtonUp(infoPtr, &pt);
3887 break;
3890 DispatchMessageW(&msg);
3893 if (GetCapture() != infoPtr->hwnd)
3894 return 0;
3897 ReleaseCapture();
3898 return 0;
3902 static LRESULT
3903 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3905 TREEVIEW_ITEM *wineItem;
3906 TVHITTESTINFO hit;
3908 TRACE("\n");
3909 SetFocus(infoPtr->hwnd);
3911 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3913 /* If there is pending 'edit label' event - kill it now */
3914 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3917 hit.pt.x = (short)LOWORD(lParam);
3918 hit.pt.y = (short)HIWORD(lParam);
3920 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3921 if (!wineItem)
3922 return 0;
3923 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3925 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3926 { /* FIXME! */
3927 switch (hit.flags)
3929 case TVHT_ONITEMRIGHT:
3930 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3931 break;
3933 case TVHT_ONITEMINDENT:
3934 if (!(infoPtr->dwStyle & TVS_HASLINES))
3936 break;
3938 else
3940 int level = hit.pt.x / infoPtr->uIndent;
3941 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3943 while (wineItem->iLevel > level)
3945 wineItem = wineItem->parent;
3948 /* fall through */
3951 case TVHT_ONITEMLABEL:
3952 case TVHT_ONITEMICON:
3953 case TVHT_ONITEMBUTTON:
3954 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3955 break;
3957 case TVHT_ONITEMSTATEICON:
3958 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3959 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3960 else
3961 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3962 break;
3965 return TRUE;
3969 static LRESULT
3970 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3972 HWND hwnd = infoPtr->hwnd;
3973 TVHITTESTINFO ht;
3974 BOOL bTrack, bDoLabelEdit;
3975 HTREEITEM tempItem;
3977 /* If Edit control is active - kill it and return.
3978 * The best way to do it is to set focus to itself.
3979 * Edit control subclassed procedure will automatically call
3980 * EndEditLabelNow.
3982 if (infoPtr->hwndEdit)
3984 SetFocus(hwnd);
3985 return 0;
3988 ht.pt.x = (short)LOWORD(lParam);
3989 ht.pt.y = (short)HIWORD(lParam);
3991 TREEVIEW_HitTest(infoPtr, &ht);
3992 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3994 /* update focusedItem and redraw both items */
3995 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3997 infoPtr->focusedItem = ht.hItem;
3998 InvalidateRect(hwnd, &ht.hItem->rect, TRUE);
4000 if(infoPtr->selectedItem)
4001 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
4004 bTrack = (ht.flags & TVHT_ONITEM)
4005 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4008 * If the style allows editing and the node is already selected
4009 * and the click occurred on the item label...
4011 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4012 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4014 /* Send NM_CLICK right away */
4015 if (!bTrack)
4016 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4017 goto setfocus;
4019 if (ht.flags & TVHT_ONITEMBUTTON)
4021 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4022 goto setfocus;
4024 else if (bTrack)
4025 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4026 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4028 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4029 infoPtr->dropItem = ht.hItem;
4031 /* clean up focusedItem as we dragged and won't select this item */
4032 if(infoPtr->focusedItem)
4034 /* refresh the item that was focused */
4035 tempItem = infoPtr->focusedItem;
4036 infoPtr->focusedItem = 0;
4037 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
4039 /* refresh the selected item to return the filled background */
4040 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
4043 return 0;
4047 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4048 goto setfocus;
4050 if (bDoLabelEdit)
4052 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4053 KillTimer(hwnd, TV_EDIT_TIMER);
4055 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4056 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4058 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4061 * if we are TVS_SINGLEEXPAND then we want this single click to
4062 * do a bunch of things.
4064 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4065 (infoPtr->hwndEdit == 0))
4067 TREEVIEW_ITEM *SelItem;
4070 * Send the notification
4072 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4075 * Close the previous selection all the way to the root
4076 * as long as the new selection is not a child
4078 if((infoPtr->selectedItem)
4079 && (infoPtr->selectedItem != ht.hItem))
4081 BOOL closeit = TRUE;
4082 SelItem = ht.hItem;
4084 /* determine if the hitItem is a child of the currently selected item */
4085 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4087 closeit = (SelItem != infoPtr->selectedItem);
4088 SelItem = SelItem->parent;
4091 if(closeit)
4093 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4094 SelItem = infoPtr->selectedItem;
4096 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4098 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4099 SelItem = SelItem->parent;
4105 * Expand the current item
4107 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4110 /* Select the current item */
4111 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4113 else if (ht.flags & TVHT_ONITEMSTATEICON)
4115 /* TVS_CHECKBOXES requires us to toggle the current state */
4116 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4117 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4120 setfocus:
4121 SetFocus(hwnd);
4122 return 0;
4126 static LRESULT
4127 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4129 TVHITTESTINFO ht;
4131 if (infoPtr->hwndEdit)
4133 SetFocus(infoPtr->hwnd);
4134 return 0;
4137 ht.pt.x = (short)LOWORD(lParam);
4138 ht.pt.y = (short)HIWORD(lParam);
4140 TREEVIEW_HitTest(infoPtr, &ht);
4142 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4144 if (ht.hItem)
4146 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4147 infoPtr->dropItem = ht.hItem;
4150 else
4152 SetFocus(infoPtr->hwnd);
4153 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4156 return 0;
4159 static LRESULT
4160 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4162 TVHITTESTINFO ht;
4164 ht.pt = *pPt;
4166 TREEVIEW_HitTest(infoPtr, &ht);
4168 if (ht.hItem)
4170 /* Change to screen coordinate for WM_CONTEXTMENU */
4171 ClientToScreen(infoPtr->hwnd, &ht.pt);
4173 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4174 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4175 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4177 return 0;
4181 static LRESULT
4182 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4184 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4185 INT cx, cy;
4186 HDC hdc, htopdc;
4187 HWND hwtop;
4188 HBITMAP hbmp, hOldbmp;
4189 SIZE size;
4190 RECT rc;
4191 HFONT hOldFont;
4193 TRACE("\n");
4195 if (!(infoPtr->himlNormal))
4196 return 0;
4198 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4199 return 0;
4201 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4203 hwtop = GetDesktopWindow();
4204 htopdc = GetDC(hwtop);
4205 hdc = CreateCompatibleDC(htopdc);
4207 hOldFont = SelectObject(hdc, infoPtr->hFont);
4209 if (dragItem->pszText)
4210 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4211 &size);
4212 else
4213 GetTextExtentPoint32A(hdc, "", 0, &size);
4215 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4216 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4217 hOldbmp = SelectObject(hdc, hbmp);
4219 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4220 size.cx += cx;
4221 if (cy > size.cy)
4222 size.cy = cy;
4224 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4225 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4226 ILD_NORMAL);
4229 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4230 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4233 /* draw item text */
4235 SetRect(&rc, cx, 0, size.cx, size.cy);
4237 if (dragItem->pszText)
4238 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4239 DT_LEFT);
4241 SelectObject(hdc, hOldFont);
4242 SelectObject(hdc, hOldbmp);
4244 ImageList_Add(infoPtr->dragList, hbmp, 0);
4246 DeleteDC(hdc);
4247 DeleteObject(hbmp);
4248 ReleaseDC(hwtop, htopdc);
4250 return (LRESULT)infoPtr->dragList;
4253 /* Selection ************************************************************/
4255 static LRESULT
4256 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4257 INT cause)
4259 TREEVIEW_ITEM *prevSelect;
4260 RECT rcFocused;
4262 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4264 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4265 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4266 newSelect ? newSelect->state : 0);
4268 /* reset and redraw focusedItem if focusedItem was set so we don't */
4269 /* have to worry about the previously focused item when we set a new one */
4270 if(infoPtr->focusedItem)
4272 rcFocused = (infoPtr->focusedItem)->rect;
4273 infoPtr->focusedItem = 0;
4274 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4277 switch (action)
4279 case TVGN_CARET:
4280 prevSelect = infoPtr->selectedItem;
4282 if (prevSelect == newSelect) {
4283 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4284 break;
4287 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4288 TVN_SELCHANGINGW,
4289 cause,
4290 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4291 prevSelect,
4292 newSelect))
4293 return FALSE;
4295 if (prevSelect)
4296 prevSelect->state &= ~TVIS_SELECTED;
4297 if (newSelect)
4298 newSelect->state |= TVIS_SELECTED;
4300 infoPtr->selectedItem = newSelect;
4302 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4304 if (prevSelect)
4305 TREEVIEW_Invalidate(infoPtr, prevSelect);
4306 if (newSelect)
4307 TREEVIEW_Invalidate(infoPtr, newSelect);
4309 TREEVIEW_SendTreeviewNotify(infoPtr,
4310 TVN_SELCHANGEDW,
4311 cause,
4312 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4313 prevSelect,
4314 newSelect);
4315 break;
4317 case TVGN_DROPHILITE:
4318 prevSelect = infoPtr->dropItem;
4320 if (prevSelect)
4321 prevSelect->state &= ~TVIS_DROPHILITED;
4323 infoPtr->dropItem = newSelect;
4325 if (newSelect)
4326 newSelect->state |= TVIS_DROPHILITED;
4328 TREEVIEW_Invalidate(infoPtr, prevSelect);
4329 TREEVIEW_Invalidate(infoPtr, newSelect);
4330 break;
4332 case TVGN_FIRSTVISIBLE:
4333 if (newSelect != NULL)
4335 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4336 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4337 TREEVIEW_Invalidate(infoPtr, NULL);
4339 break;
4342 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4343 return TRUE;
4346 /* FIXME: handle NM_KILLFOCUS etc */
4347 static LRESULT
4348 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4350 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4351 return FALSE;
4353 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4355 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4356 return FALSE;
4358 return TRUE;
4361 /*************************************************************************
4362 * TREEVIEW_ProcessLetterKeys
4364 * Processes keyboard messages generated by pressing the letter keys
4365 * on the keyboard.
4366 * What this does is perform a case insensitive search from the
4367 * current position with the following quirks:
4368 * - If two chars or more are pressed in quick succession we search
4369 * for the corresponding string (e.g. 'abc').
4370 * - If there is a delay we wipe away the current search string and
4371 * restart with just that char.
4372 * - If the user keeps pressing the same character, whether slowly or
4373 * fast, so that the search string is entirely composed of this
4374 * character ('aaaaa' for instance), then we search for first item
4375 * that starting with that character.
4376 * - If the user types the above character in quick succession, then
4377 * we must also search for the corresponding string ('aaaaa'), and
4378 * go to that string if there is a match.
4380 * RETURNS
4382 * Zero.
4384 * BUGS
4386 * - The current implementation has a list of characters it will
4387 * accept and it ignores everything else. In particular it will
4388 * ignore accentuated characters which seems to match what
4389 * Windows does. But I'm not sure it makes sense to follow
4390 * Windows there.
4391 * - We don't sound a beep when the search fails.
4392 * - The search should start from the focused item, not from the selected
4393 * item. One reason for this is to allow for multiple selections in trees.
4394 * But currently infoPtr->focusedItem does not seem very usable.
4396 * SEE ALSO
4398 * TREEVIEW_ProcessLetterKeys
4400 static INT TREEVIEW_ProcessLetterKeys(
4401 HWND hwnd, /* handle to the window */
4402 WPARAM charCode, /* the character code, the actual character */
4403 LPARAM keyData /* key data */
4406 TREEVIEW_INFO *infoPtr;
4407 HTREEITEM nItem;
4408 HTREEITEM endidx,idx;
4409 TVITEMEXW item;
4410 WCHAR buffer[MAX_PATH];
4411 DWORD timestamp,elapsed;
4413 /* simple parameter checking */
4414 if (!hwnd || !charCode || !keyData)
4415 return 0;
4417 infoPtr=(TREEVIEW_INFO*)GetWindowLongPtrW(hwnd, 0);
4418 if (!infoPtr)
4419 return 0;
4421 /* only allow the valid WM_CHARs through */
4422 if (!isalnum(charCode) &&
4423 charCode != '.' && charCode != '`' && charCode != '!' &&
4424 charCode != '@' && charCode != '#' && charCode != '$' &&
4425 charCode != '%' && charCode != '^' && charCode != '&' &&
4426 charCode != '*' && charCode != '(' && charCode != ')' &&
4427 charCode != '-' && charCode != '_' && charCode != '+' &&
4428 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4429 charCode != '}' && charCode != '[' && charCode != '{' &&
4430 charCode != '/' && charCode != '?' && charCode != '>' &&
4431 charCode != '<' && charCode != ',' && charCode != '~')
4432 return 0;
4434 /* compute how much time elapsed since last keypress */
4435 timestamp = GetTickCount();
4436 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4437 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4438 } else {
4439 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4442 /* update the search parameters */
4443 infoPtr->lastKeyPressTimestamp=timestamp;
4444 if (elapsed < KEY_DELAY) {
4445 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4446 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4448 if (infoPtr->charCode != charCode) {
4449 infoPtr->charCode=charCode=0;
4451 } else {
4452 infoPtr->charCode=charCode;
4453 infoPtr->szSearchParam[0]=charCode;
4454 infoPtr->nSearchParamLength=1;
4455 /* Redundant with the 1 char string */
4456 charCode=0;
4459 /* and search from the current position */
4460 nItem=NULL;
4461 if (infoPtr->selectedItem != NULL) {
4462 endidx=infoPtr->selectedItem;
4463 /* if looking for single character match,
4464 * then we must always move forward
4466 if (infoPtr->nSearchParamLength == 1)
4467 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4468 else
4469 idx=endidx;
4470 } else {
4471 endidx=NULL;
4472 idx=infoPtr->root->firstChild;
4474 do {
4475 /* At the end point, sort out wrapping */
4476 if (idx == NULL) {
4478 /* If endidx is null, stop at the last item (ie top to bottom) */
4479 if (endidx == NULL)
4480 break;
4482 /* Otherwise, start again at the very beginning */
4483 idx=infoPtr->root->firstChild;
4485 /* But if we are stopping on the first child, end now! */
4486 if (idx == endidx) break;
4489 /* get item */
4490 ZeroMemory(&item, sizeof(item));
4491 item.mask = TVIF_TEXT;
4492 item.hItem = idx;
4493 item.pszText = buffer;
4494 item.cchTextMax = sizeof(buffer);
4495 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4497 /* check for a match */
4498 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4499 nItem=idx;
4500 break;
4501 } else if ( (charCode != 0) && (nItem == NULL) &&
4502 (nItem != infoPtr->selectedItem) &&
4503 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4504 /* This would work but we must keep looking for a longer match */
4505 nItem=idx;
4507 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4508 } while (idx != endidx);
4510 if (nItem != NULL) {
4511 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4512 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4516 return 0;
4519 /* Scrolling ************************************************************/
4521 static LRESULT
4522 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4524 int viscount;
4525 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4526 HTREEITEM newFirstVisible = NULL;
4527 int visible_pos = -1;
4529 if (!TREEVIEW_ValidItem(infoPtr, item))
4530 return FALSE;
4532 if (!ISVISIBLE(item))
4534 /* Expand parents as necessary. */
4535 HTREEITEM parent;
4537 /* see if we are trying to ensure that root is visible */
4538 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4539 parent = item->parent;
4540 else
4541 parent = item; /* this item is the topmost item */
4543 while (parent != infoPtr->root)
4545 if (!(parent->state & TVIS_EXPANDED))
4546 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4548 parent = parent->parent;
4552 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4554 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4555 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4557 if (hasFirstVisible)
4558 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4560 if (visible_pos < 0)
4562 /* item is before the start of the list: put it at the top. */
4563 newFirstVisible = item;
4565 else if (visible_pos >= viscount
4566 /* Sometimes, before we are displayed, GVC is 0, causing us to
4567 * spuriously scroll up. */
4568 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4570 /* item is past the end of the list. */
4571 int scroll = visible_pos - viscount;
4573 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4574 scroll + 1);
4577 if (bHScroll)
4579 /* Scroll window so item's text is visible as much as possible */
4580 /* Calculation of amount of extra space is taken from EditLabel code */
4581 INT pos, x;
4582 TEXTMETRICW textMetric;
4583 HDC hdc = GetWindowDC(infoPtr->hwnd);
4585 x = item->textWidth;
4587 GetTextMetricsW(hdc, &textMetric);
4588 ReleaseDC(infoPtr->hwnd, hdc);
4590 x += (textMetric.tmMaxCharWidth * 2);
4591 x = max(x, textMetric.tmMaxCharWidth * 3);
4593 if (item->textOffset < 0)
4594 pos = item->textOffset;
4595 else if (item->textOffset + x > infoPtr->clientWidth)
4597 if (x > infoPtr->clientWidth)
4598 pos = item->textOffset;
4599 else
4600 pos = item->textOffset + x - infoPtr->clientWidth;
4602 else
4603 pos = 0;
4605 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4608 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4610 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4612 return TRUE;
4615 return FALSE;
4618 static VOID
4619 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4620 TREEVIEW_ITEM *newFirstVisible,
4621 BOOL bUpdateScrollPos)
4623 int gap_size;
4625 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4627 if (newFirstVisible != NULL)
4629 /* Prevent an empty gap from appearing at the bottom... */
4630 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4631 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4633 if (gap_size > 0)
4635 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4636 -gap_size);
4638 /* ... unless we just don't have enough items. */
4639 if (newFirstVisible == NULL)
4640 newFirstVisible = infoPtr->root->firstChild;
4644 if (infoPtr->firstVisible != newFirstVisible)
4646 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4648 infoPtr->firstVisible = newFirstVisible;
4649 TREEVIEW_Invalidate(infoPtr, NULL);
4651 else
4653 TREEVIEW_ITEM *item;
4654 int scroll = infoPtr->uItemHeight *
4655 (infoPtr->firstVisible->visibleOrder
4656 - newFirstVisible->visibleOrder);
4658 infoPtr->firstVisible = newFirstVisible;
4660 for (item = infoPtr->root->firstChild; item != NULL;
4661 item = TREEVIEW_GetNextListItem(infoPtr, item))
4663 item->rect.top += scroll;
4664 item->rect.bottom += scroll;
4667 if (bUpdateScrollPos)
4668 SetScrollPos(infoPtr->hwnd, SB_VERT,
4669 newFirstVisible->visibleOrder, TRUE);
4671 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4676 /************************************************************************
4677 * VScroll is always in units of visible items. i.e. we always have a
4678 * visible item aligned to the top of the control. (Unless we have no
4679 * items at all.)
4681 static LRESULT
4682 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4684 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4685 TREEVIEW_ITEM *newFirstVisible = NULL;
4687 int nScrollCode = LOWORD(wParam);
4689 TRACE("wp %lx\n", wParam);
4691 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4692 return 0;
4694 if (!oldFirstVisible)
4696 assert(infoPtr->root->firstChild == NULL);
4697 return 0;
4700 switch (nScrollCode)
4702 case SB_TOP:
4703 newFirstVisible = infoPtr->root->firstChild;
4704 break;
4706 case SB_BOTTOM:
4707 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4708 break;
4710 case SB_LINEUP:
4711 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4712 break;
4714 case SB_LINEDOWN:
4715 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4716 break;
4718 case SB_PAGEUP:
4719 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4720 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4721 break;
4723 case SB_PAGEDOWN:
4724 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4725 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4726 break;
4728 case SB_THUMBTRACK:
4729 case SB_THUMBPOSITION:
4730 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4731 infoPtr->root->firstChild,
4732 (LONG)(SHORT)HIWORD(wParam));
4733 break;
4735 case SB_ENDSCROLL:
4736 return 0;
4739 if (newFirstVisible != NULL)
4741 if (newFirstVisible != oldFirstVisible)
4742 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4743 nScrollCode != SB_THUMBTRACK);
4744 else if (nScrollCode == SB_THUMBPOSITION)
4745 SetScrollPos(infoPtr->hwnd, SB_VERT,
4746 newFirstVisible->visibleOrder, TRUE);
4749 return 0;
4752 static LRESULT
4753 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4755 int maxWidth;
4756 int scrollX = infoPtr->scrollX;
4757 int nScrollCode = LOWORD(wParam);
4759 TRACE("wp %lx\n", wParam);
4761 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4762 return FALSE;
4764 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4765 /* shall never occur */
4766 if (maxWidth <= 0)
4768 scrollX = 0;
4769 goto scroll;
4772 switch (nScrollCode)
4774 case SB_LINELEFT:
4775 scrollX -= infoPtr->uItemHeight;
4776 break;
4777 case SB_LINERIGHT:
4778 scrollX += infoPtr->uItemHeight;
4779 break;
4780 case SB_PAGELEFT:
4781 scrollX -= infoPtr->clientWidth;
4782 break;
4783 case SB_PAGERIGHT:
4784 scrollX += infoPtr->clientWidth;
4785 break;
4787 case SB_THUMBTRACK:
4788 case SB_THUMBPOSITION:
4789 scrollX = (int)(SHORT)HIWORD(wParam);
4790 break;
4792 case SB_ENDSCROLL:
4793 return 0;
4796 if (scrollX > maxWidth)
4797 scrollX = maxWidth;
4798 else if (scrollX < 0)
4799 scrollX = 0;
4801 scroll:
4802 if (scrollX != infoPtr->scrollX)
4804 TREEVIEW_ITEM *item;
4805 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4807 for (item = infoPtr->root->firstChild; item != NULL;
4808 item = TREEVIEW_GetNextListItem(infoPtr, item))
4810 item->linesOffset += scroll_pixels;
4811 item->stateOffset += scroll_pixels;
4812 item->imageOffset += scroll_pixels;
4813 item->textOffset += scroll_pixels;
4816 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4817 infoPtr->scrollX = scrollX;
4818 UpdateWindow(infoPtr->hwnd);
4821 if (nScrollCode != SB_THUMBTRACK)
4822 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4824 return 0;
4827 static LRESULT
4828 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4830 short gcWheelDelta;
4831 UINT pulScrollLines = 3;
4833 if (infoPtr->firstVisible == NULL)
4834 return TRUE;
4836 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4838 gcWheelDelta = -(short)HIWORD(wParam);
4839 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4841 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4843 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4844 int maxDy = infoPtr->maxVisibleOrder;
4846 if (newDy > maxDy)
4847 newDy = maxDy;
4849 if (newDy < 0)
4850 newDy = 0;
4852 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4854 return TRUE;
4857 /* Create/Destroy *******************************************************/
4859 static void
4860 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4862 RECT rc;
4863 HBITMAP hbm, hbmOld;
4864 HDC hdc, hdcScreen;
4865 int nIndex;
4867 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4869 hdcScreen = GetDC(0);
4871 hdc = CreateCompatibleDC(hdcScreen);
4872 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4873 hbmOld = SelectObject(hdc, hbm);
4875 SetRect(&rc, 0, 0, 48, 16);
4876 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4878 SetRect(&rc, 18, 2, 30, 14);
4879 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4880 DFCS_BUTTONCHECK|DFCS_FLAT);
4882 SetRect(&rc, 34, 2, 46, 14);
4883 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4884 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4886 SelectObject(hdc, hbmOld);
4887 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4888 comctl32_color.clrWindow);
4889 TRACE("checkbox index %d\n", nIndex);
4891 DeleteObject(hbm);
4892 DeleteDC(hdc);
4893 ReleaseDC(0, hdcScreen);
4895 infoPtr->stateImageWidth = 16;
4896 infoPtr->stateImageHeight = 16;
4899 static LRESULT
4900 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4902 RECT rcClient;
4903 TREEVIEW_INFO *infoPtr;
4904 LOGFONTW lf;
4906 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4908 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4910 if (infoPtr == NULL)
4912 ERR("could not allocate info memory!\n");
4913 return 0;
4916 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4918 infoPtr->hwnd = hwnd;
4919 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4920 infoPtr->Timer = 0;
4921 infoPtr->uNumItems = 0;
4922 infoPtr->cdmode = 0;
4923 infoPtr->uScrollTime = 300; /* milliseconds */
4924 infoPtr->bRedraw = TRUE;
4926 GetClientRect(hwnd, &rcClient);
4928 /* No scroll bars yet. */
4929 infoPtr->clientWidth = rcClient.right;
4930 infoPtr->clientHeight = rcClient.bottom;
4931 infoPtr->uInternalStatus = 0;
4933 infoPtr->treeWidth = 0;
4934 infoPtr->treeHeight = 0;
4936 infoPtr->uIndent = MINIMUM_INDENT;
4937 infoPtr->selectedItem = 0;
4938 infoPtr->focusedItem = 0;
4939 infoPtr->hotItem = 0;
4940 infoPtr->firstVisible = 0;
4941 infoPtr->maxVisibleOrder = 0;
4942 infoPtr->dropItem = 0;
4943 infoPtr->insertMarkItem = 0;
4944 infoPtr->insertBeforeorAfter = 0;
4945 /* dragList */
4947 infoPtr->scrollX = 0;
4949 infoPtr->clrBk = -1; /* use system color */
4950 infoPtr->clrText = -1; /* use system color */
4951 infoPtr->clrLine = RGB(128, 128, 128);
4952 infoPtr->clrInsertMark = comctl32_color.clrBtnText;
4954 /* hwndToolTip */
4956 infoPtr->hwndEdit = 0;
4957 infoPtr->wpEditOrig = NULL;
4958 infoPtr->bIgnoreEditKillFocus = FALSE;
4959 infoPtr->bLabelChanged = FALSE;
4961 infoPtr->himlNormal = NULL;
4962 infoPtr->himlState = NULL;
4963 infoPtr->normalImageWidth = 0;
4964 infoPtr->normalImageHeight = 0;
4965 infoPtr->stateImageWidth = 0;
4966 infoPtr->stateImageHeight = 0;
4968 infoPtr->items = DPA_Create(16);
4970 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
4971 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
4972 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4973 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
4974 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
4976 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4978 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4979 infoPtr->root->state = TVIS_EXPANDED;
4980 infoPtr->root->iLevel = -1;
4981 infoPtr->root->visibleOrder = -1;
4983 infoPtr->hwndNotify = lpcs->hwndParent;
4984 #if 0
4985 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4986 #endif
4988 infoPtr->hwndToolTip = 0;
4990 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
4992 /* Determine what type of notify should be issued */
4993 /* sets infoPtr->bNtfUnicode */
4994 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
4996 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4997 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
4998 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4999 hwnd, 0, 0, 0);
5001 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5002 initialize_checkboxes(infoPtr);
5004 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5005 ShowScrollBar(hwnd, SB_VERT, FALSE);
5006 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5008 OpenThemeData (hwnd, themeClass);
5010 return 0;
5014 static LRESULT
5015 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5017 TRACE("\n");
5019 TREEVIEW_RemoveTree(infoPtr);
5021 /* tool tip is automatically destroyed: we are its owner */
5023 /* Restore original wndproc */
5024 if (infoPtr->hwndEdit)
5025 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5026 (DWORD_PTR)infoPtr->wpEditOrig);
5028 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5030 /* Deassociate treeview from the window before doing anything drastic. */
5031 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5034 DeleteObject(infoPtr->hDefaultFont);
5035 DeleteObject(infoPtr->hBoldFont);
5036 DeleteObject(infoPtr->hUnderlineFont);
5037 Free(infoPtr);
5039 return 0;
5042 /* Miscellaneous Messages ***********************************************/
5044 static LRESULT
5045 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5047 static const struct
5049 unsigned char code;
5051 scroll[] =
5053 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5054 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5055 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5056 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5057 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5058 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5059 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5060 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5061 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5062 #undef SCROLL_ENTRY
5065 if (key >= VK_PRIOR && key <= VK_DOWN)
5067 unsigned char code = scroll[key - VK_PRIOR].code;
5069 (((code & (1 << 7)) == (SB_HORZ << 7))
5070 ? TREEVIEW_HScroll
5071 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5074 return 0;
5077 /************************************************************************
5078 * TREEVIEW_KeyDown
5080 * VK_UP Move selection to the previous non-hidden item.
5081 * VK_DOWN Move selection to the next non-hidden item.
5082 * VK_HOME Move selection to the first item.
5083 * VK_END Move selection to the last item.
5084 * VK_LEFT If expanded then collapse, otherwise move to parent.
5085 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5086 * VK_ADD Expand.
5087 * VK_SUBTRACT Collapse.
5088 * VK_MULTIPLY Expand all.
5089 * VK_PRIOR Move up GetVisibleCount items.
5090 * VK_NEXT Move down GetVisibleCount items.
5091 * VK_BACK Move to parent.
5092 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5094 static LRESULT
5095 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5097 /* If it is non-NULL and different, it will be selected and visible. */
5098 TREEVIEW_ITEM *newSelection = NULL;
5100 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5102 TRACE("%lx\n", wParam);
5104 if (prevItem == NULL)
5105 return FALSE;
5107 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5108 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5110 switch (wParam)
5112 case VK_UP:
5113 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5114 if (!newSelection)
5115 newSelection = infoPtr->root->firstChild;
5116 break;
5118 case VK_DOWN:
5119 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5120 break;
5122 case VK_HOME:
5123 newSelection = infoPtr->root->firstChild;
5124 break;
5126 case VK_END:
5127 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5128 break;
5130 case VK_LEFT:
5131 if (prevItem->state & TVIS_EXPANDED)
5133 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5135 else if (prevItem->parent != infoPtr->root)
5137 newSelection = prevItem->parent;
5139 break;
5141 case VK_RIGHT:
5142 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5144 if (!(prevItem->state & TVIS_EXPANDED))
5145 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5146 else
5148 newSelection = prevItem->firstChild;
5152 break;
5154 case VK_MULTIPLY:
5155 TREEVIEW_ExpandAll(infoPtr, prevItem);
5156 break;
5158 case VK_ADD:
5159 if (!(prevItem->state & TVIS_EXPANDED))
5160 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5161 break;
5163 case VK_SUBTRACT:
5164 if (prevItem->state & TVIS_EXPANDED)
5165 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5166 break;
5168 case VK_PRIOR:
5169 newSelection
5170 = TREEVIEW_GetListItem(infoPtr, prevItem,
5171 -TREEVIEW_GetVisibleCount(infoPtr));
5172 break;
5174 case VK_NEXT:
5175 newSelection
5176 = TREEVIEW_GetListItem(infoPtr, prevItem,
5177 TREEVIEW_GetVisibleCount(infoPtr));
5178 break;
5180 case VK_BACK:
5181 newSelection = prevItem->parent;
5182 if (newSelection == infoPtr->root)
5183 newSelection = NULL;
5184 break;
5186 case VK_SPACE:
5187 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5188 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5189 break;
5192 if (newSelection && newSelection != prevItem)
5194 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5195 TVC_BYKEYBOARD))
5197 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5201 return FALSE;
5204 static LRESULT
5205 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5207 if (infoPtr->hotItem)
5209 /* remove hot effect from item */
5210 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5211 infoPtr->hotItem = NULL;
5213 return 0;
5216 static LRESULT
5217 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5219 POINT pt;
5220 TRACKMOUSEEVENT trackinfo;
5221 TREEVIEW_ITEM * item;
5223 /* fill in the TRACKMOUSEEVENT struct */
5224 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5225 trackinfo.dwFlags = TME_QUERY;
5226 trackinfo.hwndTrack = infoPtr->hwnd;
5227 trackinfo.dwHoverTime = HOVER_DEFAULT;
5229 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5230 _TrackMouseEvent(&trackinfo);
5232 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5233 if(!(trackinfo.dwFlags & TME_LEAVE))
5235 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5237 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5238 /* and can properly deactivate the hot item */
5239 _TrackMouseEvent(&trackinfo);
5242 pt.x = (short)LOWORD(lParam);
5243 pt.y = (short)HIWORD(lParam);
5245 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5247 if (item != infoPtr->hotItem)
5249 /* redraw old hot item */
5250 if (infoPtr->hotItem)
5251 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5252 infoPtr->hotItem = item;
5253 /* redraw new hot item */
5254 if (infoPtr->hotItem)
5255 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5258 return 0;
5261 /* Draw themed border */
5262 static BOOL nc_paint (const TREEVIEW_INFO *infoPtr, HRGN region)
5264 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5265 HDC dc;
5266 RECT r;
5267 HRGN cliprgn;
5268 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5269 cyEdge = GetSystemMetrics (SM_CYEDGE);
5271 if (!theme) return FALSE;
5273 GetWindowRect(infoPtr->hwnd, &r);
5275 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5276 r.right - cxEdge, r.bottom - cyEdge);
5277 if (region != (HRGN)1)
5278 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5279 OffsetRect(&r, -r.left, -r.top);
5281 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5282 OffsetRect(&r, -r.left, -r.top);
5284 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5285 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5286 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5287 ReleaseDC(infoPtr->hwnd, dc);
5289 /* Call default proc to get the scrollbars etc. painted */
5290 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5292 return TRUE;
5295 static LRESULT
5296 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5298 LPNMHDR lpnmh = (LPNMHDR)lParam;
5300 if (lpnmh->code == PGN_CALCSIZE) {
5301 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5303 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5304 lppgc->iWidth = infoPtr->treeWidth;
5305 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5306 infoPtr->treeWidth, infoPtr->clientWidth);
5308 else {
5309 lppgc->iHeight = infoPtr->treeHeight;
5310 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5311 infoPtr->treeHeight, infoPtr->clientHeight);
5313 return 0;
5315 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5318 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5320 INT format;
5322 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5324 if (nCommand != NF_REQUERY) return 0;
5326 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5327 TRACE("format=%d\n", format);
5329 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5331 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5333 return format;
5336 static LRESULT
5337 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5339 if (wParam == SIZE_RESTORED)
5341 infoPtr->clientWidth = (short)LOWORD(lParam);
5342 infoPtr->clientHeight = (short)HIWORD(lParam);
5344 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5345 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5346 TREEVIEW_UpdateScrollBars(infoPtr);
5348 else
5350 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5353 TREEVIEW_Invalidate(infoPtr, NULL);
5354 return 0;
5357 static LRESULT
5358 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5360 TRACE("(%lx %lx)\n", wParam, lParam);
5362 if (wParam == GWL_STYLE)
5364 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5366 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5368 if (dwNewStyle & TVS_CHECKBOXES)
5370 initialize_checkboxes(infoPtr);
5371 TRACE("checkboxes enabled\n");
5373 else
5375 FIXME("tried to disable checkboxes\n");
5379 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5381 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5383 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5384 TRACE("tooltips enabled\n");
5386 else
5388 DestroyWindow(infoPtr->hwndToolTip);
5389 infoPtr->hwndToolTip = 0;
5390 TRACE("tooltips disabled\n");
5394 infoPtr->dwStyle = dwNewStyle;
5397 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5398 TREEVIEW_UpdateScrollBars(infoPtr);
5399 TREEVIEW_Invalidate(infoPtr, NULL);
5401 return 0;
5404 static LRESULT
5405 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5407 POINT pt;
5408 TREEVIEW_ITEM * item;
5410 GetCursorPos(&pt);
5411 ScreenToClient(infoPtr->hwnd, &pt);
5413 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5415 /* FIXME: send NM_SETCURSOR */
5417 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5419 SetCursor(infoPtr->hcurHand);
5420 return 0;
5422 else
5423 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5426 static LRESULT
5427 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5429 TRACE("\n");
5431 if (!infoPtr->selectedItem)
5433 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5434 TVC_UNKNOWN);
5437 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5438 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5439 return 0;
5442 static LRESULT
5443 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5445 TRACE("\n");
5447 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5448 UpdateWindow(infoPtr->hwnd);
5449 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5450 return 0;
5453 /* update theme after a WM_THEMECHANGED message */
5454 static LRESULT theme_changed(const TREEVIEW_INFO *infoPtr)
5456 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5457 CloseThemeData (theme);
5458 OpenThemeData (infoPtr->hwnd, themeClass);
5459 return 0;
5463 static LRESULT WINAPI
5464 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5466 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5468 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5470 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5471 else
5473 if (uMsg == WM_CREATE)
5474 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5475 else
5476 goto def;
5479 switch (uMsg)
5481 case TVM_CREATEDRAGIMAGE:
5482 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5484 case TVM_DELETEITEM:
5485 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5487 case TVM_EDITLABELA:
5488 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5490 case TVM_EDITLABELW:
5491 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5493 case TVM_ENDEDITLABELNOW:
5494 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5496 case TVM_ENSUREVISIBLE:
5497 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5499 case TVM_EXPAND:
5500 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5502 case TVM_GETBKCOLOR:
5503 return TREEVIEW_GetBkColor(infoPtr);
5505 case TVM_GETCOUNT:
5506 return TREEVIEW_GetCount(infoPtr);
5508 case TVM_GETEDITCONTROL:
5509 return TREEVIEW_GetEditControl(infoPtr);
5511 case TVM_GETIMAGELIST:
5512 return TREEVIEW_GetImageList(infoPtr, wParam);
5514 case TVM_GETINDENT:
5515 return TREEVIEW_GetIndent(infoPtr);
5517 case TVM_GETINSERTMARKCOLOR:
5518 return TREEVIEW_GetInsertMarkColor(infoPtr);
5520 case TVM_GETISEARCHSTRINGA:
5521 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5522 return 0;
5524 case TVM_GETISEARCHSTRINGW:
5525 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5526 return 0;
5528 case TVM_GETITEMA:
5529 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5531 case TVM_GETITEMW:
5532 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5534 case TVM_GETITEMHEIGHT:
5535 return TREEVIEW_GetItemHeight(infoPtr);
5537 case TVM_GETITEMRECT:
5538 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5540 case TVM_GETITEMSTATE:
5541 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5543 case TVM_GETLINECOLOR:
5544 return TREEVIEW_GetLineColor(infoPtr);
5546 case TVM_GETNEXTITEM:
5547 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5549 case TVM_GETSCROLLTIME:
5550 return TREEVIEW_GetScrollTime(infoPtr);
5552 case TVM_GETTEXTCOLOR:
5553 return TREEVIEW_GetTextColor(infoPtr);
5555 case TVM_GETTOOLTIPS:
5556 return TREEVIEW_GetToolTips(infoPtr);
5558 case TVM_GETUNICODEFORMAT:
5559 return TREEVIEW_GetUnicodeFormat(infoPtr);
5561 case TVM_GETVISIBLECOUNT:
5562 return TREEVIEW_GetVisibleCount(infoPtr);
5564 case TVM_HITTEST:
5565 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5567 case TVM_INSERTITEMA:
5568 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, FALSE);
5570 case TVM_INSERTITEMW:
5571 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, TRUE);
5573 case TVM_SELECTITEM:
5574 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5576 case TVM_SETBKCOLOR:
5577 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5579 case TVM_SETIMAGELIST:
5580 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5582 case TVM_SETINDENT:
5583 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5585 case TVM_SETINSERTMARK:
5586 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5588 case TVM_SETINSERTMARKCOLOR:
5589 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5591 case TVM_SETITEMA:
5592 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5594 case TVM_SETITEMW:
5595 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5597 case TVM_SETLINECOLOR:
5598 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5600 case TVM_SETITEMHEIGHT:
5601 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5603 case TVM_SETSCROLLTIME:
5604 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5606 case TVM_SETTEXTCOLOR:
5607 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5609 case TVM_SETTOOLTIPS:
5610 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5612 case TVM_SETUNICODEFORMAT:
5613 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5615 case TVM_SORTCHILDREN:
5616 return TREEVIEW_SortChildren(infoPtr, lParam);
5618 case TVM_SORTCHILDRENCB:
5619 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5621 case WM_CHAR:
5622 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5624 case WM_COMMAND:
5625 return TREEVIEW_Command(infoPtr, wParam, lParam);
5627 case WM_DESTROY:
5628 return TREEVIEW_Destroy(infoPtr);
5630 /* WM_ENABLE */
5632 case WM_ERASEBKGND:
5633 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5635 case WM_GETDLGCODE:
5636 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5638 case WM_GETFONT:
5639 return TREEVIEW_GetFont(infoPtr);
5641 case WM_HSCROLL:
5642 return TREEVIEW_HScroll(infoPtr, wParam);
5644 case WM_KEYDOWN:
5645 return TREEVIEW_KeyDown(infoPtr, wParam);
5647 case WM_KILLFOCUS:
5648 return TREEVIEW_KillFocus(infoPtr);
5650 case WM_LBUTTONDBLCLK:
5651 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5653 case WM_LBUTTONDOWN:
5654 return TREEVIEW_LButtonDown(infoPtr, lParam);
5656 /* WM_MBUTTONDOWN */
5658 case WM_MOUSELEAVE:
5659 return TREEVIEW_MouseLeave(infoPtr);
5661 case WM_MOUSEMOVE:
5662 if (infoPtr->dwStyle & TVS_TRACKSELECT)
5663 return TREEVIEW_MouseMove(infoPtr, lParam);
5664 else
5665 return 0;
5667 case WM_NCLBUTTONDOWN:
5668 if (infoPtr->hwndEdit)
5669 SetFocus(infoPtr->hwnd);
5670 goto def;
5672 case WM_NCPAINT:
5673 if (nc_paint (infoPtr, (HRGN)wParam))
5674 return 0;
5675 goto def;
5677 case WM_NOTIFY:
5678 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5680 case WM_NOTIFYFORMAT:
5681 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5683 case WM_PRINTCLIENT:
5684 case WM_PAINT:
5685 return TREEVIEW_Paint(infoPtr, wParam);
5687 case WM_RBUTTONDOWN:
5688 return TREEVIEW_RButtonDown(infoPtr, lParam);
5690 case WM_SETCURSOR:
5691 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5693 case WM_SETFOCUS:
5694 return TREEVIEW_SetFocus(infoPtr);
5696 case WM_SETFONT:
5697 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5699 case WM_SETREDRAW:
5700 return TREEVIEW_SetRedraw(infoPtr, wParam);
5702 case WM_SIZE:
5703 return TREEVIEW_Size(infoPtr, wParam, lParam);
5705 case WM_STYLECHANGED:
5706 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5708 case WM_SYSCOLORCHANGE:
5709 COMCTL32_RefreshSysColors();
5710 return 0;
5712 /* WM_SYSKEYDOWN */
5714 case WM_TIMER:
5715 return TREEVIEW_HandleTimer(infoPtr, wParam);
5717 case WM_THEMECHANGED:
5718 return theme_changed (infoPtr);
5720 case WM_VSCROLL:
5721 return TREEVIEW_VScroll(infoPtr, wParam);
5723 /* WM_WININICHANGE */
5725 case WM_MOUSEWHEEL:
5726 if (wParam & (MK_SHIFT | MK_CONTROL))
5727 goto def;
5728 return TREEVIEW_MouseWheel(infoPtr, wParam);
5730 case WM_DRAWITEM:
5731 TRACE("drawItem\n");
5732 goto def;
5734 default:
5735 /* This mostly catches MFC and Delphi messages. :( */
5736 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5737 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5738 def:
5739 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5744 /* Class Registration ***************************************************/
5746 VOID
5747 TREEVIEW_Register(void)
5749 WNDCLASSW wndClass;
5751 TRACE("\n");
5753 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5754 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5755 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5756 wndClass.cbClsExtra = 0;
5757 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5759 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5760 wndClass.hbrBackground = 0;
5761 wndClass.lpszClassName = WC_TREEVIEWW;
5763 RegisterClassW(&wndClass);
5767 VOID
5768 TREEVIEW_Unregister(void)
5770 UnregisterClassW(WC_TREEVIEWW, NULL);
5774 /* Tree Verification ****************************************************/
5776 #ifdef NDEBUG
5777 static inline void
5778 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5780 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5781 TREEVIEW_ITEM *item)
5783 assert(infoPtr != NULL);
5784 assert(item != NULL);
5786 /* both NULL, or both non-null */
5787 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5789 assert(item->firstChild != item);
5790 assert(item->lastChild != item);
5792 if (item->firstChild)
5794 assert(item->firstChild->parent == item);
5795 assert(item->firstChild->prevSibling == NULL);
5798 if (item->lastChild)
5800 assert(item->lastChild->parent == item);
5801 assert(item->lastChild->nextSibling == NULL);
5804 assert(item->nextSibling != item);
5805 if (item->nextSibling)
5807 assert(item->nextSibling->parent == item->parent);
5808 assert(item->nextSibling->prevSibling == item);
5811 assert(item->prevSibling != item);
5812 if (item->prevSibling)
5814 assert(item->prevSibling->parent == item->parent);
5815 assert(item->prevSibling->nextSibling == item);
5819 static inline void
5820 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5822 assert(item != NULL);
5824 assert(item->parent != NULL);
5825 assert(item->parent != item);
5826 assert(item->iLevel == item->parent->iLevel + 1);
5828 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5830 TREEVIEW_VerifyItemCommon(infoPtr, item);
5832 TREEVIEW_VerifyChildren(infoPtr, item);
5835 static inline void
5836 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5838 TREEVIEW_ITEM *child;
5839 assert(item != NULL);
5841 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5842 TREEVIEW_VerifyItem(infoPtr, child);
5845 static inline void
5846 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5848 TREEVIEW_ITEM *root = infoPtr->root;
5850 assert(root != NULL);
5851 assert(root->iLevel == -1);
5852 assert(root->parent == NULL);
5853 assert(root->prevSibling == NULL);
5855 TREEVIEW_VerifyItemCommon(infoPtr, root);
5857 TREEVIEW_VerifyChildren(infoPtr, root);
5860 static void
5861 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5863 assert(infoPtr != NULL);
5865 TREEVIEW_VerifyRoot(infoPtr);
5867 #endif